rhservan
asked on
How do I return the total count of a column through 3 filters?
I need a query that will return a single summary row on a single column for three different filters so that the output looks like this:
--------Filter1--------Fil ter2------ --Filter3
Total----30-------------52 ---------- --22
Count
--------Filter1--------Fil
Total----30-------------52
Count
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Removing null values when you are performing calculations can be important, because certain calculations, such as an average, can be inaccurate if NULL columns are included (ms-help://MS.SQLCC.v10/MS .SQLSVR.v1 0.en/s10de _1devconc/ html/f77b3 bfe-b5be-4 17e-96a3-f f3a19c3311 9.htm).
So better to avoid ‘NULL’
select count(case when filter1 then 1 else null end) as fil1
,count(case when filter2 then 1 else null end) as fil2
,count(case when filter3 then 1 else null end) as fil3
from yourtable
where ....
Instead you can try below one
SELECT SUM(CASE WHEN Filter1 THEN 1 ELSE 0 END) FilterData1,
SUM(CASE WHEN Filter2 THEN 1 ELSE 0 END) FilterData2,
SUM(CASE WHEN Filter3 THEN 1 ELSE 0 END) FilterData3
FROM MyTable
WHERE <FilterCondition>
E.g.
-----------
SELECT SUM(CASE Name = 'Name1' THEN 1 ELSE 0 END) FilterData1,
SUM(CASE Age < 20 THEN 1 ELSE 0 END) FilterData2,
SUM(CASE Gender = 'M' THEN 1 ELSE 0 END) FilterData3
FROM MyTable
WHERE Country = 'XYZ'
So better to avoid ‘NULL’
select count(case when filter1 then 1 else null end) as fil1
,count(case when filter2 then 1 else null end) as fil2
,count(case when filter3 then 1 else null end) as fil3
from yourtable
where ....
Instead you can try below one
SELECT SUM(CASE WHEN Filter1 THEN 1 ELSE 0 END) FilterData1,
SUM(CASE WHEN Filter2 THEN 1 ELSE 0 END) FilterData2,
SUM(CASE WHEN Filter3 THEN 1 ELSE 0 END) FilterData3
FROM MyTable
WHERE <FilterCondition>
E.g.
-----------
SELECT SUM(CASE Name = 'Name1' THEN 1 ELSE 0 END) FilterData1,
SUM(CASE Age < 20 THEN 1 ELSE 0 END) FilterData2,
SUM(CASE Gender = 'M' THEN 1 ELSE 0 END) FilterData3
FROM MyTable
WHERE Country = 'XYZ'
#Null# in averages etc...
it all depends on what you are trying to achieve... and wether or not the filters are related or independant as to wether to use NULL or 0 with the aggregate functions...
it all depends on what you are trying to achieve... and wether or not the filters are related or independant as to wether to use NULL or 0 with the aggregate functions...
ASKER
@lowfatspread & nirmalrampk
My concern right now is the sums are not correct yet. It returns a much higher number in some cases than what is actually the count. What I percieve is going on is the sum\counts are filtered correctly but the returns are like this:
There is a column in the table which adds more rows, for an example:
ColumnA------------ColumnB
Type1----------------Type1 a
Type1----------------Type1 b
Type1----------------Type1 c
Type1----------------Type1 d
Type2----------------Type2 a
Type2----------------Type2 b
etc.........
Now the count needs to be on the distinct single count of each type in ColumnA. In the example there are 2 Types in ColumnA. The query results we are discussing retruns ColumnB for a total count of 6 as in the example.
Here is what I need:
I need a count of 2 as in ColumnA and I am getting a count of 6 as in ColumnB. How do Ido this?
My concern right now is the sums are not correct yet. It returns a much higher number in some cases than what is actually the count. What I percieve is going on is the sum\counts are filtered correctly but the returns are like this:
There is a column in the table which adds more rows, for an example:
ColumnA------------ColumnB
Type1----------------Type1
Type1----------------Type1
Type1----------------Type1
Type1----------------Type1
Type2----------------Type2
Type2----------------Type2
etc.........
Now the count needs to be on the distinct single count of each type in ColumnA. In the example there are 2 Types in ColumnA. The query results we are discussing retruns ColumnB for a total count of 6 as in the example.
Here is what I need:
I need a count of 2 as in ColumnA and I am getting a count of 6 as in ColumnB. How do Ido this?
ASKER
@ewangoya I was able to make it work as you wrote it above. However, Once I add these two params to each filter, which I need to exclude other items, My return is all zeroes.
AND DimCompany.CompanyDesc IN (@CompanyName)
AND(AdHoc.DimDate.Date_AK BETWEEN @StartDate AND @EndDate)
I copied the below attachment to my SSRS Dataset and I can no longer make it work with these. Any ideas?
SampleEwangoya.txt
AND DimCompany.CompanyDesc IN (@CompanyName)
AND(AdHoc.DimDate.Date_AK BETWEEN @StartDate AND @EndDate)
I copied the below attachment to my SSRS Dataset and I can no longer make it work with these. Any ideas?
SampleEwangoya.txt
AND DimCompany.CompanyDesc IN (@CompanyName)
doesn't make any sense... you can only test against a single value....
EQUALS or NOT EQUALS....
if @companyname is supposed to be a list of values then you need to parse that through a table valued function in order for IN processing to work...
doesn't make any sense... you can only test against a single value....
EQUALS or NOT EQUALS....
if @companyname is supposed to be a list of values then you need to parse that through a table valued function in order for IN processing to work...
ASKER
@lowfatspread - If you are referring to the attachment based ewangoya, Ill check on that. But What about 35160345 referring to your original query any thoughts?
ASKER
Also, there are no nulls.
ASKER
More informatiopn, COUNT returns all rows even if distinct is used.
ASKER
Okay, Nothing has worked up to this point so it is still anybodys game. Here is another way to approach it: The query below gives me the exact output I am looking for except it is single column & single row. Put this exact query into 6 columns, 1 row. How can this be done?!
SELECT COUNT(DISTINCT DimInvoiceDetail.Invoice_A K) AS 'Inv #'
FROM AdHoc.DimInvoiceDetail AS DimInvoiceDetail INNER JOIN AdHoc.DimCustomer AS DimCustomer ON DimInvoiceDetail.Customer_ SK = DimCustomer.Customer_SK INNER JOIN
AdHoc.DimCompany AS DimCompany ON DimInvoiceDetail.Company_S K = DimCompany.Company_SK INNER JOIN AdHoc.SalesData AS SalesData ON DimInvoiceDetail.InvoiceDe tail_SK = SalesData.InvoiceDetail_SK INNER JOIN AdHoc.DimDate ON DimInvoiceDetail.InvoiceDa te_SK = AdHoc.DimDate.Date_SK
WHERE (DimInvoiceDetail.OrderTyp e NOT IN ('DE', 'DH', 'DJ', 'DK', 'DL', 'DM', 'E2', 'E4', 'W2', 'W4', 'W5', 'W6')) AND (SalesData.GPPercentage <= 0.05) AND (AdHoc.DimDate.Date_AK BETWEEN @StartDate AND @EndDate) AND (DimCompany.CompanyDesc IN (@CompanyName))
SELECT COUNT(DISTINCT DimInvoiceDetail.Invoice_A
FROM AdHoc.DimInvoiceDetail AS DimInvoiceDetail INNER JOIN AdHoc.DimCustomer AS DimCustomer ON DimInvoiceDetail.Customer_
AdHoc.DimCompany AS DimCompany ON DimInvoiceDetail.Company_S
WHERE (DimInvoiceDetail.OrderTyp
ASKER
There has been no solution to this prolem in responses above.
Please disregard this question and respond to new submitted question regarding same problem
Thanks for your expert help
Please disregard this question and respond to new submitted question regarding same problem
Thanks for your expert help
ASKER
ASKER
I found that one of the experts above have provided the correct information and would like to award points. I apologize for the way things went here. But, I would like to award them to erwangoya.
ASKER
Excellent solution to my problem. I just had to work with it more to get to resolution. Would have appreciated some additional follow up though. I have attached my final query which works with most of the elements you provided. Look near the 2nd to last comment
select count(case when filter1 then 1 else null end) as fil1
,count(case when filter2 then 1 else null end) as fil2
,count(case when filter3 then 1 else null end) as fil3
from yourtable
where ....