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
-Learnsequel

san 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.

Similar Messages

  • 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

  • How to count distinct excluding a value in business layer?

    Hi all,
    I'm having a column which has many values. I need to make this is as a measure with count distinct aggregator. But i should not count 0 in the column. How can i do this. If i try to use any condition means the aggregator option is disables. Please help
    Thanks

    Look this example:
    I made in BMM in the SALES fact table measure:
    Count_Distinct_Prod_Id_Exclude_Prod_Id_144
    I'll count distinct PRODUCTS.PROD_ID, but exclude PROD_ID=144 in counting.
    Make this measure like this:
    1. New object/Logical column
    2. Go to data type tab and click EDIT on the logical table table source
    3. Now, in the general tab add join to a table (in my case PRODUCTS)
    4. Go to the column mapping tab -> show unmapped columns
    5. In the new column (in my case Count_Distinct_Prod_Id_Exclude_Prod_Id_144) write code like similar:
    CASE WHEN "orcl".""."SH"."PRODUCTS"."PROD_ID" = 144 THEN NULL ELSE "orcl".""."SH"."PRODUCTS"."PROD_ID" END
    6. Click OK and close the logical table source window
    7. Now, in the logical column window go to aggregation tab and choose COUNT DISTINCT.
    8. Move the measure Count_Distinct_Prod_Id_Exclude_Prod_Id_144 in the presentation area
    9. Test in Answers (report cointains columns as follow)
    PROD_CATEGORY_ID
    Count_Distinct_Prod_Id_Exclude_Prod_Id_144
    And the result in the NQQuery.log is:
    select T21473.PROD_CATEGORY_ID as c1,
    count(distinct case when T21473.PROD_ID = 144 then NULL else T21473.PROD_ID end ) as c2
    from
    PRODUCTS T21473
    group by T21473.PROD_CATEGORY_ID
    order by c1
    Regards
    Goran
    http://108obiee.blogspot.com

  • Logical Aggregate Column (count(distinct)) Does Not Group for SQL Server DB

    When utilizing the count(distinct column_name) aggregate function within a Logical Fact source in the Business Model and Mapping layer in the RPD file the output in BI Answers is not grouping correctly for SQL Server 2008 database sources only. All Oracle database sources represent the same aggregate column correctly within BI Answers.
    I am using OBIEE version 10.1.3.3.3
    Does anyone know how to resolve this issue?
    Thanks in advance,
    Kyle

    I thought that I would update my current findings with this issue. If you display the report in BI Answers as a Pivot Table view the aggregate column displays properly, it does not in a Table or Compound Layout view for some reason. I am still working with Oracle Support on this.

  • Count Distinct Wtih CASE Statement - Does not follow aggregation path

    All,
    I have a fact table, a day aggregate and a month aggregate. I have a time hierarchy and the month aggregate is set to the month level, the day aggregate is set to the day level within the time hierarchy.
    When using any measures and a field from my time dimension .. the appropriate aggregate is chosen, ie month & activity count .. month aggregate is used. Day & activity count .. day aggregate is used.
    However - when I use the count distinct aggregate rule .. the request always uses the lowest common denominator. The way I have found to get this to work is to use a logical table source override in the aggregation tab. Once I do this .. it does use the aggregates correctly.
    A few questions
    1. Is this the correct way to use aggregate navigation for the count distinct aggregation rule (using the source override option)? If yes, why is this necessary for count distinct .. what is special about it?
    2. The main problem I have now is that I need to create a simple count measure that has a CASE statement in it. The only way I see to do this is to select the Based on Dimensions checkbox which then allows me to add a CASE statement into my count distinct clause. But now the aggregation issue comes back into play and I can't do the logical table source override when the based on dimensions checkbox is checked .. so I am now stuck .. any help is appreciated.
    K

    Ok - I found a workaround (and maybe the preferred solution for my particular issue), which is - Using a CASE Statement with a COUNT DISTINCT aggregation and still havine AGGREGATE AWARENESS
    To get all three of the requirements above to work I had to do the following:
    - Create the COUNT DISTINCT as normal (counting on a USERID physically mapped column in my case)
    - Now I need to map my fact and aggregates to this column. This is where I got the case statement to work. Instead of trying to put the case statement inside of the Aggregate definition by using the checkbox 'Base on Dimension' (which didnt allow for aggregate awareness for some reason) .. I instead specified the case statement in the Column Mapping section of the Fact and Aggregate tables.
    - Once all the LTS's (facts and aggregates) are mapped .. you still have to define the Logical Table Source overrides in the aggregate tab of the count distinct definition. Add in all the fact and aggregates.
    Now the measure will use my month aggregate when i specify month, the day aggregate when i specify day, etc..
    If you are just trying to use a Count Distinct (no CASE satement needed) with Aggregate Awareness, you just need to use the Logical Table Source override on the aggregate tab.
    There is still a funky issue when using the COUNT aggregate type. As long as you dont map multiple logical table sources to the COUNT column it works fine and as expected. But, if you try to add in multiple sources and aggregate awareness it randomly starts SUMMING everything .. very weird. The blog in this thread says to check the 'Based on Dimension' checkbox to fix the problem but that did not work for me. Still not sure what to do on this one .. but its not currently causing me a problem so I will ignore for now ;)
    Thanks for all the help
    K

  • Count Distinct over a Window

    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

  • 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_APPLICATION

    Here 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

  • Query with COUNT DISTINCT

    Hello,
    We are in 10g ...
    I have to compute COUNT DISTINCT of customers, per month, and YearToDate.
    Per month, I think I found it out ...
    On the year to date ... I have no clue at all ... and I hope that you could provide me with a solution or advice...
    Here is my example :
    month cust
    200711 A
    200711 B
    200712 A
    200712 C
    200801 A
    200801 B
    200802 A
    200802 C
    200803 A
    200803 C
    200803 A
    200804 D
    I would like to get this :
    month cust_count cust_count_YTD
    200711......2................2 (because cust A and B)
    200712......2................3 (because cust A and C)
    200801......2................2 (Back to 0 at the beginning of each year)
    200802......2................3 (because cust A and C)
    200803......2................3 (because cust A and C, and A but count distinct)
    200804......1................4 (because D)
    Thank you in advance,
    Olivier

    Oh This is an interesting question.
    create table custTable(yyyymm,cust) as
    SELECT '200711','A' FROM dual UNION all
    SELECT '200711','B' FROM dual UNION all
    SELECT '200712','A' FROM dual UNION all
    SELECT '200712','C' FROM dual UNION all
    SELECT '200801','A' FROM dual UNION all
    SELECT '200801','B' FROM dual UNION all
    SELECT '200802','A' FROM dual UNION all
    SELECT '200802','C' FROM dual UNION all
    SELECT '200803','A' FROM dual UNION all
    SELECT '200803','C' FROM dual UNION all
    SELECT '200803','A' FROM dual UNION all
    SELECT '200804','D' FROM dual;
    select distinct yyyymm,cust_count,
    sum(WillSum) over(partition by substr(yyyymm,1,4) order by yyyymm) as cust_count_YTD
    from (select yyyymm,count(distinct cust) over(partition by yyyymm) as cust_count,
          case Row_Number() over(partition by substr(yyyymm,1,4),cust order by yyyymm)
          when 1 then 1 else 0 end as WillSum
            from custTable)
    order by yyyymm;or
    select yyyymm,count(distinct cust) as cust_count,
    sum(sum(WillSum)) over(partition by substr(yyyymm,1,4) order by yyyymm) as cust_count_YTD
    from (select yyyymm,cust,
          case Row_Number() over(partition by substr(yyyymm,1,4),cust order by yyyymm)
          when 1 then 1 else 0 end as WillSum
            from custTable)
    group by yyyymm
    order by yyyymm;
    YYYYMM  CUST_COUNT  CUST_COUNT_YTD
    200711           2               2
    200712           2               3
    200801           2               2
    200802           2               3
    200803           2               3
    200804           1               4similer threads
    Rolling unique person count by month over a time period
    [SQL] how can i get this result....??(accumulation distinct count)

  • Did I misuse 'DISTINCT' in this SQL statement?

    I have a table TI_ORDER only contains following data:
    O_ID E_CODE M_SEQ B_SEQ
    VARCHAR NUMBER NUMBER NUMBER
    CE013 1 1 1
    CE013 1 2 1
    CE013 1 3 1
    CE013 1 4 1
    CE013 1 5 1
    CE013 1 6 1
    CE013 1 6 2
    CE013 1 7 1
    CE013 1 8 1
    CE013 1 8 2
    CE013 1 9 1
    CE013 1 10 1
    CE013 1 10 2
    CE013 2 1 1
    CE013 2 2 1
    CE013 2 3 1
    CE013 2 4 1
    CE013 2 5 1
    CE013 2 6 1
    CE013 2 6 2
    CE013 2 7 1
    CE013 2 8 1
    CE013 2 8 2
    CE013 2 9 1
    CE013 2 10 1
    CE013 2 10 2
    If I execute this SQL:
    ==============================================
    SELECT a.o_id, a.e_code,
    COUNT(a.o_id) OVER (PARTITION BY a.o_id, a.e_code) AS cnt
    FROM ( SELECT DISTINCT o_id, e_code, m_seq FROM ti_order ) a
    WHERE a.o_id = 'CE013'
    ==============================================
    It will show:
    ==============================================
    CE013 1 10
    CE013 1 10
    CE013 1 10
    CE013 1 10
    CE013 1 10
    CE013 1 10
    CE013 1 10
    CE013 1 10
    CE013 1 10
    CE013 1 10
    CE013 2 10
    CE013 2 10
    CE013 2 10
    CE013 2 10
    CE013 2 10
    CE013 2 10
    CE013 2 10
    CE013 2 10
    CE013 2 10
    CE013 2 10
    =============================================
    If I add 'DISTINCT' to previous SQL statement:
    ============================================
    SELECT DISTINCT a.o_id, a.e_code,
    COUNT(a.o_id) OVER (PARTITION BY a.o_id, a.e_code) AS cnt
    FROM ( SELECT DISTINCT o_id, e_code, m_seq FROM ti_order ) a
    WHERE a.o_id = 'CE013'
    ============================================
    It displays:
    ============================================
    CE013 1 13
    CE013 2 13
    ============================================
    Why does it not show following output as I want ?
    ============================================
    CE013 1 10
    CE013 2 10
    ============================================

    Looks like you have stumbled across a bug. Below output indicates that 9i (example 2) gets the correct answer here while 8i (example 1) does not. You ARE on 8i right?
    In any case, there is no need to use analytic functions here, good old COUNT is fine to get the correct answer (example 3).
    --------------------------- example 1 -----------------------------
    Oracle8i Enterprise Edition Release 8.1.7.4.0 - Production
    With the Partitioning option
    JServer Release 8.1.7.4.0 - Production
    SQL> CREATE TABLE table_name (
    2 o_id VARCHAR2 (5),
    3 e_code NUMBER (2),
    4 m_seq NUMBER (2),
    5 b_seq NUMBER (2));
    Table created.
    SQL> INSERT INTO TABLE_NAME VALUES ('CE013','1','1','1');
    1 row created.
    SQL> INSERT INTO TABLE_NAME VALUES ('CE013','1','2','1');
    (snip)
    SQL> INSERT INTO TABLE_NAME VALUES ('CE013','2','10','2');
    1 row created.
    SQL> SELECT DISTINCT a.o_id, a.e_code,
    2 COUNT (a.o_id) OVER (PARTITION BY a.o_id, a.e_code) AS cnt
    3 FROM (SELECT DISTINCT o_id, e_code, m_seq
    4 FROM table_name) a
    5 WHERE a.o_id = 'CE013';
    O_ID E_CODE CNT
    CE013 1 13
    CE013 2 13
    SQL>
    --------------------------- example 2 -----------------------------
    Oracle9i Enterprise Edition Release 9.2.0.4.0 - 64bit Production
    With the Partitioning, OLAP and Oracle Data Mining options
    JServer Release 9.2.0.4.0 - Production
    SQL> CREATE TABLE table_name (
    2 o_id VARCHAR2 (5),
    3 e_code NUMBER (2),
    4 m_seq NUMBER (2),
    5 b_seq NUMBER (2));
    SQL> INSERT INTO TABLE_NAME VALUES ('CE013','1','1','1');
    1 row created.
    SQL> INSERT INTO TABLE_NAME VALUES ('CE013','1','2','1');
    1 row created.
    (snip)
    SQL> INSERT INTO TABLE_NAME VALUES ('CE013','2','10','2');
    1 row created.
    SQL> SELECT DISTINCT a.o_id, a.e_code,
    2 COUNT (a.o_id) OVER (PARTITION BY a.o_id, a.e_code) AS cnt
    3 FROM (SELECT DISTINCT o_id, e_code, m_seq
    4 FROM table_name) a
    5 WHERE a.o_id = 'CE013';
    O_ID E_CODE CNT
    CE013 1 10
    CE013 2 10
    SQL>
    --------------------------- example 3 -----------------------------
    Oracle8i Enterprise Edition Release 8.1.7.4.0 - Production
    With the Partitioning option
    JServer Release 8.1.7.4.0 - Production
    SQL> SELECT a.o_id, a.e_code, COUNT (*)
    2 FROM (SELECT DISTINCT o_id, e_code, m_seq
    3 FROM table_name) a
    4 WHERE a.o_id = 'CE013'
    5 GROUP BY a.o_id, a.e_code;
    O_ID E_CODE COUNT(*)
    CE013 1 10
    CE013 2 10
    SQL>
    Padders

  • Help needed in SQL performance - Using CASE in SQL statement versus 2 query

    Hi,
    I have a requirement to find count from a bunch of tables.
    The SQL I have gives the count of all members.
    I have created 2 queries to find count of active and inactive members.
    The key difference is only the active dates.
    Each query takes 20 seconds to execute.
    I modified the SQL to use CASE statement in the SELECT.
    So after the data is fetched the CASE statement will evaluate the active date and gives 2 counts (active and inactive)
    Is it advisable to use this approach. Will CASE improve SQL performance ? I have to justify this.
    Please let me know your thoughts.
    Thanks,
    J

    Hi,
    If it can be done in single SQL do it in single SQL.
    You said:
    Will CASE improve SQL performance There can be both cases to prove if the performance is better or worse.
    In your case you should tell us how it is.
    Regards,
    Bhushan

  • COUNT(DISTINCT) WITH ORDER BY in an analytic function

    -- I create a table with three fields: Name, Amount, and a Trans_Date.
    CREATE TABLE TEST
    NAME VARCHAR2(19) NULL,
    AMOUNT VARCHAR2(8) NULL,
    TRANS_DATE DATE NULL
    -- I insert a few rows into my table:
    INSERT INTO TEST ( TEST.NAME, TEST.AMOUNT, TEST.TRANS_DATE ) VALUES ( 'Anna', '110', TO_DATE('06/01/2005 08:00:00 PM', 'MM/DD/YYYY HH12:MI:SS PM') );
    INSERT INTO TEST ( TEST.NAME, TEST.AMOUNT, TEST.TRANS_DATE ) VALUES ( 'Anna', '20', TO_DATE('06/01/2005 08:00:00 PM', 'MM/DD/YYYY HH12:MI:SS PM') );
    INSERT INTO TEST ( TEST.NAME, TEST.AMOUNT, TEST.TRANS_DATE ) VALUES ( 'Anna', '110', TO_DATE('06/02/2005 08:00:00 PM', 'MM/DD/YYYY HH12:MI:SS PM') );
    INSERT INTO TEST ( TEST.NAME, TEST.AMOUNT, TEST.TRANS_DATE ) VALUES ( 'Anna', '21', TO_DATE('06/03/2005 08:00:00 PM', 'MM/DD/YYYY HH12:MI:SS PM') );
    INSERT INTO TEST ( TEST.NAME, TEST.AMOUNT, TEST.TRANS_DATE ) VALUES ( 'Anna', '68', TO_DATE('06/04/2005 08:00:00 PM', 'MM/DD/YYYY HH12:MI:SS PM') );
    INSERT INTO TEST ( TEST.NAME, TEST.AMOUNT, TEST.TRANS_DATE ) VALUES ( 'Anna', '110', TO_DATE('06/05/2005 08:00:00 PM', 'MM/DD/YYYY HH12:MI:SS PM') );
    INSERT INTO TEST ( TEST.NAME, TEST.AMOUNT, TEST.TRANS_DATE ) VALUES ( 'Anna', '20', TO_DATE('06/06/2005 08:00:00 PM', 'MM/DD/YYYY HH12:MI:SS PM') );
    INSERT INTO TEST ( TEST.NAME, TEST.AMOUNT, TEST.TRANS_DATE ) VALUES ( 'Bill', '43', TO_DATE('06/01/2005 08:00:00 PM', 'MM/DD/YYYY HH12:MI:SS PM') );
    INSERT INTO TEST ( TEST.NAME, TEST.AMOUNT, TEST.TRANS_DATE ) VALUES ( 'Bill', '77', TO_DATE('06/02/2005 08:00:00 PM', 'MM/DD/YYYY HH12:MI:SS PM') );
    INSERT INTO TEST ( TEST.NAME, TEST.AMOUNT, TEST.TRANS_DATE ) VALUES ( 'Bill', '221', TO_DATE('06/03/2005 08:00:00 PM', 'MM/DD/YYYY HH12:MI:SS PM') );
    INSERT INTO TEST ( TEST.NAME, TEST.AMOUNT, TEST.TRANS_DATE ) VALUES ( 'Bill', '43', TO_DATE('06/04/2005 08:00:00 PM', 'MM/DD/YYYY HH12:MI:SS PM') );
    INSERT INTO TEST ( TEST.NAME, TEST.AMOUNT, TEST.TRANS_DATE ) VALUES ( 'Bill', '73', TO_DATE('06/05/2005 08:00:00 PM', 'MM/DD/YYYY HH12:MI:SS PM') );
    commit;
    /* I want to retrieve all the distinct count of amount for every row in an analytic function with COUNT(DISTINCT AMOUNT) sorted by name and ordered by trans_date where I get only calculate for the last four trans_date for each row (i.e., for the row "Anna 110 6/5/2005 8:00:00.000 PM," I only want to look at the previous dates from 6/2/2005 to 6/5/2005 and get the distinct count of how many amounts there are different for Anna). Note, I cannot use the DISTINCT keyword in this query because it doesn't work with the ORDER BY */
    select NAME, AMOUNT, TRANS_DATE, COUNT(/*DISTINCT*/ AMOUNT) over ( partition by NAME
    order by TRANS_DATE range between numtodsinterval(3,'day') preceding and current row ) as COUNT_AMOUNT
    from TEST t;
    This is the results I get if I just count all the AMOUNT without using distinct:
    NAME     AMOUNT     TRANS_DATE     COUNT_AMOUNT
    Anna 110 6/1/2005 8:00:00.000 PM     2
    Anna 20 6/1/2005 8:00:00.000 PM     2
    Anna 110     6/2/2005 8:00:00.000 PM     3
    Anna 21     6/3/2005 8:00:00.000 PM     4
    Anna 68     6/4/2005 8:00:00.000 PM     5
    Anna 110     6/5/2005 8:00:00.000 PM     4
    Anna 20     6/6/2005 8:00:00.000 PM     4
    Bill 43     6/1/2005 8:00:00.000 PM     1
    Bill 77     6/2/2005 8:00:00.000 PM     2
    Bill 221     6/3/2005 8:00:00.000 PM     3
    Bill 43     6/4/2005 8:00:00.000 PM     4
    Bill 73     6/5/2005 8:00:00.000 PM     4
    The COUNT_DISTINCT_AMOUNT is the desired output:
    NAME     AMOUNT     TRANS_DATE     COUNT_DISTINCT_AMOUNT
    Anna     110     6/1/2005 8:00:00.000 PM     1
    Anna     20     6/1/2005 8:00:00.000 PM     2
    Anna     110     6/2/2005 8:00:00.000 PM     2
    Anna     21     6/3/2005 8:00:00.000 PM     3
    Anna     68     6/4/2005 8:00:00.000 PM     4
    Anna     110     6/5/2005 8:00:00.000 PM     3
    Anna     20     6/6/2005 8:00:00.000 PM     4
    Bill     43     6/1/2005 8:00:00.000 PM     1
    Bill     77     6/2/2005 8:00:00.000 PM     2
    Bill     221     6/3/2005 8:00:00.000 PM     3
    Bill     43     6/4/2005 8:00:00.000 PM     3
    Bill     73     6/5/2005 8:00:00.000 PM     4
    Thanks in advance.

    you can try to write your own udag.
    here is a fake example, just to show how it "could" work. I am here using only 1,2,4,8,16,32 as potential values.
    create or replace type CountDistinctType as object
       bitor_number number,
       static function ODCIAggregateInitialize(sctx IN OUT CountDistinctType) 
         return number,
       member function ODCIAggregateIterate(self IN OUT CountDistinctType, 
         value IN number) return number,
       member function ODCIAggregateTerminate(self IN CountDistinctType, 
         returnValue OUT number, flags IN number) return number,
        member function ODCIAggregateMerge(self IN OUT CountDistinctType,
          ctx2 IN CountDistinctType) return number
    create or replace type body CountDistinctType is 
    static function ODCIAggregateInitialize(sctx IN OUT CountDistinctType) 
    return number is 
    begin
       sctx := CountDistinctType('');
       return ODCIConst.Success;
    end;
    member function ODCIAggregateIterate(self IN OUT CountDistinctType, value IN number)
      return number is
      begin
        if (self.bitor_number is null) then
          self.bitor_number := value;
        else
          self.bitor_number := self.bitor_number+value-bitand(self.bitor_number,value);
        end if;
        return ODCIConst.Success;
      end;
      member function ODCIAggregateTerminate(self IN CountDistinctType, returnValue OUT
      number, flags IN number) return number is
      begin
        returnValue := 0;
        for i in 0..log(2,self.bitor_number) loop
          if (bitand(power(2,i),self.bitor_number)!=0) then
            returnValue := returnValue+1;
          end if;
        end loop;
        return ODCIConst.Success;
      end;
      member function ODCIAggregateMerge(self IN OUT CountDistinctType, ctx2 IN
      CountDistinctType) return number is
      begin
        return ODCIConst.Success;
      end;
      end;
    CREATE or REPLACE FUNCTION CountDistinct (n number) RETURN number 
    PARALLEL_ENABLE AGGREGATE USING CountDistinctType;
    drop table t;
    create table t as select rownum r, power(2,trunc(dbms_random.value(0,6))) p from all_objects;
    SQL> select r,p,countdistinct(p) over (order by r) d from t where rownum<10 order by r;
             R          P          D
             1          4          1
             2          1          2
             3          8          3
             4         32          4
             5          1          4
             6         16          5
             7         16          5
             8          4          5
             9          4          5buy some good book if you want to start at writting your own "distinct" algorythm.
    Message was edited by:
    Laurent Schneider
    a simpler but memory killer algorithm would use a plsql table in an udag and do the count(distinct) over that table to return the value

  • "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 AM

    Hi 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

  • Set Aggregation type of Count Distinct to use correct table aggregation in

    Hi there,
    Currently I use OBIEE 10.1.3.4.1 , and there is a case where a fact table consist of 2 logical table source: detail and aggregate table, which has some measure using count distinct as aggregation type. The problem is everytime I browse the measure with no dimension at all , it always use detail table not aggegation one..
    Really appreciate for any suggestion ..
    thanks a lot

    Hi,
    I don't think it's the same case as mine. Let say I have 2 table : detail and aggegate
    Detail Table consists 4 fields:
    *) Period
    *) Market
    *) Region
    *) Measure : Customer ID, Sales
    Aggregate Table consists 3 fields :
    *) Period
    *) Region
    *) Measure : Customer ID, Sales
    in the measure I set aggregation type for each field:
    *) Sales >> set as Sum
    *) Customer ID >> copy as "Number of Customer" and set as Count Distinct
    In each LTS' contents I set the level of aggregation using "Get Levels" feature..
    Then I try to browse via Presentation and do some querys belows:
    a) only choose single field of measure : Sales, the session shows that the value is taken from aggregation table and just as I expected.
    b) choose period and sales, the session shows that the values are taken from aggregation table, and still just as I expected.
    c) choose period, sales , and market, the session shows that the values are taken from detail table, just as I expected.
    d) only choose single field of measure : "Number of Customer", the session shows that the value is taken from detail table , this is NOT as I expected. It suppose to take the value from aggregation table..
    e) choose period and "Number of Customer", the session shows that the value is taken from detail table , this is also NOT as I expected. It suppose to take the value from aggregation table..
    I've tried to override the aggregation , but still confuse how to apply in measure "Number of Customer" and did not work at all..
    any idea ?
    thanks a lot

  • Count distinct derived measure on SCD type 2 dimension

    Hi,
    I have 2 dimension tables with SCD type 2 and one fact table :
    DIM1 :
    DIM1_SURR_KEY
    DIM1_NAT_KEY
    DIM1_PROPERTY1
    DIM1_PROPERTY2
    EFFECTIVE_DATE
    EXPIRATION_DATE
    DIM2 :
    DIM2_SURR_KEY
    DIM2_NAT_KEY
    DIM2_PROPERTY1
    DIM2_PROPERTY2
    EFFECTIVE_DATE
    EXPIRATION_DATE
    FACT :
    DIM1_SURR_KEY
    DIM2_SURR_KEY
    MEA1
    MEA2
    Dimension and fact tables are joined with : DIM1_SURR_KEY and DIM2_SURR_KEY.
    In my business layer fact table, I would like to define this derived measure : count distinct of DIM1_NAT_KEY.
    I tried to add new source for the fact table. I also tried an alias of DIM1 in physical layer.
    Nothing works as I want : In Answers, if I select the fact and the count distinct, it works. Even if I select property of DIM1. But if I select property of DIM2, my count distinct return 0 (in SQL sent to Oracle DB, the formula is replaced with NULL).
    Is it possible (and how) to count the number of Nat_Key with a derived measure defined in business layer ?
    If not, I’ll define materialized view on fact table with natural key and dimension ID.
    My first goal is to avoid end user to redefine derived column in Answers for each reports.
    Thanks for your help

    Hi,
    my advise is to map the DIM1_NAT_KEY iside the Fact Table of the Business Model, so you have a new Logical Table Source inside the Logical Fact Table that maps the DIM1_NAT_KEY as a measure. Define the Level for this Logical Table Source and set the COUNT DISTINC aggregation. In this way OBIEE knows that that measure is inside a fact an it treat like that.
    I hope it helps.
    Regards,
    Gianluca

  • Group by count distinct

    mytable
    id | yy
    1 | 78
    2 | 78
    3 | 78
    3 | 79
    3 | 79
    4 | 79
    5 | 79
    5 | 80
    Desired output:
    yy | id_count
    78 | 3
    79 | 2
    80 | 0
    Following query doesn't work, as it doesn't take into account that id was already counted
    select yy, count(distinct id) as id_count
    from mytable
    group by yy
    --output
    yy | id_count
    78 | 3
    79 | 4
    80 | 1
    Hope this makes sense.
    Ideas?

    Hi,
    You only want to count each id once, with the first (that is, lowest) yy: is that right?
    Here's one way:
    WITH     got_r_num    AS
         SELECT  id
         ,     yy
         ,     ROW_NUMBER () OVER ( PARTITION BY  id
                                   ORDER BY          yy
                           ) AS r_num
         FROM    my_table
    SELECT       yy
           COUNT ( CASE
                      WHEN  r_num = 1
                    THEN  id
                  END
              )     AS id_cnt
    FROM       got_r_num
    GROUP BY  yy
    ORDER BY  yy
    ;Doing anything for the first of each id is probably a job for "ROW_NUMBER () OVER (PARTITION BY id ...)".

Maybe you are looking for

  • How do you import photos from an ipod to iphoto?

    How do you import photos from an ipod to iphoto? I can't sync with itunes because it will wipe the photos off the ipod?

  • Possible bug with Triggers tab

    Hi there, I'm using 1.0.0.12.15 and think there is a bug with the triggers tab. When I navigate to my table (which has one trigger on it), and then click on the Triggers tab, the trigger shows up multiple times. It's probably not a coincidence that i

  • Issue in adapters mapping in OIM 11g

    Hi, I am having the issue in adapters mapping in OIM 11g..... I had created an Entity Adapter using utility task in OIM... and I had attached it to post update of users form in data object manager.... But when I am trying to map the adapter variables

  • Boot camp or Parallels with Late 2008 MBP 2.8

    Does anyone have any experience yet? I need to use Adobe CS3, Wordperfect X3, and Autocad 2009. I still wanted the new MBP so I ordered it and it will be here next week. I will be running XP Pro. Which is the more useful platform Parallels or BC.

  • Business Content Source System Assignment???

    Hi Gurus, i am installing Business content and after going thru few posts in this Forums i have been hearing about Source system assignment in the Business content screen. but when ever i installed i never really used this option and we have only one