Select count distinct
Hi, Anybody can help !
I have problem with select count distinct.
example :
select distinct custid from order_h
total result : 141 rows selected.
but :
select count(distinct custid) from order_h
result :
COUNT(DISTINCTCUSTID)
140
Why the total difference, for listing 141 but for count 140 ?
Is my statement wrong ? How to use count and distinct ?
Thank's
Look here..
http://download-uk.oracle.com/docs/cd/B19306_01/server.102/b14200/functions032.htm#i82697
Bye
Acr
Similar Messages
-
Hi,
I have a tabe "M" that as messages, each message has a priority and is part of a departement. Now i wanted to create a report that count the number of messeges by departement group by priority and the total.............. something like this:
Departement Priority1 Priority2 Priority3 Total
A 1 2 3 6
B 2 3 3 8
C 1 1 1 3
Can someboby help me please
Than's
LuisUnfortunately prior to 11g Oracle doesn't supply any means of creating cross tab queries, so any solution prior to 11g is going to rely on static views.
However, if you don't mind having the data go down instead of accross you could use this:
with m as (select 'high' priority, 'department A' department, 1 id_message from dual
union all select 'medium', 'department A', 2 from dual
union all select 'high', 'department B', 3 from dual
union all select 'medium', 'department C', 4 from dual
union all select 'high', 'department C', 5 from dual
union all select 'medium', 'department B', 6 from dual
union all select 'low', 'department A', 7 from dual
union all select 'medium', 'department A', 8 from dual
union all select 'low', 'department A', 9 from dual
union all select 'high', 'department B', 10 from dual
union all select 'low', 'department C', 11 from dual
union all select 'medium', 'department C', 12 from dual
union all select 'high', 'department B', 13 from dual
union all select 'medium', 'department B', 14 from dual
union all select 'medium', 'department B', 15 from dual
union all select 'low', 'department B', 16 from dual
union all select 'low', 'department C', 17 from dual
union all select 'high', 'department C', 18 from dual
select department, priority, count(id_message) from m
group by rollup (department, priority)
order by department, priority
DEPARTMENT PRIORITY COUNT(ID_MESSAGE)
department A high 1
department A low 2
department A medium 2
department A (null) 5
department B high 3
department B low 1
department B medium 3
department B (null) 7
department C high 2
department C low 2
department C medium 2
department C (null) 6
(null) (null) 18
13 rows selectedor
select department, priority, count(id_message) from m
group by department, rollup (priority)
order by department, priority
DEPARTMENT PRIORITY COUNT(ID_MESSAGE)
department A high 1
department A low 2
department A medium 2
department A (null) 5
department B high 3
department B low 1
department B medium 3
department B (null) 7
department C high 2
department C low 2
department C medium 2
department C (null) 6
12 rows selected -
MViews with Aggregates -- SELECT COUNT(DISTINCT(x))
Hi all,
In the documentation of MViews with Aggregates says that only SUM, COUNT, AVG, STDDEV, VARIANCE, MIN and MAX are supported for fast refresh
If we consider <expr> = DISTINCT(x)
can I fast refresh my mviews containing a COUNT(<expr>) ???
Thanks in advanced
aLeXLook here..
http://download-uk.oracle.com/docs/cd/B19306_01/server.102/b14200/functions032.htm#i82697
Bye
Acr -
Performance issue when using select count on large tables
Hello Experts,
I have a requirement where i need to get count of data from a database table.Later on i need to display the count in ALV format.
As per my requirement, I have to use this select count inside a nested loops.
Below is the count snippet:
LOOP at systems assigning <fs_sc_systems>.
LOOP at date assigning <fs_sc_date>.
SELECT COUNT( DISTINCT crmd_orderadm_i~header )
FROM crmd_orderadm_i
INNER JOIN bbp_pdigp
ON crmd_orderadm_iclient EQ bbp_pdigpclient "MANDT is referred as client
AND crmd_orderadm_iguid EQ bbp_pdigpguid
INTO w_sc_count
WHERE crmd_orderadm_i~created_at BETWEEN <fs_sc_date>-start_timestamp
AND <fs_sc_date>-end_timestamp
AND bbp_pdigp~zz_scsys EQ <fs_sc_systems>-sys_name.
endloop.
endloop.
In the above code snippet,
<fs_sc_systems>-sys_name is having the system name,
<fs_sc_date>-start_timestamp is having the start date of month
and <fs_sc_date>-end_timestamp is the end date of month.
Also the data in tables crmd_orderadm_i and bbp_pdigp is very large and it increases every day.
Now,the above select query is taking a lot of time to give the count due to which i am facing performance issues.
Can any one pls help me out to optimize this code.
Thanks,
SumanHi Choudhary Suman ,
Try this:
SELECT crmd_orderadm_i~header
INTO it_header " interna table
FROM crmd_orderadm_i
INNER JOIN bbp_pdigp
ON crmd_orderadm_iclient EQ bbp_pdigpclient
AND crmd_orderadm_iguid EQ bbp_pdigpguid
FOR ALL ENTRIES IN date
WHERE crmd_orderadm_i~created_at BETWEEN date-start_timestamp
AND date-end_timestamp
AND bbp_pdigp~zz_scsys EQ date-sys_name.
SORT it_header BY header.
DELETE ADJACENT DUPLICATES FROM it_header
COMPARING header.
describe table it_header lines v_lines.
Hope this information is help to you.
Regards,
José -
Sub-Select Count query breaking TOAD
Oracle 10.2.0.4.0
Running TOAD 9.1
I am running some SQL on our eBusiness Suite:
SELECT pha.segment1
, pha.type_lookup_code
, (SELECT COUNT(DISTINCT pha2.po_header_id)
FROM po.po_headers_all pha2
, po.po_lines_all pla
WHERE pha2.po_header_id = pla.po_header_id
AND pla.contract_id = pha.po_header_id) po_count
, (SELECT MAX(pha2.creation_date)
FROM po.po_headers_all pha2
, po.po_lines_all pla
WHERE pha2.po_header_id = pla.po_header_id
AND pla.contract_id = pha.po_header_id) latest_cpa_po
FROM po.po_headers_all pha
, po.po_vendors pv
, po.po_vendor_sites_all pvsa
WHERE pha.vendor_id = pv.vendor_id
AND pha.vendor_site_id = pvsa.vendor_site_id
-- AND pv.VENDOR_NAME LIKE 'H%'
AND pha.vendor_id = 98
AND pha.type_lookup_code = 'CONTRACT'
AND pha.org_id IN(7041, 7042);The above query runs quicky (approx. 1 second). If I take out the AND pha.vendor_id = 98 then the query takes a few minutes to run.
When I try to export it, or scroll down to view > 500 rows, TOAD crashes.
I know this isn't a TOAD forum, but I think that this is probably an issue with my no doubt rubbish SQL.
If I take out this sub-select, then the problem doesn't happen:
, (SELECT COUNT(DISTINCT pha2.po_header_id)
FROM po.po_headers_all pha2
, po.po_lines_all pla
WHERE pha2.po_header_id = pla.po_header_id
AND pla.contract_id = pha.po_header_id) po_countHowever, I can't work out a better way of getting the data I need.
The sub-select counts POs which have been raised where the contractID on the PO line is the same as the PO Header ID from the main query.
Any advice please, on what I could do to sort this out would be much appreciated.
Thanks!Hi,
It looks like you can replace both scalar sub-queries with a join, like this:
WITH header_lines_summary AS
SELECT pla.contract_id
, COUNT (DISTINCT pha2.po_header_id) AS po_count
, MAX (pha2.creation_date) AS latest_cpa_po
FROM po.po_headers_all pha2
, po.po_lines_all pla
WHERE pha2.po_header_id = pla.po_header_id
GROUP BY pla.contract_id
) -- Everything up to this line is new
SELECT pha.segment1
, pha.type_lookup_code
, hls.po_count -- Changed
, hls.latest_cpa_po -- Changed
FROM po.po_headers_all pha
, po.po_vendors pv
, po.po_vendor_sites_all pvsa
, header_lines_summary hls -- New
WHERE pha.vendor_id = pv.vendor_id
AND pha.vendor_site_id = pvsa.vendor_site_id
AND pha.po_header_id = hls.contract_id (+) -- New
-- AND pv.VENDOR_NAME LIKE 'H%'
AND pha.vendor_id = 98
AND pha.type_lookup_code = 'CONTRACT'
AND pha.org_id IN (7041, 7042);Aside from the sub-query (which is entirely new), the query above is just what you posted, with 2 lines changed and 2 lines added, as marked.
This should be more efficient, but I don't know for certain that it will solve the Toad problem.
I hope this answers your question.
If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables, and also post the results you want from that data.
It never hurts to say what version of Oracle you're using. -
"group by" slow for using "count(distinct some_column)" - a better way?
Hi all,
i have an
select
count(distinct some_column),
from [...]
group by [...];
Which is slowed down for the "*count(distinct some_column)*".
The "group by" aggregates base records.
But the base records have 1:n for some #1 event #n records each.
Some of the #n records fall into group by result record (A), some other into group by result record (B).
But each shall only count +1 per event - disregarding how many of the #n record have fallen into that category.
Is there another (faster) way to count for this?
- thanks!
best regards,
Frank
Edited by: user8704911 on Jun 29, 2011 1:30 AMHi Dom,
incidentally i went in the direction you proposed:
I replaced the pl/sql collection with the global temporary table.
But the reason for doing this was a different one:
I recognized, that the group by is much faster, if applied on table or global temporary table.
However i first just moved the data from pl/sql collection to global temporary table in order to apply the group by there.
Then the group by is much faster - but the moving of data from pl/sql collection to global temporary table then took away the time.
So it was not the group by, but in general the read-access to the pl/sql collection (btw, around #65,000 records).
Now having completely replaced the pl/sql collection with global temporary table everything is fine.
cheers,
Frank -
Hi everyone,
An analyst on my team heard of a new metric called a "Stickiness" metric. It basically measures how often users are coming to your website overtime.
The definition is as follows:
# Unique Users Today/#Unique users Over Last 7 days
and also
# Unique Users Today/#Unique users Over Last 30 days
We have visit information stored in a table W_WEB_VISIT_F. For the sake of simplicity say it has columns VISIT_ID, VISIT_DATE and USER_ID (there are several more dimensional columns it has but I want to keep this exercise simple).
I want to create an aggregate table called W_WEB_VISIT_A that pre-aggregates the three values I need per day: # Unique Users Today, #Unique users Over Last 7 days and #Unique users Over Last 30 days. The only way I can think of building the aggregate table is as follows
WITH AGG AS (
SELECT
VISIT_DATE,
USER_ID
FROM W_WEB_VISIT_F
GROUP BY
VISIT_DATE,
USER_ID
select
VISIT_DATE
COUNT(DISTINCT USER_ID) UNIQUE_TODAY,
(select count(distinct hist.USER_ID) from agg hist where hist.VISIT_DATE between src.VISIT_DATE - 6 and src.VISIT_DATE) SEVEN_DAYS,
(select count(distinct hist.USER_ID) from agg hist where hist.VISIT_DATE between src.VISIT_DATE - 29 and src.VISIT_DATE) THIRTY_DAYS
from agg
group by visit_date
The problem I am having is that W_WEB_VISIT_F has several million records in it and I can't get it the above query to complete. It ran over night and didn't complete.
Is there a fancy 11g function I can use to do this for me? Is there a more efficient method?
Thanks everyone for the help!
-Joe
Edited by: user9208525 on Jan 13, 2011 6:24 AM
You guys are right. I missed the group by I had in the WITH Clause.Hi,
Haven't used the windowing clause a lot, so I wanted to give a try.
I made up some data with this query :create table t as select sysdate-dbms_random.value(0,10) visit_date, mod(level,5)+1 user_id
from dual
connect by level <= 20;Which gave me following rows :Scott@my10g SQL>select * from t order by visit_date;
VISIT_DATE USER_ID
03/01/2011 13:17:10 1
04/01/2011 05:30:30 4
04/01/2011 08:08:13 5
04/01/2011 14:42:24 3
04/01/2011 20:20:58 3
05/01/2011 17:29:24 2
05/01/2011 17:40:20 4
05/01/2011 18:32:56 2
06/01/2011 04:12:53 5
06/01/2011 08:59:18 2
06/01/2011 09:04:26 3
06/01/2011 10:14:20 1
06/01/2011 14:22:54 1
06/01/2011 19:39:04 1
08/01/2011 14:44:18 5
08/01/2011 21:38:04 5
11/01/2011 04:56:05 4
11/01/2011 18:52:29 2
11/01/2011 23:57:30 4
13/01/2011 07:24:22 3
20 rows selected.I came up to that query :select
v.*,
case
when unq_l3d is null then -1
else trunc(unq_today/unq_l3d,2)
end ratio
from (
select distinct trcdt, unq_today, unq_l3d
from (
select
trcdt,
count(user_id)
over (
order by trcdt
range between numtodsinterval(1,'DAY') preceding and current row
) unq_today,
count(user_id)
over (
order by trcdt
range between numtodsinterval(3,'DAY') preceding and current row
) unq_l3d
from (
select distinct trunc(visit_date) trcdt, user_id from t
) v
order by trcdtWith my sample data, it gives me :TRCDT UNQ_TODAY UNQ_L3D RATIO
03/01/2011 00:00:00 1 1 1.00
04/01/2011 00:00:00 4 4 1.00
05/01/2011 00:00:00 5 6 0.83
06/01/2011 00:00:00 6 10 0.60
08/01/2011 00:00:00 1 7 0.14
11/01/2011 00:00:00 2 3 0.66
13/01/2011 00:00:00 1 3 0.33
7 rows selected.where :
- UNQ_TODAY is the number of distinct user_id in the day
- UNQ_L3D is the number of distinct user_id in the last 3 days
- RATIO is UNQ_TODAY divided by UNQ_L3D +(when UNQ_L3D is not zero)+
It seems quite correct, but you would have to modify the query to fit to your needs and double-check the results !
Just noticed that my query is all wrong*... must have been missing coffeine, or sleep.... but I'm still trying !
Edited by: Nicosa on Jan 13, 2011 5:29 PM -
Counting distinct values???
Hi all,
Why does this not work?
"Select count(distinct employee_id) from employee_table"
I am looking to return the count of the distinct employee_id.because its not valid SQL
try:
select * from employee_table where employee_id in (select distinct employee_id from employee_table)not very neat but should work (on oracle or SQLserveR) -
Efficiency of "Count(Distinct Case" in SQL
Hi,
Could you please let me know if "Count(Distinct Case" statement is efficient for a million rows or is there a better way to do it
For example -this table below contains a set of customers with status flag as 'new' or 'existing'.
CREATE TABLE tableA
( cust_id NUMBER
, status VARCHAR(10)
,txn_id NUMBER
INSERT INTO tableA (cust_id, status,txn_id) VALUES ( 6433, 'New', 11);
INSERT INTO tableA (cust_id, status,txn_id) VALUES ( 6433, 'New', 21);
INSERT INTO tableA (cust_id, status,txn_id) VALUES ( 1234, 'existing', 31);
INSERT INTO tableA (cust_id, status,txn_id) VALUES ( 1234, 'existing', 41);
INSERT INTO tableA (cust_id, status,txn_id) VALUES ( 7654, 'New', 51);
INSERT INTO tableA (cust_id, status,txn_id) VALUES ( 7654, 'New', 61);
INSERT INTO tableA (cust_id, status,txn_id) VALUES ( 9999, 'existing', 71);
INSERT INTO tableA (cust_id, status,txn_id) VALUES ( 8888, 'New', 81);
INSERT INTO tableA (cust_id, status,txn_id) VALUES ( 8888, 'existing', 91);
INSERT INTO tableA (cust_id, status,txn_id) VALUES ( 2121, 'New', 100);
am using the below SQL to calculate the number of distinct customers with status 'New'.
Select
Count(Distinct Case When status = 'New' Then cust_id end) New_Cust_Cnt
from tableA
Regards
-Learnsequelsan wrote:
Hello,
Select
Count(Distinct Case When status = 'New' Then cust_id end) New_Cust_Cnt
from tableA
_Use like this:_
Select
Count(cust_id) New_Cust_Cnt
from tableA
where status='new';And also you can create index on status you will get faster.
Thanks,
SanjeevaAny how you have to use DISTINCT keywork. Otherwise you will not get the correct results for the OP's data. -
COUNT(DISTINCT) on multiple columns?
Is there an easier way of doing a COUNT(DISTINCT...) on multiple items than converting them to strings and concatenating them?
i.e. if I have a table with column string1 as VARCHAR2(1000), number2 as NUMBER, and date3 as DATE, and I want a count on how many distinct combinations of the three exist, is there a better way than:
SELECT COUNT(DISTINCT string1 || TO_CHAR(number2) || TO_CHAR(date3, 'YYYYMMDD'))-- DonHi,
Why not a group by?
SQL> ed
Wrote file afiedt.buf
1 with t as
2 (
3 select 'string1' string1, 1 number1, to_date('10-NOV-2009','DD-MON-YYYY') date1 from dual
4 union all select 'string2',1,to_date('10-NOV-2009','DD-MON-YYYY') from dual
5 union all select 'string1',1,to_date('11-NOV-2009','DD-MON-YYYY') from dual
6 union all select 'string1',2,to_date('11-NOV-2009','DD-MON-YYYY') from dual
7 union all select 'string2',1,to_date('10-NOV-2009','DD-MON-YYYY') from dual
8 )
9 select string1, number1, date1 from t
10* group by string1, number1, date1
SQL> /
STRING1 NUMBER1 DATE1
string1 1 11-NOV-09
string2 1 10-NOV-09
string1 1 10-NOV-09
string1 2 11-NOV-09
SQL> ed
Wrote file afiedt.buf
1 with t as
2 (
3 select 'string1' string1, 1 number1, to_date('10-NOV-2009','DD-MON-YYYY') date1 from dual
4 union all select 'string2',1,to_date('10-NOV-2009','DD-MON-YYYY') from dual
5 union all select 'string1',1,to_date('11-NOV-2009','DD-MON-YYYY') from dual
6 union all select 'string1',2,to_date('11-NOV-2009','DD-MON-YYYY') from dual
7 union all select 'string2',1,to_date('10-NOV-2009','DD-MON-YYYY') from dual
8 )
9 select string1, number1, date1 from t
10 group by string1, number1, date1
11* having count(*) > 1
SQL> /
STRING1 NUMBER1 DATE1
string2 1 10-NOV-09-Arun -
Performance problem with more than one COUNT(DISTINCT ...) in a query
Hi,
(I hope this is the good forum).
In the following query, I have 2 Count Distinct on 2 different fields of the same table. Execution time is okay (2 s) with one or the other COUNT(DISCTINCT ...) in the SELECT clause, but is not tolerable (12 s) with both together in the query! I have
a similar case with 3 counts: 4 s each, 36 s when together!
I've looked at the execution plan, and it seems that with two count distinct, SQL server sorts the table twice before joining the results.
I do not have much experience with SQL server optimization, and I don't know what to improve and how. The SQL is generated by Business Objects, I have few possibilities to tune it. The most direct way would be to execute 2 different queries, but I'd like
to avoid it.
Any advice?
SELECT
DIM_MOIS.DATE_DEBUT_MOIS,
DIM_MOIS.NUM_ANNEE_MOIS,
DIM_DEMANDE_SCD.CAT_DEMANDE,
DIM_APPLICATION.LIB_APPLICATION,
DIM_DEMANDE_SCD.CAT_DEMANDE ,
count(distinct FAITS_DEMANDE.NB_DEMANDE_FLUX),
count(distinct FAITS_DEMANDE.NB_DEMANDE_RESOL_NIV1)
FROM
ALIM_SID.DIM_MOIS INNER JOIN ALIM_SID.DIM_JOUR ON (DIM_JOUR.SEQ_MOIS=DIM_MOIS.SEQ_MOIS)
INNER JOIN ALIM_SID.FAITS_DEMANDE ON (FAITS_DEMANDE.SEQ_JOUR=DIM_JOUR.SEQ_JOUR)
INNER JOIN ALIM_SID.DIM_APPLICATION ON (FAITS_DEMANDE.SEQ_APPLICATION=DIM_APPLICATION.SEQ_APPLICATION)
INNER JOIN ALIM_SID.DIM_DEMANDE_SCD ON (FAITS_DEMANDE.SEQ_DEMANDE_SCD=DIM_DEMANDE_SCD.SEQ_DEMANDE_SCD)
WHERE
( ( DIM_MOIS.NUM_ANNEE_MOIS ) >201301
GROUP BY
DIM_MOIS.DATE_DEBUT_MOIS,
DIM_MOIS.NUM_ANNEE_MOIS,
DIM_DEMANDE_SCD.CAT_DEMANDE,
DIM_APPLICATION.LIB_APPLICATIONHere is the script, nothing original. Hope this helps.
-- Fact table :
-- foreign keys begin by FK_,
-- measures to counted (COUNT DISTINCT) begin with NB_
CREATE TABLE [ALIM_SID].[FAITS_DEMANDE](
[SEQ_JOUR] [int] NOT NULL,
[SEQ_DEMANDE] [int] NOT NULL,
[SEQ_DEMANDE_SCD] [int] NOT NULL,
[SEQ_APPLICATION] [int] NOT NULL,
[SEQ_INTERVENANT] [int] NOT NULL,
[SEQ_SERVICE_RESPONSABLE] [int] NOT NULL,
[NB_DEMANDE_FLUX] [int] NULL,
[NB_DEMANDE_STOCK] [int] NULL,
[NB_DEMANDE_RESOLUE] [int] NULL,
[NB_DEMANDE_LIVREE] [int] NULL,
[NB_DEMANDE_MEP] [int] NULL,
[NB_DEMANDE_RESOL_NIV1] [int] NULL,
CONSTRAINT [PK_FAITS_DEMANDE] PRIMARY KEY CLUSTERED
[SEQ_JOUR] ASC,
[SEQ_DEMANDE] ASC,
[SEQ_DEMANDE_SCD] ASC,
[SEQ_APPLICATION] ASC,
[SEQ_INTERVENANT] ASC,
[SEQ_SERVICE_RESPONSABLE] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [AK_AK_FAITS_DEMANDE_FAITS_DE] UNIQUE NONCLUSTERED
[SEQ_JOUR] ASC,
[SEQ_DEMANDE] ASC,
[SEQ_DEMANDE_SCD] ASC,
[SEQ_APPLICATION] ASC,
[SEQ_INTERVENANT] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [ALIM_SID].[FAITS_DEMANDE] WITH CHECK ADD CONSTRAINT [FK_FAITS_DEMANDE_DIM_APPLICATION] FOREIGN KEY([SEQ_APPLICATION])
REFERENCES [ALIM_SID].[DIM_APPLICATION] ([SEQ_APPLICATION])
GO
ALTER TABLE [ALIM_SID].[FAITS_DEMANDE] CHECK CONSTRAINT [FK_FAITS_DEMANDE_DIM_APPLICATION]
GO
ALTER TABLE [ALIM_SID].[FAITS_DEMANDE] WITH CHECK ADD CONSTRAINT [FK_FAITS_DEMANDE_DIM_DEMANDE] FOREIGN KEY([SEQ_DEMANDE])
REFERENCES [ALIM_SID].[DIM_DEMANDE] ([SEQ_DEMANDE])
GO
ALTER TABLE [ALIM_SID].[FAITS_DEMANDE] CHECK CONSTRAINT [FK_FAITS_DEMANDE_DIM_DEMANDE]
GO
ALTER TABLE [ALIM_SID].[FAITS_DEMANDE] WITH CHECK ADD CONSTRAINT [FK_FAITS_DEMANDE_DIM_DEMANDE_SCD] FOREIGN KEY([SEQ_DEMANDE_SCD])
REFERENCES [ALIM_SID].[DIM_DEMANDE_SCD] ([SEQ_DEMANDE_SCD])
GO
ALTER TABLE [ALIM_SID].[FAITS_DEMANDE] CHECK CONSTRAINT [FK_FAITS_DEMANDE_DIM_DEMANDE_SCD]
GO
ALTER TABLE [ALIM_SID].[FAITS_DEMANDE] WITH CHECK ADD CONSTRAINT [FK_FAITS_DEMANDE_DIM_INTERVENANT] FOREIGN KEY([SEQ_INTERVENANT])
REFERENCES [ALIM_SID].[DIM_INTERVENANT] ([SEQ_INTERVENANT])
GO
ALTER TABLE [ALIM_SID].[FAITS_DEMANDE] CHECK CONSTRAINT [FK_FAITS_DEMANDE_DIM_INTERVENANT]
GO
ALTER TABLE [ALIM_SID].[FAITS_DEMANDE] WITH CHECK ADD CONSTRAINT [FK_FAITS_DEMANDE_DIM_JOUR] FOREIGN KEY([SEQ_JOUR])
REFERENCES [ALIM_SID].[DIM_JOUR] ([SEQ_JOUR])
GO
ALTER TABLE [ALIM_SID].[FAITS_DEMANDE] CHECK CONSTRAINT [FK_FAITS_DEMANDE_DIM_JOUR]
GO
ALTER TABLE [ALIM_SID].[FAITS_DEMANDE] WITH CHECK ADD CONSTRAINT [FK_FAITS_DEMANDE_DIM_SERVICE_RESPONSABLE] FOREIGN KEY([SEQ_SERVICE_RESPONSABLE])
REFERENCES [ALIM_SID].[DIM_SERVICE] ([SEQ_SERVICE])
GO
ALTER TABLE [ALIM_SID].[FAITS_DEMANDE] CHECK CONSTRAINT [FK_FAITS_DEMANDE_DIM_SERVICE_RESPONSABLE]
GO
-- not shown : extended properties
-- One of the dimension tables (they all have a primary key named SEQ_)
CREATE TABLE [ALIM_SID].[DIM_JOUR](
[SEQ_JOUR] [int] IDENTITY(1,1) NOT NULL,
[SEQ_ANNEE] [int] NOT NULL,
[SEQ_MOIS] [int] NOT NULL,
[DATE_JOUR] [date] NULL,
[CODE_ANNEE] [varchar](25) NULL,
[CODE_MOIS] [varchar](25) NULL,
[CODE_SEMAINE_ISO] [varchar](25) NULL,
[CODE_JOUR_ANNEE] [varchar](25) NULL,
[CODE_ANNEE_JOUR] [varchar](25) NULL,
[LIB_JOUR] [varchar](25) NULL,
[LIB_JOUR_COURT] [varchar](25) NULL,
[JOUR_OUVRE] [tinyint] NULL,
[JOUR_CHOME] [tinyint] NULL,
CONSTRAINT [PK_DIM_JOUR] PRIMARY KEY CLUSTERED
[SEQ_JOUR] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [ALIM_SID].[DIM_JOUR] WITH CHECK ADD CONSTRAINT [FK_DIM_JOUR_DIM_ANNEE] FOREIGN KEY([SEQ_ANNEE])
REFERENCES [ALIM_SID].[DIM_ANNEE] ([SEQ_ANNEE])
GO
ALTER TABLE [ALIM_SID].[DIM_JOUR] CHECK CONSTRAINT [FK_DIM_JOUR_DIM_ANNEE]
GO
ALTER TABLE [ALIM_SID].[DIM_JOUR] WITH CHECK ADD CONSTRAINT [FK_DIM_JOUR_DIM_MOIS] FOREIGN KEY([SEQ_MOIS])
REFERENCES [ALIM_SID].[DIM_MOIS] ([SEQ_MOIS])
GO
ALTER TABLE [ALIM_SID].[DIM_JOUR] CHECK CONSTRAINT [FK_DIM_JOUR_DIM_MOIS]
GO -
Why is the select Count too slow
I am doing the following select count and calling it from my JSP to get the total number of records... why is it so slow...
select count(*)
from
(select distinct o.receive_id, o.name, o.address
from order o, item i
where o.id = i.id
and o.status = 2 and i.status = 0)If the data in the table that you are referring to in the query gets refreshed very often and your high water mark on your table not reset, then this query always runs longer. While deleting data in the table, use 'TRUNCATE' rather than 'DELETE' in your data queries. that would help reset the high water mark and your count() queries will run very very fast.
-
Count distinct in case statement
SELECT A.P_ID,
B.P_NAME,
C.P_DESC,
SUM(CASE
WHEN A.DATE BETWEEN TRUNC(ADD_MONTHS(LAST_DAY(SYSDATE),-4) + 1) AND ADD_MONTHS(LAST_DAY(TO_DATE(SYSDATE)),-1)
AND A.M_ID IS NOT NULL
THEN 1
ELSE 0
END) AS COUNT,
SUM(CASE
WHEN A.DATE BETWEEN TRUNC(ADD_MONTHS(LAST_DAY(SYSDATE),-4) + 1) AND ADD_MONTHS(LAST_DAY(TO_DATE(SYSDATE)),-1)
AND A.M_ID IS NOT NULL
THEN COUNT(DISTINCT A.M_ID)
ELSE 0
END) AS UNIQUE_COUNT, /* Not possible */
SUM(CASE
WHEN A.DATE BETWEEN TRUNC(SYSDATE,'YEAR') AND ADD_MONTHS(LAST_DAY(TO_DATE(SYSDATE)),-1)
THEN A.AMT_1
ELSE 0
END) AS TOTAL_AMT_1,
SUM(CASE
WHEN A.DATE BETWEEN TRUNC(SYSDATE,'YEAR') AND ADD_MONTHS(LAST_DAY(TO_DATE(SYSDATE)),-1)
THEN A.AMT_2
ELSE 0
END) AS TOTAL_AMT_2
FROM TABLE_A A,
TABLE_B B,
TABLE_C C
WHERE A.P_ID = B.P_ID
AND B.PT_ID = C.PT_ID
GROUP BY A.P_ID,
B.P_NAME,
C.P_DESC
Hi,
This is a simplified version of my query.
I am trying to do 4 things here,
1. count A.M_ID
2. count distinct A.M_ID, this is where I have a problem.
3. and 4. Its just the sum from 2 diff columns.
Note that the dates for count and amt are different and I can't hard code them.
Can any one help me in the distinct count step?
This query is also running kinda slow.
So any suggestions, comments are very welcome.
Note: TABLE_A has 700 million recs, TABLE_B 4 million and TABLE_c is just 500 recs
Thanks!Taking advantage of the fact that most aggregate functions ignore nulls, you could do something like:
SELECT a.p_id, b.p_name, c.p_desc,
COUNT(CASE WHEN a.date BETWEEN TRUNC(ADD_MONTHS(LAST_DAY(sysdate),-4) + 1) AND
ADD_MONTHS(LAST_DAY(TO_DATE(sysdate)),-1) AND
a.m_id IS NOT NULL THEN m_id END) AS countall,
COUNT(DISTINCT CASE WHEN a.date BETWEEN TRUNC(ADD_MONTHS(LAST_DAY(sysdate),-4) + 1) AND
ADD_MONTHS(LAST_DAY(TO_DATE(sysdate)),-1) AND
a.m_id IS NOT NULL THEN a.m_id END) AS unique_count, /* entirely possible */
SUM(CASE WHEN a.date BETWEEN TRUNC(sysdate,'YEAR') AND
ADD_MONTHS(LAST_DAY(TO_DATE(sysdate)),-1) THEN a.amt_1
ELSE 0 END) AS total_amt_1,
SUM(CASE WHEN A.DATE BETWEEN TRUNC(sysdate,'YEAR') AND
ADD_MONTHS(LAST_DAY(TO_DATE(sysdate)),-1) THEN A.AMT_2
ELSE 0 END) AS TOTAL_AMT_2
FROM table_a a, table_b b, table_c c
WHERE a.p_id = b.p_id and
b.pt_id = c.pt_id
GROUP BY a.p_id, b.p_name, c.p_descThe two case statements inside the COUNT return either a.m_id or NULL. A simplified test case is:
SQL> WITH t as (
2 SELECT 1 m_id, 9 dt FROM dual UNION ALL
3 SELECT 1 m_id, 6 dt FROM dual UNION ALL
4 SELECT 2 m_id, 9 dt FROM dual UNION ALL
5 SELECT 2 m_id, 6 dt FROM dual UNION ALL
6 SELECT 1 m_id, 5 dt FROM dual UNION ALL
7 SELECT 2 m_id, 5 dt FROM dual UNION ALL
8 SELECT null m_id, 9 dt FROM dual)
9 SELECT count(CASE WHEN dt BETWEEN 6 and 9 THEN m_id end) cid,
10 count(distinct CASE WHEN dt BETWEEN 6 and 9 THEN m_id end) cdid
11 FROM t;
CID CDID
4 2I'm not entirely sure that you actually need the a.m_id IS NOT NULL predicate in the CASE statements, but I left it to be safe.
John -
SELECT COUNT( ) gives warning in ECC6.0.
Hi,
The following query is giving syntax error in ECC6.0, where as it worked absolutely fine in 3.1i server.
SELECT COUNT( OBOX_CNTNO ) FROM ZT2OB
INTO W_COUNT
WHERE SUPPL_CD = W_SUPPL_CD
AND INVC_NO = W_INVC_NO
AND INVC_LN_NO = T_SCR_ITEMS-INVC_LN_NO.
Error: "COUNT( OBOX_CNTNO )" is only valid in the extended form "COUNT( DIS
OBOX_CNTNO )" . . .
The query seems obsolete in ECC6.0. Can anybody help me to give valid query for this in ECC6.0?Hi,
<b>Count</b> functionality is to be used along with <b>Distinct</b> functionality.
COUNT( DISTINCT col )
<b>Reward points if this helps,</b>
Kiran -
Hi
Need help with getting a count of different sums:
Table: Stay
Columns: Name, Nights
each name can have multiple rows with different number of nights, now I have a select looking like this:
select sum(nights),name from stay
group by name
The result could look something like this:
1 smith
2 miller
1 grant
1 simpson
2 sharp
How do I do to get a count on the sum(night)? Is it possible in the same query?If you want to count how many distinct records appear in your current select statement (i.e. 5 in this case), you can do
SELECT count(*) num_distinct_guests,
total_nights,
name
FROM (
SELECT sum(nights) total_nights,
name
FROM stay
GROUP BY name
)If you want the total number of stays, in addition to the total time stayed, use something like
SELECT sum(nights),
name,
count(nights)
FROM stay
GROUP BY name;Justin
Maybe you are looking for
-
How do I share files between OS Macs on non-apple wireless network?
Hello: We have two iBook G4s, one with 10.4.4, and the other with 10.3.x. When they are connected via hard cable ethernet, they see each other and can share files. But when connected via wireless, they cannot see each other. What do we do? Thanks!
-
R/3 ISA B2B: Catalog Views
Hello experts in the group, I need to create catalog views for R/3 ISA B2B application. I have to use cross division 00 to handle multiple sales areas and I want to address these divisions using catalog views. I already have one of the four product c
-
External speakers not working on iphone
Hi, have a new iphone and the speakers do not work, at all! I can hear the ringtones etc with the earphones on but nothing without them. Have tried reset and plugging/unplugging the earphones but no luck at all, so frustrating!
-
Can't connect my bluetoot hands-free kit
Hello, I can't get my hands-free kit to work. I want to use it with skype or whatever, but when I try to connect it, i've got: [bluetooth]# connect 0C:E0:E4:3A:D6:ED Attempting to connect to 0C:E0:E4:3A:D6:ED [CHG] Device 0C:E0:E4:3A:D6:ED Connected:
-
Top-level navigation & workset title
Hello, I have created a workset with three pages. Is it possible to display only the title of the pages and not the title of the workset in the top-level navigation ? Thanks VC