Count distinct aggregation in CWM2

Hi,
Is there any way to make a count distinct aggregation with a CWM2 model?
I mean, for the documentation example (geography, product, channel and time dimensions), I would like to show in a graph the sum of the sales measure and the number of different products, for the different filter selections made.
E.g.:
Page items: geography dim, time dim.
Groups: Measures (sales measure, ¿number of different products?)
Series: channel dim.
I know that the unique possible aggregation in CWM2 model is SUM, so I think it can't be done with an aggregation. Can anyone suggest me a workaround to do this? Is it possible within an analytical workspace?
Thanks in advanced...

Hi Nagarajan,
1. have to count distinct records from one internal ta
  first of all u have to decide
  which FIELD COMBINATION makes a record unique.
2. After that u can use the abap syntax
   delete adjacent duplicates (see documention/help)
    (before this make sure to SORT the internal table
    in the same sequence of FIELD COMBINATION)
3. then u can use
   describe table itab.
4. Before doing step 2,
   u can copy the whole internal table to another internal table
  and do your logic on the second internal table
  so that the original is not lost.
  u can copylike this.
   ITAB2[]   =  ITAB1[].
Hope it helps.
Regards,
Amit M.
Message was edited by: Amit Mittal

Similar Messages

  • 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&Count Distinct

    Hi Gurus,
    In one of OBIEE blogs I studied "Aggregation should only be performed on measure columns, with the possible exception of the aggregation COUNT and Count Distinct"....my Question is what happens if we apply count&count distinct aggregation rules to measures because we are using that in our project,what are problems we will face?

    User wrote:
    you mean it executes a sub query internally to count and later it sum the result..correct.- You can ship the total calculations to the underlying RDBMS if 'Report Based Total' is not ticked, otherwise if it is, the BI Server computes the subtotal.
    and in your answers "No" mean results coming by putting sum in criteria are different ..right- Yes thats what I mean, sorry if unclear - Are your totals correct now? Failing that try 'server complex aggregate' but this is usually only requried if your deriving calculations in Answers (ie % of total etc).

  • 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

  • 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

  • OBIEE 10G count distinct problem

    Hi,
    I am really new to OBI now runs into this problem.
    I have a fact and three dimension tables as follows:
    fact:
    1. sales:
    sold_vlaue (sum)
    transactions (count distinct receipt_id)
    branch_id (foreign key)
    daykey (foreign key)
    receipt_id (foreign key)
    product_key (foreign key)
    dimensions
    1. branch
    branch_id (key)
    2. time
    daykey (key)
    3. product
    product_key (key)
    These tables are joined as star schema by keys mentioned above. sales.sold_value is aggregated by 'sum', transactions is by (count distinct receipt_id). I don't have a dimension for receipt_id since it's only for the calculation of transaction.
    So how can I set up to make the transactions correct (count distinct receipt_id)?
    I tried to set transactions as count distinct in Default aggregation rule. But the result is wrong (all 1)

    All right. I figured it out.
    The fact table should be modelled as:
    1. sales:
    physical layer:
    sold_vlaue
    branch_id (foreign key)
    daykey (foreign key)
    receipt_id (foreign key)
    product_key (foreign key)
    The underlying query is:
    select
    branch_id, daykey, receipt_id, product_key
    , sum(sold_value)
    from table
    group by
    branch_id, daykey, receipt_id, product_key
    BMM layer:
    sold_value (sum)
    transactions (count distinct receipt_id)
    branch_id (foreign key)
    daykey (foreign key)
    receipt_id (foreign key) (removed)
    product_key (foreign key)

  • OLAP Analysis Count Distinct?

    If this query is better suited to the OLAP forum, please let me know.
    I am creating an Enrollment cube that has a dimension of Student with a Student_ID attribute. The fact table contains a measure column called Students, with each record having a value of 1. This results in getting a total SUM of students for a specific semester in an analysis in BI. However, this SUM aggregation does not distinctly identify students, resulting in a student that attends 4 semesters being counted as 4 students for the entire academic year. Adding COUNT(DISTINCT Student.Student_ID) to the analysis worked with an earlier test cube that I had created, but when I try to perform it on my updated cube it will only give me a COUNT(DISTINCT) for All Time, even when looking at the Semester or Academic Year levels. The only appreciable difference in my updated cube is that it has more dimensions.

    Yes, you can post your query on the OLAP forum because this forum is on Oracle BI Applications (pre packages applications using OBEE + DAC + Informatica).
    Regards,
    Benoit

  • 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

  • Grand Total with Count Distinct

    Hi,
    I want to use grand total to count distinct my data,
    I had physical map ActivityId into my fact and set count distinct to its aggregation rules.
    then when i create my report the anwser is OK.(count distinct ActivityID). But in grandtotal is sum.
    The aggregation Rules in BI Awnser is set it Default.
    [Pic >> http://image.ohozaa.com/i/866/tablecountdisctinct.jpg ]
    i don't know why , i try to set many thing but it isn't work
    Thank ,Regard.
    And sorry about my English Language.

    couple of things required here..
    how did you build this measure in rpd?
    Is that physical mapped or logical mapped column(derived column)?
    Map it as physically and apply aggregation rule on top of it to: CountDistinct
    Now, pull the column in front-end it has to give you proper sub-total/total..
    Or, Try putting the formula in front-end in fx of the column and try..

  • Count distinct on a varchar field

    Hi,
    i want to count the unique values for a field, but when i try this in a aggregator i'm getting the following error:
    aggrete function COUNT expected a numeric type.
    This is the query i want to reproduce in a mapping to update a tablefield.
    select material, material_grid, count (distinct season)
    from material_season
    group by material, material_grid
    having count (distinct season)>1
    Why isn't this possible in a aggregator?
    regards,
    Osman

    Hi Osman
    When do you get the error? Which version of OWB are you using? What SQL do you get generated - look at the intermediate code gen within the mapping.
    Cheers
    David

  • COUNT DISTINCT in SAP Hana

    I am not getting the results that what i am looking for when i try to use COUNT DISTINCT function. I did able to write the Calc View using the COUNT (DISTINCT <column_name1> || <column_name2>) but when i tried to look at the explorer it doesn't give me the right results. On the other side, the same system i have in MSAS 2000 and MSAS 2008, they are giving the right results while porting the same query in CalcView for SAP Hana, the results doesn't tally. Any suggestions or experience to share. Look at the side below;
    http://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/401eb46e-d407-2f10-c999-e467d93eae50?QuickLink=index&overridelayout=true&53244709744350
    Other Examples
    u2013 Exception Aggregation (e.g. Distinct Count only available as BWA Calculation Engine feature)
    http://www.saptechno.com/sap-notes.html?view=sapnote&id=1631919

    I believe you are more likely to get a response in the [In-Memory Business Data Management|SAP HANA and In-Memory Computing; forum.
    I'd move your post there, but I don't have the rights for this.
    Please remeber to close this thread.
    Thank you for your understanding,
    - Ludek

  • How to display the count distinct in a report

    hi,
    i have a report with multiple columns in it and with column, say A; i need to display in a calculated column B how many distinct values there are in A across the entire report; how to do that?

    Hi.
    For example:
    CALENDAR_YEAR
    CALENDAR_MONTH_DESC
    count(distinct TIMES.CALENDAR_MONTH_DESC by TIMES.CALENDAR_YEAR)
    Count will give you how many distinct months are in year.
    Regards
    Goran
    http://108obiee.blogspot.com

  • 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

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

Maybe you are looking for

  • ITunes has encountered a problem and needs to close when adding movie files

    So.. I haven't done anything recently with installing any new versions of itunes or anything of the such. Just one day whenever I tried to add .mp4 files (videos) on my ipod using Itunes, it crashes with the error message "iTunes has encountered a pr

  • Itunes wont load onto ipod

    i have an itunes account and used a gift card to load songs.. when i 'buy a song" it shows that the song was purchased but it won't load it into my ipod..help

  • XL Report in SAP 2007

    Sir, I am getting problem in xl report which is inbuild in SAP 2007 the message i am getting is as follow " Class does not support Automation or does not support expected interface " Kindly guide me Thanks Rajesh B K

  • Internet Explorer 7 and Zen Media Explo

    Does the final version of Internet Explorer 7 still break Zen Media Explorer? There were posts saying that the Beta version broke it. I keep telling auto update to not install IE7, because I never use IE for web surfing. Thanks. Does anyone have any

  • Shut down Windows 8.1 embedded from within an app

    I am developing a windows 8.1 app with microsoft visual studio. The app shall run in an embedded box PC. From within this app I want to be able to completely shut down the computer. Is there some simple way of simulating or acessing windows/shut down