Distinct count differnt from variable count on group
I have a date field and a number field. I have grouped the date by week and by month. I have created a DistinctCount summary on number field and placed in each group. I also created a variable formula which gets reset in each group header. I get differnt results on the monthly group footer.
monthly group header formula:
WhilePrintingRecords;
Shared NumberVar Job_Count_Monthly;
Job_Count_Monthly := 0
weekly group footer formula:
WhilePrintingRecords;
Shared NumberVar Job_Count_Monthly;
Job_Count_Monthly := Job_Count_Monthly + 1
monthly group footer formula:
WhilePrintingRecords;
Shared NumberVar Job_Count_Monthly;
Job_Count_Monthly
Results:
11/30/08 102
12/7/08 110
12/14/08 152
12/21/08 79
12/28/08 89
December 08 DistinctCount returns 491 Formulas returns 532
Can someone please explain why and which is correct?
Thanks Vinay.
I resumed all my details and exported to excel to do a count. I found that a least one of my numbers were in two different weekly groups. Now I don't understand why this is because I am grouped by the number. My date group should be the second criteria. My date is from a formula and I wanted it to group by Max Date however it appears the group does not do that. I previously tried to use sub-reports to return number & desired date but I could not figure out how to create groups from sub-report variable to display weekly,monthly & yearly totals. Does anyone have any ideas?
Thanks
Wayne
Similar Messages
-
Use Variance for Distinct Count of Group Results
Post Author: Judith
CA Forum: Crystal Reports
Hi there,
I am new to CR. I am using CR 2008. I am stuck (every two minutes) and it would be great, if you could help me with this one:
I have a list of people who are talking to each other:A to B, B to A, C to D, D to A etc. Then I wanted to see, who has most friends and I have created Groups to have a Distinct Count on how many people are talking to A, and to B, and to C etc. that worked fine. What I would like to do is to find out is who of these has most people they are talking to. Or even better, what is the variance of the resulting subtotals. Simply using Variance or Maximum doesn't seem to work on the DistinctCount Summary.
I would very much appreciate any help on this.
JudithPost Author: Jagan
CA Forum: Crystal Reports
I understand the issue, I don't understand why you think DistinctCount at two different levels should total up. Consider this sample data:Facility, EmployeeA, 1A, 2B, 1B, 3
Facility A's distinct count => 2Facility B's distinct count => 2Report's distinct count => 3
Use DistinctCount() at the group level and create a formula to sum these counts yourself and print that in the report footer. -
Distinct count of Group header
Hi Experts ,
I have a crystal report where i have grouped on Business area and group header carries YTD Actuals Vs Target
and in detail section it carries in different regions information ofthe business area.
Now my concern is i need to count distinct business area in report header based YTD Actuals <= or > Target.
Any Ideas!!!
Thanks,
KrishnaI am not sure if I am understanding what you are asking correctly, but here is my take on it.
You have a group that is identified as "Business Area"
You have done a calculation to determine the YTD Actual for each Business Area which you have put into the Group footer or header
Now you want at the report level a count of the business areas that have YTD actuals <= the Target, and a count of the business areas that have YTD actuals > the Target.
If this is correct, could you use a running total? In your field explorer pain, right click on Running Total and select New to open up the running total editor. In the summary section, select field to summarize on the field you used to define a business area; type of summary is "distinct count'; in the evaluate section, set up a formula to look for the YTD Actual <= to whatever the target was supposed to be; in the reset section, select never.
If this is not what you are trying to do, could you please reword your question? For example, are you trying to reshow each business areas YTD totals compared to the target in your report header instead of a count? -
Running Total & Distinct Count Query
Crystal 10.0.0.533 CR Professional
Hope you can assist.
I have a report listing deals signed, each deal has a corresponding category i.e. industry type, Accountant, Chiropractor, Financial Services, etc.
I am undertaking a Distinct Count on these categories so that in the group footer it is showing number of different categories signed in a period (the report is grouped into different date periods). This is working OK.
However, I need to EXCLUDE the category from this distinct count if the deal value is zero.
I have tried using a formula to show a blank field if the value is zero, however it appears to be distinctly counting the blank as a category?
Any assistance would be much appreciated.
TracyDeffinetly it will be less than what you expect for grand total. It is because when you are calculating distinct count for each group suppose
Group A has categories A,B,C,D-->distinct count (4)
Group B has C,D,E,F-->distinct count (4)
but while calculating grand total then the distinct count will be
A,B,C,D,E,F --> grand total (6) but not (8).
In this case you need to use mannual running total like this
whileprintingrecords;
numbervar i;
i:=i+{running total};
place this in group footer and create another fomula like this
whileprintingrecords;
numbervar i;
place this in report footer to get the correct grand total.
Regards,
Raghavendra -
Distinct count inside a measure group with other measures
Hello,
I have 1 distinct count inside a measure group with other measures, sum, count etc. I know this is not recommended due to poor processing performance and query response time.
Processing performance I can live with if it means not having another measure group, which increases processing time anyway.
I have used the recommended approach before and it generated many questions about what this second measure group is for (visible via excel), even though I made the distinct count appear in the main measure group via a calculated measure.
(it would be nice if you could hide measure groups)
However my question is: is query response time only effected when the distinct count is used in the query? Or is query response time effected regardless if the distinct count is used or not??
Below is an extract from the 2005 distinct count optimizer white paper. It’s not completely clear but I assume if effects queries regardless if distinct count is used or not?
"By adding other measures to the measure group holding a distinct count measure, all of the other measures will be at the same granularity as the distinct count measure, resulting in inefficient data structures and suboptimal
queries."You might also be interested in reading this blog post, which deals with a similar scenario, to get a feeling for some of the things that might be going on behind the scenes:
http://cwebbbi.wordpress.com/2012/11/27/storage-engine-caching-measures-and-measure-groups/
Chris
Check out my MS BI blog I also do
SSAS, PowerPivot, MDX and DAX consultancy
and run public SQL Server and BI training courses in the UK -
Getting DISTINCT count from two different columns
Hi all,
I have following query which gives currency code from two different tables. I would like to get the distinct count of currency codes from these two different columns.
SELECT eb.person_seq_id, eb.bonus_amount, eb.currency_cd, ed.currency_cd_host
FROM fr_emp_bonuses eb, fr_emp_details ed, fr_periods p
WHERE eb.person_seq_id = ed.person_seq_id AND ed.period_seq_id = eb.period_seq_id
AND ed.period_seq_id = p.period_seq_id AND p.period_status = 'CURRENT'
AND eb.bonus_amount >= 0 AND eb.person_seq_id = 3525125;
This query gives following result
3525125 240000 USD INR
3525125 0 USD INR
3525125 60000 USD INR
3525125 50000 USD INR
There are two distinct currency codes (USD, INR) and total amount is 350000. So I am looking for a query to give me the following result
3525125 350000 2
Thanks in advanceHi,
Here's one way:
WITH original_query AS
SELECT eb.person_seq_id
, eb.bonus_amount
, eb.currency_cd
, ed.currency_cd_host
FROM fr_emp_bonuses eb
, fr_emp_details ed
, fr_periods p
WHERE eb.person_seq_id = ed.person_seq_id
AND ed.period_seq_id = eb.period_seq_id
AND ed.period_seq_id = p.period_seq_id
AND p.period_status = 'CURRENT'
AND eb.bonus_amount >= 0
AND eb.person_seq_id = 3525125
, unpivoted_data AS
SELECT person_seq_id
, bonus_amount
, currency_cd
FROM original_query
UNION ALL
SELECT person_seq_id
, 0 AS bonus_amount
, currency_cd_host AS currency_cd
FROM original_query
SELECT person_seq_id
, SUM (bonus_amount) AS total_bonus_amount
, COUNT (DISTINCT currency_cd) AS distinct_currency_cds
FROM unpivoted_data
GROUP BY person_seq_id
;There may be a shorter, more efficient way to get the same results, but without knowing more about your tables, I can't tell.
The tricky thing is getting two columns (currency_cd and currencuy_cd_host in this case) counted together. You can't simply say
COUNT (DISTINCT eb.currency_cd) +
COUNT (DISTINCT ed.currency_code_host)That happens to get the correct result with the sample data you posted, but what if you had data like thEe following?
currency_cd currency_cd_host
INR USD
USD INRHere, the count of distinct currency_cds is 2, and the count of distinct currency_cd_hsots is also 2. Does that mean the grand total is 2 + 2 = 4? No, the 2 codes in one column arte the same 2 codes as in the other column. We need to get both currency_cd and currency_cd_hsot into the same column, and then do COUNT (DISTINCT ...) on that combined column. A UNION, as shown above, will certainly do that, starting with your query as you posted it. The query you posted isn't necessarily the best frist step towards this result, however, so there may be a much better approach, depending on your tables.
Edited by: Frank Kulash on Feb 1, 2012 6:21 PM
Here's a slightly shorter, and probably more efficient way to get the same results:
WITH cntr AS
SELECT LEVEL AS n
FROM dual
CONNECT BY LEVEL <= 2
SELECT eb.person_seq_id
, SUM (eb.bonus_amount) AS total-amount
, COUNT ( DISTINCT CASE
WHEN c.n = 1
THEN eb.currency_cd
ELSE ed.currency_cd_host
END
) AS distinct_currency_cds
FROM fr_emp_bonuses eb
, fr_emp_details ed
, fr_periods p
, cntr c
WHERE eb.person_seq_id = ed.person_seq_id
AND ed.period_seq_id = eb.period_seq_id
AND ed.period_seq_id = p.period_seq_id
AND p.period_status = 'CURRENT'
AND eb.bonus_amount >= 0
AND eb.person_seq_i = 3525125
-- NOTE: no join condition involving c; we really do want a cross-join
GROUP BY eb.person_seq_id
; -
Specified Order Grouping does not show if Distinct Count is Zero
Post Author: Hieu
CA Forum: General
Hello,
I'm using Crystal Reports XI R2 with SQL data source. I have a cross-tab report with grouping in specified order. It's a report of applicants applying to a college. The grouping is of various majors (degrees). The report summarizes (distinct counts) the number of applicants for the groups of majors. The problem is that if the count for a group of major is zero, then that Named Group does not appear at all in the cross-tab report. I want the Name Group to appear with the count of "0".
I notice this same phenomenon with specified order grouping anywhere and not just in a cross-tab. I have tried changing "convert database/other null values to default" but nothing working yet.
Any help will be most appreciated. Thanks.Post Author: synapsevampire
CA Forum: General
It's not a phenomenon, it's how databases and SQL works.
You didn't get any rows back for those with a zero distinct count (otherwise the count would be 1 or more, right?), so Crystal doesn't show any data for those groups.
So to display a zero for those that do not exist would reuire either advanced SQL, or manual summaries.
One method for manual summaries is to use Running Totals. Select distinct count of the applicants and group by the majors, then in the evaluate use a formula place:
{table.majors} = "Blah 1"
Creating a seperate Running Total for every group and replacing "Blah 1" with the various majors.
-kai -
Distinct count of GRN's in Query PLD
hi all,
We have developed a daily grn report and designed report
using Query PLD.
Report contains
(grn no,date,vendor name,vendor ref no,item code,item
description,quanity & line total)
We want to display no of grn's(distinct count)
@ repetitive area footer.
Is there any function available in formula field to display
distinct count of no. of grn's ? / It should done thru query.
Thanks,
with regards,
A.Jeyakanthanit will be hard from PLD, instead u can have ur query like,
SELECT 'Details', ' ',T0.Docnum, T0.CardName, .... FROM OPDN T0 WHERE Month( T0.DocDate ) = Month ('{%0]')
Union
SELECT 'Counts', count(T0.DocNum) as 'Doc(s) No', ' ', ' ', ... FROM OPDN T0 WHERE Month( T0.DocDate ) = Month ('{%0]')
(Replace { with [ )
Note ' ' is a gap in order to fill the other fields. in first query have given tht ' ' gap purposely so tht the display will not affect ur designing part
union is used b'coz Count() fn is like aggregate, so wont allow further individual fields coming along with it.
the 2nd query will occupy only one row, which by grouping u can separate and can hide from repetitive and take the value via formula field by writing that field's id to it into Footer Area.
Regards,
Dhana.
Edited by: Dhanalakshmi C on Mar 5, 2008 1:31 PM -
How to get Distinct Count of Products across two dimensions
Hi,
I have two dimensions, Item and Presentations. I need to get distinct count of products for IMD_Id + Merc_Pres_Id. IMD_Id is the lowest member in Item and Merc_Pres_Id is lowest in Presentation. My MDX query is given below but when I apply filters to
slice it, it does not work and does not give right count. It always gives the count for all.
/* Last Year Demand - Demand for 12 months back of selected months */
With
Member [Measures].[LYDemand]
as
Sum(
Generate(
EXISTING[All Date].[Fiscal Month Name].[Fiscal Month Name].Members,
{parallelperiod([All Date].[Fiscal Month Name].[Fiscal Month Name], 12
,[All Date].[Fiscal Month Name].CurrentMember)}
,[Measures].[Proj Demand]
/* Last to last Year Demand - Demand for 24 back of selected months */
Member [Measures].[LLYDemand]
as
Sum(
Generate(
EXISTING[All Date].[Fiscal Month Name].[Fiscal Month Name].Members,
{parallelperiod([All Date].[Fiscal Month Name].[Fiscal Month Name], 24
,[All Date].[Fiscal Month Name].CurrentMember)}
,[Measures].[Proj Demand]
/* Current Year Active Products */
Member [Measures].[CYCount]
as
CASE
WHEN
[Measures].[Proj Demand] > 0
THEN
DistinctCount(Existing(([All Items].[IMD Id].[IMD Id],[All Merchandise Presentations].[Merch
Pres Key].[Merch Pres Key])))
ELSE
NULL
END
/* Last year Active Products */
Member [Measures].[LYCount]
as
CASE
WHEN
[Measures].[LYDemand] > 0
THEN
DistinctCount(([All Items].[IMD Id].[IMD Id],[All Merchandise Presentations].[Merch Pres Key].[Merch Pres
Key], [All Items].[Style Name].CurrentMember, (StrToMember('[All Date].[Fiscal Month Name].&[201401]',CONSTRAINED).Lag(12)
: StrToMember('[All Date].[Fiscal Month Name].&[201411]',CONSTRAINED).Lag(12))))
ELSE
NULL
END
/* Last to last Year Active Products */
Member [Measures].[LLYCount]
as
CASE
WHEN
[Measures].[LLYDemand] > 0
THEN
DistinctCount(([All Items].[IMD Id].[IMD Id],[All Merchandise Presentations].[Merch Pres Key].[Merch Pres
Key], [All Items].[Style Name].CurrentMember, (StrToMember('[All Date].[Fiscal Month Name].&[201401]',CONSTRAINED).Lag(24)
: StrToMember('[All Date].[Fiscal Month Name].&[201411]',CONSTRAINED).Lag(24))))
ELSE NULL END
SELECT
[Measures].[CYCount], [Measures].[LYCount], [Measures].[LLYCount],
[Measures].[Proj Demand],[Measures].[LYDemand],[Measures].[LLYDemand]
ON
COLUMNS,
Non
Empty([All Items].[Demand Center Name].[Demand Center Name], [All Items].[Style Name].[Style Name])
ON ROWS
FROM
(SELECT (StrToSet('[All Items].[Style].[ALL]'))
ON COLUMNS
FROM
(SELECT (StrToSet('[All Items].[Demand Center].[ALL]'))
ON COLUMNS
FROM
(select (STRTOSET('[All Items].[Merch Group].&[MG-110]'))
on Columns
FROM
(SELECT (StrToSet('[All Merchandise Presentations].[Merch Pres Chnl Dkey].&[MPC-1]'))
ON COLUMNS
From
[FMI Forecasting]
WHERE {strToMember('[All Date].[Fiscal Month
Name].&[201401]',CONSTRAINED) :
StrToMember('[All Date].[Fiscal Month Name].&[201411]',CONSTRAINED)}
Requirements are as follows:
1. Distinct Count should not include products where Proj Demand is 0, when I am using Filter function to remove products with 0 demand, query is really slow and execution time goes up from 35- 40 secs to 8-9 Minutes.
2. When we apply filter (parameters) Distinct Count should be in the context of filters( which are mentioned in the select statement like Style, Demand Center and Merch Group). Currently after applying filters count does not change.
Thanks for help.Hi Skd78,
Thank you for your question.
I am trying to involve someone more familiar with this topic for a further look at this issue. Sometime delay might be expected from the job transferring. Your patience is greatly appreciated.
Thank you for your understanding and support.
Regards,
Charlie Liao
TechNet Community Support -
SQL DISTINCT COUNT IN DATE RANGE
Dear All
Thanks for your attention.
A table is storing the task summary of each order
e.g.
Date | Order Number | Task Number
2015-01-01 00:00:01 | ABC123456 | JOB001
2015-01-01 00:01:02 | ABC123456 | JOB002
2015-01-01 00:10:02 | ABC123444 | JOB001
2015-01-01 10:12:59 | ABC123456 | JOB002 (Since the job002 is not done correctly, need to work again)
2015-01-01 18:20:05 | ABC888888 | JOB001
2015-01-01 20:22:42 | ABC789456 | JOB001
2015-01-01 21:02:11 | BBB121212 | JOB002
I would like to write a single query that get the distinct count of order number in sepcific date range
result as expected in three columns:
2015-01-01
task_number | AM | PM
JOB001 | 2 | 2
JOB002 | 1 | 1
explain the figures here:
JOB001 AM = 2 (ABC123456, ABC123444)
JOB002 AM = 1 (two records of ABC123456 count as 1 since we would like to know the number of orders)
JOB001 PM = 2 (ABC888888, ABC789456)
JOB002 PM = 1 (BBB121212)
I wrote a query get similar result but the count of order is not distinct
SELECT task_number,
AM=SUM(CASE WHEN date >= ('2015-01-01 00:00:00') AND date < ('2015-01-01 12:00:00') THEN 1 ELSE 0 END),
PM=SUM(CASE WHEN date >= ('2015-01-01 12:00:00') AND date < ('2015-01-02 00:00:00') THEN 1 ELSE 0 END)
FROM MyTable
WHERE task_number = 'JOB001'
Could anyone advise how to enhance this query to let the result become disintct of order number?
Many Thanks,
swivanTry
select task_number,
count(distinct case when datepart(hour, [date]) >=0 and datepart(hour, [date]) < 12 then [OrderNumbers] END) as AM,
count(distinct case when datepart(hour, [date]) >=12 and datepart(hour, [date]) < 24 then [OrderNumbers] END) as PM FROM myTable
GROUP BY Task_Number
For every expert, there is an equal and opposite expert. - Becker's Law
My blog
My TechNet articles
Thank you very much, short and good answer -
Distinct count using lookup table
How can I get a distinct count of column values using a different table?
Let's say I want to get a distinct count of all "company_name" records in table "emp" that corespond (match) with "lookup" table, "state" category.
What I want is to find counts for all companies that have a value of "california" in the "state" column of the "lookup" Table. I want the output to look like:
Sears 17
Pennys 22
Marshalls 6
Macys 9
I want the result to show me the company names dynamically as I don't know what they are, just that they are part of the "state" group in the lookup Table. Does this make sense?
MMark,
In the future you might consider creating test cases for us to work with. Something similar to the following where sample data is created for each table as the union all of multiple select statementsselect 'INIT_ASSESS' lookup_type
, 1 lookup_value
, 'Initial Assessment' lookup_value_desc
from dual union all
select 'JOB_REF', 2, 'Job Reference' from dual union all
select 'SPEC_STA', 3, 'SPEC STA' from dual;
select 'INIT_ASSESS' rfs_category
, 1 val
from dual union all
select 'JOB_REF', 1 from dual union all
select 'JOB_REF', 1 from dual union all
select 'SPEC_STA', null from dual;Then we can either take your select statements and make them the source of a CTAS (create table as) statementcreate table lookup as
select 'INIT_ASSESS' lookup_type
, 1 lookup_value
, 'Initial Assessment' lookup_value_desc
from dual union all
select 'JOB_REF', 2, 'Job Reference' from dual union all
select 'SPEC_STA', 3, 'SPEC STA' from dual;, or include them as subfactored queries by using the with statement:with lookup as (
select 'INIT_ASSESS' lookup_type
, 1 lookup_value
, 'Initial Assessment' lookup_value_desc
from dual union all
select 'JOB_REF', 2, 'Job Reference' from dual union all
select 'SPEC_STA', 3, 'SPEC STA' from dual
), RFS as (
select 'INIT_ASSESS' rfs_category
, 1 val
from dual union all
select 'JOB_REF', 1 from dual union all
select 'JOB_REF', 1 from dual union all
select 'SPEC_STA', null from dual
select lookup_value_desc, count_all, count_val, dist_val
from lookup
join (select rfs_category
, count(*) count_all
, count(val) count_val
, count(distinct val) dist_val
from RFS group by rfs_category)
on rfs_category = lookup_type;Edited by: Sentinel on Nov 17, 2008 3:38 PM -
Hi,
I have a question about performing a distinct count of number of clients in my cube OLAP.
My clients-dimension table is populated from 4 different databases, this means that I can find the same clients with different Clients_ID (Natural key), because each client has a different ID in every database.
I want to calculate a distinct count on client’s phone number because it’s the same record in all Databases, I always get a big number which is not the right result. I checked deeply the issue and I found that the query I set seems
to not perform the distinct count right on the phone number.
The small test I did with only filters (see screenshot below) let me believe that my query is performing the distinct count on each client ID instead of the phone number.
Here it is the query I used to perform my distinct count:
WITH SET MySet AS
Filter(
{[Dim Date].[Year].&[2014]} *
{[DIM FA Clients].[FaCuNumberX].[FaCuNumberX]},[Measures].[QQT - Fact Ventes]>1 or [Measures].[QQT - Fact Ventes]=1)
MEMBER MEASURES.SETDISTINCTCOUNT AS
DISTINCTCOUNT(MySet)
SELECT {MEASURES.SETDISTINCTCOUNT} ON 0
FROM [CubeAll]
I don’t know why the filter on this mdx query doesn’t work properly like an SQL group by.
Thanks for help!Hi Merouane,
According to your description, you want to calculated to the numbers of client by count distinct Phone Number, right?
In your scenario, you can add a distinct count measure in your project. Distinctcounts are one of the most demanding requests that can be placed on a MOLAP database, caused by the large number of dimensional attributes that typically exist in a cube. Here
is a blog which describe several solutions for distinctcount in a SSAS project, please refer to the link below to see the detais.
http://www.mssqltips.com/sqlservertip/3043/different-options-for-creating-a-distinct-count-measure-in-ssas/
Regards,
Charlie Liao
TechNet Community Support -
MSSQL2005 Analysis Service Distinct Count
hi,
i am currently trying to build a distinct count on my cube (mssql2005 analysis services).
But after i added the discount count on the field i want to and start the processing, the following errors appear.
- Errors in the OLAP storage engine: The sort order specified for distinct count records is incorrect.
- Errors in the OLAP storage engine: An error occurred while processing the 'FACT VIEW STATISTIC' partition of the 'FACT VIEW STATISTIC 1' measure group for the 'Accident Statistic' cube from the OLAP_PROJECT database.
the count measure works fine.
will appreciate any help on this distinct count problem.
thanks in advance.
HYI also received this error:
"Errors in the OLAP storage engine: The sort order specified for distinct count records is incorrect. "
Running SQL Server 2005 SP2 Enterprise Edition
The collation between SQL Server and Analysis Services was the same.
The distinct count was on a character data type.
There were no NULLs in the data.
The cube was processing fine until new data was added.
After some investigation into the data it seems that the culprit was one row that the data length was 13 characters on the column of the distinct count. Everything else was less than 13 characters. (See results below). Updating this one row solved the problem. The exact value of the data is: '1-4296-175-9'
Here is a result set:
select len(columnname) as data_length, count(*) as count
from [tablename]
group by len(columnname)
order by data_length
data_length count
2 3
5 1
6 3
7 2
9 1
10 856
13 1
My question though is if SQL2005 can do distinct counts on strings then why choke on one row with an extra length? -
I’ve been at this for two days now. Could someone lead me down the right path? Given the following data set:
create table data_owner.test_data
item_number varchar2(10 byte),
store_number varchar2(10 byte),
calendar_year varchar2(10 byte),
calendar_week varchar2(10 byte),
units_sold integer
insert into test_data(item_number, store_number, calendar_year, calendar_week, units_sold)
values ('1111', '31', '2010', '51', 4)
insert into test_data(item_number, store_number, calendar_year, calendar_week, units_sold)
values ('1111', '16', '2010', '51', 2)
insert into test_data(item_number, store_number, calendar_year, calendar_week, units_sold)
values ('1111', '31', '2010', '52', 3)
insert into test_data(item_number, store_number, calendar_year, calendar_week, units_sold)
values ('1111', '27', '2010', '52', 1)
insert into test_data(item_number, store_number, calendar_year, calendar_week, units_sold)
values ('1111', '16', '2011', '1', 3)
insert into test_data(item_number, store_number, calendar_year, calendar_week, units_sold)
values ('1111', '27', '2011', '2', 5)
insert into test_data(item_number, store_number, calendar_year, calendar_week, units_sold)
values ('1111', '20', '2011', '2', 4)
insert into test_data(item_number, store_number, calendar_year, calendar_week, units_sold)
values ('2222', '27', '2010', '51', 3)
insert into test_data(item_number, store_number, calendar_year, calendar_week, units_sold)
values ('2222', '16', '2010', '52', 2)
insert into test_data(item_number, store_number, calendar_year, calendar_week, units_sold)
values ('2222', '20', '2010', '52', 1)
insert into test_data(item_number, store_number, calendar_year, calendar_week, units_sold)
values ('2222', '16', '2011', '1', 3)
insert into test_data(item_number, store_number, calendar_year, calendar_week, units_sold)
values ('2222', '31', '2011', '2', 3)
select * from test_data
item_number store_number calendar_year calendar_week units_sold
1111 31 2010 51 4
1111 16 2010 51 2
1111 31 2010 52 3
1111 27 2010 52 1
1111 16 2011 1 3
1111 27 2011 2 5
1111 20 2011 2 4
2222 27 2010 51 3
2222 16 2010 52 2
2222 20 2010 52 1
2222 16 2011 1 3
2222 31 2011 2 3
My desired result is a sum of units sold and an accumulative distinct count of store numbers grouped by item, year, and week. i.e.:
item_number calendar_year calendar_week store_count sum(units_sold)
1111 2010 51 2 6
1111 2010 52 3 4
1111 2011 1 3 3
1111 2011 2 4 9
2222 2010 51 1 3
2222 2010 52 3 3
2222 2011 1 3 3
2222 2011 2 4 3
I can’t seem to get the store count right. I’ve been trying various methods of the count(distinct store_number) over (…) analytic function, but nothing works. Thanks.Hi,
Interesting problem!
When using analytic functions, you can't use both DISTINCT and ORDER BY. Too bad; that sure would be convenient.
The most general solution is to use aggregate functions instead of analytic functions, and do a self-join to pair every row ("table" l, for "later" in the query below) with every earleir row ("table" e below) for the same item:
SELECT l.item_number
, l.calendar_year
, l.calendar_week
, COUNT (DISTINCT e.store_number) AS store_count
, SUM (l.units_sold)
/ COUNT (DISTINCT e.ROWID) AS total_units_sold
FROM test_data e
JOIN test_data l ON e.item_number = l.item_number
AND e.calendar_year || LPAD (e.calendar_week, 2)
<= l.calendar_year || LPAD (l.calendar_week, 2)
GROUP BY l.item_number
, l.calendar_year
, l.calendar_week
ORDER BY l.item_number
, l.calendar_year
, l.calendar_week
;You might think about storing a DATE (say, the date when the week begins) instead of year and week. It would simplify this query, and probably lots of other ones, too. I realize that might complicate some other queries, but I think you'll fiond a net gain.
Thanks for posting the CREATE TABLE and INSERT statements; that helps a lot!
Edited by: Frank Kulash on Nov 18, 2011 12:48 PM
Here's an analytic solution. As you can see, it requires more code, and more complicated code, but it might perform better:
WITH got_r_num AS
SELECT item_number
, calendar_year
, calendar_week
, units_sold
, ROW_NUMBER () OVER ( PARTITION BY item_number
, store_number
ORDER BY calendar_year
, calendar_week
) AS r_num
FROM test_data
SELECT DISTINCT
item_number
, calendar_year
, calendar_week
, COUNT ( CASE
WHEN r_num = 1
THEN 1
END
) OVER ( PARTITION BY item_number
ORDER BY calendar_year
, calendar_week
) AS store_count
, SUM (units_sold) OVER ( PARTITION BY item_number
, calendar_year
, calendar_week
) AS total_units_sold
FROM got_r_num
ORDER BY item_number
, calendar_year
, calendar_week
;This approah will not work in all windowing situations. It's okay fo this job, but not if you wanted,for example, a count of distinct stores from the last 6 weeks, and the report covers more than 6 weeks. -
Hi..
Can anyone tell me how to do the SQL
SELECT EMPNO,DISTINCT(COUNT(SALARY))
FROM EMPLOYEE
Thanks in Advnace......
RajeshwariYou need to count the distinct account_keys in the account table as an in-line view and join that to the product table. Something like:
SELECT p.product_key, p.name, a.cnt
FROM product p,
(SELECT product_key, COUNT(DISTINCT account_key) cnt
FROM account
GROUP BY product_key) a
WHERE p.product_key = a.product_keyAlthough, your sample data gives odd results because there are two entries for p1 A and product p4 has two names D and B, although since it does not appear in accounts, it makes no differene in this particular case.
If you actually want to show all of the products from the product table and a zero count if they do not appear in accounts, then you need an outer join, something like:
SELECT p.product_key, p.name, NVL(a.cnt, 0) cnt
FROM product p
LEFT JOIN (SELECT product_key, COUNT(DISTINCT account_key) cnt
FROM account
GROUP BY product_key) a
ON p.product_key = a.product_keyHTH
John
Maybe you are looking for
-
Error when Creating a delivery for an STO DEL
I have a client that at first was getting an error about shipping point can not be determined. I extended the sales view, and also created the Sales Org and division. When I created the delivery it worked. Then when the client tested this, it did n
-
How to use JPA in a worker thread?
I'm using WebLogic 10.3.5.0 with JPA 2.0 and have the following scenario: - user needs to start a task takes some time to complete; - this task needs to read and write to DB; One solution would be to write a worker and use the WorkManager feature to
-
Qt freezes during play... constantly
I upgraded to QT 7 and bought Pro, and ever since the image freezes often during play. I have to restart or redownload the movie to get it unstuck; sometimes changing Views (small to full screen) will nudge it. 10.3.9. This happens on both of my comp
-
Problem with the purchasing iOS 3.1 software update for iPod touch
I have an ipod touch (1st generation) and now while I'm looking for my update to iOS 3.1 for the most current, seeking the support of Apple. I found an article talking about buying an upgrade for the iPod touch iOS 3.1 Software (1st generation) then
-
One of my app's has changed it's icon
Mac DVD Ripper Pro (MDRP): Usually when the transfer is finished, it will appear using a "dvd" icon. Recently it changed to plain paper with a corner turned. I brought this up with MDRP, and they said there was nothing wrong with the app & I was 't