Sum Distinct Count
Is there a way to sum a distinct count?
I have a distinct count placed on GF2 and need the Sum of the Distinct count of GF1.
Is this possible?
Thanks for any help and suggestions,
Jen
Hi Jen,
Try this:
1) Create a formula on the Group Footer 2:
WhilePringtingRecords;
Numbervar dc;
dc := dc + distinctcount({database field}, {group});
2) Create another formula to display the sum and place this on the GF1:
WhilePringtingRecords;
Numbervar dc;
3) Create a reset formula and place it on the GH1:
WhilePringtingRecords;
Numbervar dc := 0;
-Abhilash
Similar Messages
-
Getting Sum, Count and Distinct Count of a file
Hi all this is a UNIX question.
I have a large flat file with millions of records.
col1|col2|col3
1|a|b
2|c|d
3|e|f
3|g|h
footer****
I am supposed to calculate the sum of col1 =9, count of col1 =4, and distinct count of col1 =c3
I would like it if you avoid external commands like AWK. Also, can we do the same by creating a function?
Please bear in mind that the file is huge
Thanks in advanceThis sounds like homework for a shell class, but here goes. Save into a file, maybe "doit". Run it like this:
$ ./doit < data
<snip>
#!/bin/sh
got0=0
got1=0
got2=0
got3=0
got4=0
got5=0
got6=0
got7=0
got8=0
got9=0
sum=0
cnt=0
IFS='|'
while read c1 c2 c3 junk; do
# Sum and count
echo "c1=${c1}"
case "${c1}" in
[0-9] )
sum=$(expr ${sum} + ${c1})
cnt=$(expr ${cnt} + 1)
esac
# Distinct
case "${c1}" in
0 ) got0=1;;
1 ) got1=1;;
2 ) got2=1;;
3 ) got3=1;;
4 ) got4=1;;
5 ) got5=1;;
6 ) got6=1;;
7 ) got7=1;;
8 ) got8=1;;
9 ) got9=1;;
esac
done
echo "cnt=${cnt}"
echo "sum=${sum}"
echo "distinct="$(expr $got0 + $got1 + $got2 + $got3 + $got4 + $got5 + $got6 + $got7 + $got8 + $got9)
<snip> -
Regular measures(measures with SUM function) are not working along Distinct count measures
Hi All,
I am creating a cube that got to have a distinct count measure and a sum measure. if i have created only sum measure then it is working fine. if i create both measures and process the cube only distinct count measure is populated. the sum measure is showing
all blank values. i am using 2008 R2, and creating 2 different measure groups for both measures, after i include the distinct count measure the sum measure becoming null. can you please help me with this? i am breaking my head for last 2 days on this.. Thank
YouRamesh, measures are affected by the context of the queries that contain them, for example and in some cases, you can get a different total count of something by two different queries, this is because the context of the first query is different than
the second one ... keep this in mind.
Now, I've noticed that you are "creating 2 different measure
GROUPS for both measures", and i guess that you are trying to view those two measures _which are from different measure
groups_ at the same time and in the same report.
considering the info in the first point and as you are create the calculated measures in two different measure
groups, I'm not sure but i guess that this is the problem, and i suggest you create those two calculated measures
in the same measure group, then try to view them again and let's see.
if the previous point didn't solve it, please post the expressions you are using to create the calculated measures, maybe this will help in finding the problem. -
Report using Tabular Model and Measures based on Distinct Counts
Hello,
I am creating a report that should present something like this:
YEAR-1 | MONTH-1 | MONTH-2 | MONTH-3... | YEAR | MONTH-1 | MONTH-2 | MONTH-3...
My problem is that when designing the dataset to support this layout I drag the Year, Month and Distinct count Measure, but on the report when I want the value for the YEAR level I don't have it and I cannot sum the months value...
What is the best aproach to solve this? Do I really have to go to advanced mode and customize my MDX or DAX? Can't basic users do something like this that seems so trivial and needed?
Thank you
Luis SimõesHi Luis,
According to your description, you create a Reporting Services report using Analysis Service Tabular Model as the datasource, now what you want is sum the months value on year level, right?
In your scenario, you can add the Month field to column group, add a parent group using Year Field and then add a Total on Month group. In this case, Reporting Services will sum the months value on Year level. I have tested it on my local environment, the
screenshot below is for you reference.
Reference:Lesson 6: Adding Grouping and Totals (Reporting Services)
If this is not what you want, please describe your dataset structure, so that we can make further analysis.
Regards,
Charlie Liao
TechNet Community Support -
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 doesn't return the expected results
Hi All,
I was fighting a little trying to implement a Distinct Count measure over an account dimension in my cube. I read a couple of posts relateed to that and I followed the steps posted by the experts.
I could process the cube but the results I'm getting are not correct. The cube is returning a higher value compared to the correct one calculated directly from the fact table.
Here are the details:
Query of my fact table:
select distinct cxd_account_id,
contactable_email_flag,
case when recency_date>current_date-365 then '0-12' else '13-24' end RECENCY_DATE_ROLLUP,
1 QTY_ACCNT
from cx_bi_reporting.cxd_contacts
where cxd_account_id<>-1 and recency_date >current_date-730;
I have the following dimensions:
Account (with 3 different hierarchies)
Contactable Email Flag (Just 3 values, Y, N, Unknown)
Recency_date (Just dimension members)
All dimensions are sparse and the cube is a compressed one. I defined "MAXIMUM" as aggregate for Contactable Email flag and Recency date and at the end, SUM over Account.
I saw that there is a patch to fix an issue when different aggregation rules are implemented in a compressed cube and I asked the DBA folks to apply it. They told me that the patch cannot be applied because we have an advanced version already installed (Patch 11.2.0.1 ).
These are the details of what we have installed:
OLAP Analytic Workspace 11.2.0.3.0 VALID
Oracle OLAP API 11.2.0.3.0 VALID
OLAP Catalog 11.2.0.3.0 VALID
Is there any other patch that needs to be applied to fix this issue? Or it's already included in the version we have installed (11.2.0.3.0)?
Is there something wrong in the definition of my fact table and that's why I'm not getting the right results?
Any help will be really appreciated!
Thanks in advance,
MartínNot sure I would have designed the dimensions /cubes as you, but there is another method you can obtain distinct counts.
Basically relies on using basic OLAP DML Expression language and can be put in a Calculated Measure, or can create two Calculated measures
to contain each specific result. I use this method to calculate distinct counts when I want to calculate averages, etc ...
IF account_id ne -1 and (recency_date GT today-365) THEN -
CONVERT(NUMLINES(UNIQUELINES(CHARLIST(Recency_date))) INTEGER)-
ELSE IF account_id ne -1 and (recency_date GT today-730 and recency_date LE today-365) THEN -
CONVERT(NUMLINES(UNIQUELINES(CHARLIST(Recency_date))) INTEGER)-
ELSE NA
This exact code may not work in your case, but think you can get the gist of the process involved.
This assumes the aggregation operators are set to the default (Sum), but may work with how you have them set.
Regards,
Michael Cooper -
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 -
Hi,
I’m facing a little issue to calculate a distinct count of number of clients in SSAS OLAP Cube. The difficulty appears for the credited client’s accounts, in other words, for the clients how have credit (quantity = -1) or for the clients
how have bought the product and they receive a credit after (quantity = 0). My actual distinct count in my cube considers these two cases as real buying transaction, but in fact they’re not. I’ve checked in SSAS to make a distinct count with the expression
(SUM Quantity > 1), but I didn’t find nothing. Now I’m thinking to model these cases directly in my Datawarehouse, but I don’t see how can’t do it. Can anyone de give me a little help? Thanks.Hi Merouane,
According to your description, you want to count the members with the condition quantity=-1, right? In this case, we can use Filter function inside the Count function. The query will looks like below.
Count(Filter([Product].[Product].[Product], [Measures].[Internet Order Quantity] =-1))
However, the Filter function might cause a performance issue, we can change the query to
Sum(
[Product].[Product].[Product],
Iif([Measures].[Internet Order Quantity] =-1,1,0))
For the detail information about it, please refer to the link below.
http://sqlblog.com/blogs/mosha/archive/2007/11/22/optimizing-count-filter-expressions-in-mdx.aspx
Regards,
Charlie Liao
TechNet Community Support -
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. -
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. -
The distinct count issue?
Hello experts,
So, I have an OBI report (table view). I needed to get the percentage difference btn 2 columns, I did. Then I had to summarize difference in 4 buckets (0-15, 16-30, 31-50, >50%); I did (case statement). NOW, I need to summarize(distinct count) the above buckets based on Store numbers for each day.
Basically, if the difference is btn(0-5%) and I have 5 stores then I need to see 5 stores separately. The problem I am having when I do the distinct count instead of having the counts separately for each bucket I am getting the total. I see the buckets summarized, but the store column is showing the total number of all(we have about 700 stores) instead of breaking down the count for each bucket. In the stores column I am using the distinct count function, I don't know if the problem is here or the case statement.
I know its Friday, still I believe there is someone who might have an idea on my dilemma.
As always, your insights are highly appreciated,Header 1
Header 2
Header 3
Header 4
Header 5
Header 6
Date
Store Count
Sales
Todate Sales
Difference %
Difference buckets
03/24/2013
698
716,374
717,011
-0%
0-15%
03/25/2013
698
583,335
583,793
-0%
0-15%
03/26/2013
698
220,640
220,886
-0%
0-15%
03/27/2013
698
236,803
188,150
215
11-30%
Header 8
Srini,
I am still not good at using OTN tables, but above is what I am talking about. Basically, The store count should not show the total count of all stores, instead it should show only distinct count that within the percentage bucket. As u can see it gives me the total, I made sure the aggregate rule isn't sum. By the way, could the problem be the case statement not the store column..
I also tried bucket and store only, still it shows the sum of all stores and not just distinct.
any idea -
<p>I have a report that gets a distinct count of items per day.</p><p>DistinctCount(, , "daily")</p><p> I would like to create a running total of the daily count for a weekly grand total.</p><p>Thanks!</p>
<p>I'll take a stab at this - So I understand that the report is grouped by Day and looks like this:</p><p>GF1: 1/1/07 9</p> <p>GF1: 1/2/07 10</p> <p>GF1: 1/3/07 12</p> <p>GF1: 1/4/07 15</p> <p>GF1: 1/5/07 20</p><p> </p><p>The first thing I'd try is to create a another group on your Date field, this time selecting the grouping option to be "For Each Week". Take that group and move it so that it is Group1 and your current group is Group 2.... your structure will now look like this. </p><p>GH1: 1/1/07 < new 'week' grouping' > </p><p> GF2: 1/1/07 9</p> <p> GF2: 1/2/07 10</p> <p> GF2: 1/3/07 12</p> <p> GF2: 1/4/07 15</p> <p> GF2: 1/5/07 20</p><p> Then I would copy the Sum field (distinct count) that you are using in Group 2 and put it in the Group 1 footer. We could also create a formula to sum up all distinct counts per day, but a Distinct Count by week should accomplish what you are after. Then in a seperate formula, you could divide the DistinctCount of orders (by week) by the DistinctCount of dates (by week) to get your daily average....</p><p>formula:</p><p>DistinctCount ({Orders.Order ID}, {Orders.Order Date}, "weekly") <br />/ <br />DistinctCount ({Orders.Order Date}, {Orders.Order Date}, "weekly") </p><p> </p><p>Hope that helps. </p>
-
[SQL] how can i get this result....??(accumulation distinct count)
Hi everybody,
pls tell me how can it possible to get result?
### sample data
date visitor
10-01 A
10-01 A
10-01 B
10-01 B
10-01 C
10-01 C
10-02 A
10-02 C
10-02 C
10-02 D
10-02 D
10-03 B
10-03 B
10-03 B
10-03 A
10-03 A
10-03 F
10-04 A
10-04 A
10-04 F
result that i want...like this.
date date_unqiue_visitors acc_date_unique_visitors
10-01 3 3
10-02 3 4
10-03 3 5
10-04 2 5
date distinct visitors : count(distinct visitor)
but how can i get accumulation distinct visitor count???
Thanks to every body..SQL> select dt,cnt,sum(cnt1) over(order by dt) cnt1
2 from(
3 select dt,count(distinct visitor) cnt,sum(flg) cnt1
4 from
5 (select dt,visitor,(select decode(count(*),0,1,0)
6 from test
7 where rowid < t.rowid
8 and visitor = t.visitor) flg
9 from test t)
10 group by dt);
DT CNT CNT1
10-01 3 3
10-02 3 4
10-03 3 5
10-04 2 5
Message was edited by:
jeneesh
Wrong... -
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
;
Maybe you are looking for
-
What cables do I need for Apple TV streaming from my iPad to my tv
What cables do I need for Apple TV, streaming fom my iPad to a HD tv?
-
What system utilities do you recommend?
Hi, I am a new switcher to Mac. Can you please recommend what system utilities I should purchase and which brand I should purchase. The ones I want are : 1) I need an anti-virus program (I know there are no real problems on the Mac with virus's, but
-
iphone 4, since iso5 update, first my wifi greyed out, loss of camera, not total power loss, not even charging.. dead iphone 4. can any one help please..
-
Populate Form Field from Post-Query Trigger
I have a form with some fields like "start_date", "company_id", "previous_start_date", "previous_company_id". Out of these, the "previous" fields are unbound and the others are bound to "my_companies" table. All are in the same block on the form. I h
-
Of the few solutions I have read, all talk about slowness or some other performance issue - my mouse wheel scrolling has stopped altogether and nothing I have tried works, including restarting with add-ons disabled and checking the config.