Showing posts with label aggregation. Show all posts
Showing posts with label aggregation. Show all posts

Monday, March 26, 2012

Problem with an aggregation

Hi,

I am new to the reporting services and I've been working on problem in one of my reports all day long and after 8 hours of frusturation I decided ask for a profesional help.

Ok here is my problem: I have a report that calculates the amount of meetings with our clients. The dataset contains an activity_id field that we assign for each our meetings with our clients. SSRS counts these meetings and shows it in a drilldown enabled report. Everything seems fine on the report except that someof the activities involves few different clients and SSRS is not counting the activities multiple times in region drilldown as there is only one activity id associates in that region even though it contains different companies. And I want those companies to be calculated in too.

From the crude drawing below I wanted to explain my dilemma visually. As it can be seen the total number of meetings we had is actually 40. But as we had 3 activities that involves more than 1 clients it only gives 37 as a count. I would like to know is there a way to make the report count the same activity multiple times if activity_id is associated with more than one clients.

I hope I managed to explain my problem

**********************************************************************************************

Manager Region Market Company Meeting Detail

+Manager 1 (9 meetings)

+Manager 2 (37 meetings)

- West (37 meetings)

-Denver (37 meetings)

+Company 1 (5 meetings)

+Company 2 (2meetings)

+Company 3 (2meetings)

+Company 4 (3meetings)

+Company 5 (0meetings)

+Company 6 (0meetings)

+Company 7 (5meetings)

+Company 8 (1meetings)

+Comapny 9 (19meetings)

+Company 10 (3meetings)

Total (40 meetings)

You would need to use a composite key by grouping on the combination of the activity_id and company_id rather than just the activity_id. I believe strongly in doing as much of the math as possible in the SQL statement, rather than the report. That way you can use your same report and simply show the total that was calculated rather than the calculating the total. For example:

Code Snippet

SELECT

base.Manager,

base.Region,

base.Market,

base.Company,

base.MeetingDetail,

MTot.Total as ManagerTotal,

RTot.Total as RegionTotal,

M2Tot.Total as MarketTotal,

CTot.Total as CompanyTotal

FROM

baseTable base,

(SELECT COUNT(*) AS Total FROM baseTable bt WHERE bt.Manager = base.Manager) MTot,

(SELECT COUNT(*) AS Total FROM baseTable bt WHERE bt.Manager = base.Manager AND bt.Region = base.Region) RTot,

etc.

This is really bad SQL, but I don't know the actual structure that you are working from and I think this conveys the idea.

If this isn't clear, post or send me the table structures and I will help you build a query that calculates your totals.

Larry

|||

Thank you Larry,

I will try to follow up with your suggestion hopefully it will help. if I got stuck I will post the table structure to you. Thank you again for your prompt response.

Regards,

Burak

|||

Hi Larry,

I've been trying to use your suggestion but I couldn't figure it out a way to use it.

My sql code is as follows

Code Snippet

SELECT DISTINCT
TOP 100 PERCENT ACTIVITY_ID, Date, ACTIVITY_TYP_NM, LAST_NM, FIRST_NM, COMPANY_NM, DISPLAY_NM, Region, SUMMARY, MLAE, Market,
STATE, Expr1
FROM dbo.INTV_Sales_Funnel
GROUP BY ACTIVITY_ID, Date, ACTIVITY_TYP_NM, LAST_NM, FIRST_NM, COMPANY_NM, DISPLAY_NM, Region, SUMMARY, MLAE, Market, STATE,
Expr1
ORDER BY ACTIVITY_ID, Date, SUMMARY, COMPANY_NM

MLAE represents the Manager

and Expr1 represents the Count

the thing is when I used the count im getting a value that more than I should get and the reason for this, is in some of our events we invite multiple people from a company or multiple people from multiple companies.But the report should reflect only 1 count of the event if the meeting had either 1 person or more from a single company. I managed to get rid of those extra people on the meetings over the report so activities only listed once. But it seems like I really need to find a way to make that calculation over the report. Is there a way to use Count Distinct on report with 2 values. Right now I am using Activity ID but I guess if add both activity id and company name as you mentioned earlier on your response I might be able to solve this problem.

|||

First, on re-reading your original post, I am not sure that your original solution wasn't correct. If you had 37 meetings and 3 meetings had two customers each then you would show the result of the sum of the meetings that each customer attended was 40 rather than 37 because each of the three meetings with two customers each would be counted twice.

That said, here is a method of generating the query that will return your results. Lets start with what you know that you want and build the query from left to right on your table above. This will not get us the most optimized query, but it should show the method of layering results within a query that can allow you to get complex results with simple nested queries. One advantage to this method is that you can check the results at each stage.

First, let's get a count of all of the meetings for the managers:

Code Snippet

SELECT
DISF.MLAE,
COUNT(DISF.Activity_Id) ManagerTotal
FROM
(SELECT
DISTINCT
MLAE,
Activity_Id
FROM
INTV_Sales_Funnel
) DISF
GROUP BY
DISF.MLAE


Then, let's get the count of meetings per region per manager:

Code Snippet

SELECT
DISF.MLAE,
DISF.Region,
COUNT(DISF.Activity_Id) RegionTotal
FROM
(SELECT
DISTINCT
MLAE,
Region,
Activity_Id
FROM
INTV_Sales_Funnel
) DISF
GROUP BY
DISF.MLAE,
DISF.Region

Next, let's get the count of meetings per market within a region for each manager:

Code Snippet

SELECT
DISF.MLAE,
DISF.Region,
DISF.Market,
COUNT(DISF.Activity_Id) MarketTotal
FROM
(SELECT
DISTINCT
MLAE,
Region,
Market,
Activity_Id
FROM
INTV_Sales_Funnel
) DISF
GROUP BY
DISF.MLAE,
DISF.Region,
DISF.Market

Lastly, let's get the count of meetings per customer within each market and region for each manager:

Code Snippet

SELECT
DISF.MLAE,
DISF.Region,
DISF.Market,
DISF.Company_NM,
COUNT(DISF.Activity_Id) CompanyTotal
FROM
(SELECT
DISTINCT
MLAE,
Region,
Market,
Company_NM,
Activity_Id
FROM
INTV_Sales_Funnel
) DISF
GROUP BY
DISF.MLAE,
DISF.Region,
DISF.Market,
DISF.Company_NM

Then, when you want to combine them and return all of the values, join each of the four queries to the main query as follows:

Code Snippet

SELECT
main.Activity_Id,
main.Date,
main.ACTIVITY_TYP_NM,
main.LAST_NM,
main.FIRST_NM,
main.COMPANY_NM,
main.DISPLAY_NM,
main.Region,
main.SUMMARY,
main.MLAE,
main.Market,
main.STATE,
mgr.ManagerTotal,
rgn.RegionTotal,
mkt.MarketTotal,
cmp.CompanyTotal
FROM
INTV_Sales_Funnel main
JOIN
(
SELECT
DISF.MLAE,
COUNT(DISF.Activity_Id) ManagerTotal
FROM
(SELECT
DISTINCT
MLAE,
Activity_Id
FROM
INTV_Sales_Funnel
) DISF
GROUP BY
DISF.MLAE
) mgr
ON
main.MLAE = mgr.MLAE
JOIN
(
SELECT
DISF.MLAE,
DISF.Region,
COUNT(DISF.Activity_Id) RegionTotal
FROM
(SELECT
DISTINCT
MLAE,
Region,
Activity_Id
FROM
INTV_Sales_Funnel
) DISF
GROUP BY
DISF.MLAE,
DISF.Region
) rgn
ON
main.MLAE = rgn.MLAE
AND
main.Region = rgn.Region
etc.

I hope this helps. Please let me know how it turns out.

Larry

Problem with aggregation...

I am currently facing 2 problems
1. with calculated cells on my cube. Bascially I have used a Time dimension
broken into year, week and days and a measure which is aggregated as
distinct count
Imagine for 2004, week 52, I have 7 days and the distinct count for all days
are 2, I am not able to see that the total distinct count for week 52 as 14
(7 *2), is there any way to do tat?
2. I also have a caculate measure which rely on the distinct count measure
to calcualte percentages, the returned values for the 7 days in week 52 is
correct, but it's summing up for the total in week 52 which it should be
actaully doing an average.
Can someone help me out with this or at least point out how to go about
doing this? ThanksHello all, I just found out that the 2nd problem is due to the first, but
I'm still trying to figure out how to do a aggregated distinct count value
for my cube. Just to make things clearer, here's an example
Distinct Count Measure
Week 1 Total 3 (<- I need this to be 13)
Days
1 2
2 2
3 1
4 1
5 1
6 3
7 3
I enabled drilldown for the cube and verified that the week1 total is really
a distinct count of 3 only... but what I am really trying to is to count
distinctly for days... but aggregate for Weeks and Years... Is there anyway
to achieve that?
Any form of advise is greatly appreciated... thanks in advance
"Nestor" <test@.test.com> wrote in message
news:Oow6ZqKLFHA.3832@.TK2MSFTNGP12.phx.gbl...
> I am currently facing 2 problems
> 1. with calculated cells on my cube. Bascially I have used a Time
dimension
> broken into year, week and days and a measure which is aggregated as
> distinct count
> Imagine for 2004, week 52, I have 7 days and the distinct count for all
days
> are 2, I am not able to see that the total distinct count for week 52 as
14
> (7 *2), is there any way to do tat?
> 2. I also have a caculate measure which rely on the distinct count measure
> to calcualte percentages, the returned values for the 7 days in week 52 is
> correct, but it's summing up for the total in week 52 which it should be
> actaully doing an average.
>
> Can someone help me out with this or at least point out how to go about
> doing this? Thanks
>|||You can use Calculated Member or Calculated Cell.
If you use Calculated Member,
Dcount = IIF(Time.CurrentMember.Level.Name = "Day", [Distinct Count
Measure], Sum(Time.CurrentMember.Children, [Dcount]))
Or if you use Calculated Cell,
Calculation Subcube: {[Measures].[Distinct Count Measure]},
Descendants(Time.Year.Members, Week, SELF_AND_BEFORE)
Calculation Value: Sum(Time.CurrentMember.Children, [Distinct Count
Measure])
But this is the case when you consider only time dimension. I'm not sure you
have to consider more dimensions.
Ohjoo Kwon
"Nestor" <test@.test.com> wrote in message
news:u$nyDYVLFHA.3988@.tk2msftngp13.phx.gbl...
> Hello all, I just found out that the 2nd problem is due to the first, but
> I'm still trying to figure out how to do a aggregated distinct count value
> for my cube. Just to make things clearer, here's an example
> Distinct Count Measure
> Week 1 Total 3 (<- I need this to be 13)
> Days
> 1 2
> 2 2
> 3 1
> 4 1
> 5 1
> 6 3
> 7 3
> I enabled drilldown for the cube and verified that the week1 total is
really
> a distinct count of 3 only... but what I am really trying to is to count
> distinctly for days... but aggregate for Weeks and Years... Is there
anyway
> to achieve that?
> Any form of advise is greatly appreciated... thanks in advance
>
> "Nestor" <test@.test.com> wrote in message
> news:Oow6ZqKLFHA.3832@.TK2MSFTNGP12.phx.gbl...
> dimension
> days
> 14
measure[vbcol=seagreen]
is[vbcol=seagreen]
>|||Thanks a lot of the help Ohjoo, I'm using calculated member and I'm
inputting the MDX statement into the ValuedExpression, basically this is the
MDX i've keyed into the Value Expression
iif
([My Time].CurrentMember.Level.Name = "Day",
[Measures].[Distinct Products],
iif([My Time].CurrentMember.Level.Name = "Year",
sum([My Time].CurrentMember.Children, [Measures].[New Calculated
Measure]), <-- Error here
sum([My Time].CurrentMember.Children, [Measures].[Distinct
Products])
)
)
What I am trying to do is to count distinctly for days only, for weeks it
should aggregate the days distinct count and for years it should aggregate
the weeks sum. The calculated measure is simply called "New Calculated
Measure"
Can this be done?
count(distinct(<measure to count> ), exlcudeempty)
"Ohjoo Kwon" <ojkwon@.olap.co.kr> wrote in message
news:OWnHDMWLFHA.2796@.tk2msftngp13.phx.gbl...
> You can use Calculated Member or Calculated Cell.
> If you use Calculated Member,
> Dcount = IIF(Time.CurrentMember.Level.Name = "Day", [Distinct Count
> Measure], Sum(Time.CurrentMember.Children, [Dcount]))
> Or if you use Calculated Cell,
> Calculation Subcube: {[Measures].[Distinct Count Measure]},
> Descendants(Time.Year.Members, Week, SELF_AND_BEFORE)
> Calculation Value: Sum(Time.CurrentMember.Children, [Distinct Count
> Measure])
> But this is the case when you consider only time dimension. I'm not sure
> you
> have to consider more dimensions.
> Ohjoo Kwon
>
> "Nestor" <test@.test.com> wrote in message
> news:u$nyDYVLFHA.3988@.tk2msftngp13.phx.gbl...
> really
> anyway
> measure
> is
>|||Next is simpler.
IIF(Time.CurrentMember.Level.Name = "Day",
[Distinct Products],
Sum(Time.CurrentMember.Children, [New Calculated Measure])
)
Ohjoo
"Nestor" <n3570r@.yahoo.com> wrote in message
news:OT2O0gbLFHA.1156@.TK2MSFTNGP09.phx.gbl...
> Thanks a lot of the help Ohjoo, I'm using calculated member and I'm
> inputting the MDX statement into the ValuedExpression, basically this is
the
> MDX i've keyed into the Value Expression
> iif
> ([My Time].CurrentMember.Level.Name = "Day",
> [Measures].[Distinct Products],
> iif([My Time].CurrentMember.Level.Name = "Year",
> sum([My Time].CurrentMember.Children, [Measures].[New[/vbc
ol]
Calculated[vbcol=seagreen]
> Measure]), <-- Error here
> sum([My Time].CurrentMember.Children, [Measures].[
Distinct
> Products])
> )
> )
>
> What I am trying to do is to count distinctly for days only, for weeks it
> should aggregate the days distinct count and for years it should aggregate
> the weeks sum. The calculated measure is simply called "New Calculated
> Measure"
> Can this be done?
>
> count(distinct(<measure to count> ), exlcudeempty)
>
> "Ohjoo Kwon" <ojkwon@.olap.co.kr> wrote in message
> news:OWnHDMWLFHA.2796@.tk2msftngp13.phx.gbl...
but[vbcol=seagreen]
count[vbcol=seagreen]
all[vbcol=seagreen]
52[vbcol=seagreen]
about[vbcol=seagreen]
>|||thanks a lot Ohjoo, you've been of great assistances...
"Ohjoo Kwon" <ojkwon@.olap.co.kr> wrote in message
news:%23jpmrEcLFHA.2136@.TK2MSFTNGP14.phx.gbl...
> Next is simpler.
> IIF(Time.CurrentMember.Level.Name = "Day",
> [Distinct Products],
> Sum(Time.CurrentMember.Children, [New Calculated Measure])
> )
> Ohjoo
>
> "Nestor" <n3570r@.yahoo.com> wrote in message
> news:OT2O0gbLFHA.1156@.TK2MSFTNGP09.phx.gbl...
> the
> Calculated
> but
> count
> all
> 52
> about
>

Monday, February 20, 2012

Problem using aggregation in MS SQL

Hi,
I would like to get an aggregation from several different tables, but don't know how to get. I have tried many different options, but no success. Hopefully someone here can help me out?

The setting is:

Table 1(actually a view) - contains a list of persons
P_id
A
B
C

Table2 - A log of posts the persons have made during a day
P_id; Post
A; 1
A; 3
B; 1

Table3 - A log of orders the persons have made during a day
P_id; Orders
A; 2
B; 2
C; 1

So, I want to loop through all Persons in table 1, and count their "activites" shown in table2 and table3 (and 4,5,6 etc, I have 7 tables). The result should be:

P_id; Count(posts); Count(Orders)
A; 2; 2
B; 1; 1
C; 0; 1

Anyone knows how to achieve this?

Thanks,
incubeme

Quote:

Originally Posted by incubeme

Hi,
I would like to get an aggregation from several different tables, but don't know how to get. I have tried many different options, but no success. Hopefully someone here can help me out?

The setting is:

Table 1(actually a view) - contains a list of persons
P_id
A
B
C

Table2 - A log of posts the persons have made during a day
P_id; Post
A; 1
A; 3
B; 1

Table3 - A log of orders the persons have made during a day
P_id; Orders
A; 2
B; 2
C; 1

So, I want to loop through all Persons in table 1, and count their "activites" shown in table2 and table3 (and 4,5,6 etc, I have 7 tables). The result should be:

P_id; Count(posts); Count(Orders)
A; 2; 2
B; 1; 1
C; 0; 1

Anyone knows how to achieve this?

Thanks,
incubeme


TRY

select P_id, postings.PostCount, orders.OrderCount
from Table1
left join (select P_id, count(*) PostCount from table2) as postings on table1.p_id = postings.p_id
left join (select P_id, count(*) OrderCount from table3) as Orders on table1.p_id = Orders.p_id

...AND SO ON ...|||Thanks for reply. I tried it out, but got the following mesages:

Column 'table2.P_id' is invalid in the select list because it is not contained in an aggregate function and there is no GROUP BY clause.

Column 'table3.P_id' is invalid in the select list because it is not contained in an aggregate function and there is no GROUP BY clause.

When I tried to add: group by table2.P_id, I just got:
The column prefix 'table2.P_id' does not match with a table name or alias name used in the query.

Any clues?|||

Quote:

Originally Posted by incubeme

Thanks for reply. I tried it out, but got the following mesages:

Column 'table2.P_id' is invalid in the select list because it is not contained in an aggregate function and there is no GROUP BY clause.

Column 'table3.P_id' is invalid in the select list because it is not contained in an aggregate function and there is no GROUP BY clause.

When I tried to add: group by table2.P_id, I just got:
The column prefix 'table2.P_id' does not match with a table name or alias name used in the query.

Any clues?


yes, it's my fault :)

now, try this:

select P_id, postings.PostCount, orders.OrderCount
from Table1
left join (select P_id, count(*) PostCount from table2 group by p_id) as postings on table1.p_id = postings.p_id
left join (select P_id, count(*) OrderCount from table3 group by p_id) as Orders on table1.p_id = Orders.p_id

...and so on...|||That did the trick! :-) :-) :-)

Thank you very much for your help