473,399 Members | 3,603 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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
Jul 31 '07 #1
4 1763
ck9663
2,878 Expert 2GB
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 ....
Jul 31 '07 #2
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?
Jul 31 '07 #3
ck9663
2,878 Expert 2GB
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...
Jul 31 '07 #4
That did the trick! :-) :-) :-)

Thank you very much for your help
Aug 1 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Merlin | last post by:
Hi Imagine the following classes (A class diagram will help) BASE, A, B, C, D, E, F, G. A, B, C, D, G inherit from BASE. E, F inherit from D.
12
by: Bing Wu | last post by:
Hi, Has anyone got problem with this on large table. It seems it take ages to return the result ( half an hour :-( ). SELECT COUNT(*) FROM COORDINATE A strange thing is the tempory space...
4
by: cmrchs | last post by:
Hi, how do I implement aggregation and how composition in C# ? When I say : an Airplane has a Pilot then I use aggregation but when I say : an Airplane has a Cockpit then I use composition. How...
2
by: Jozsef Bekes | last post by:
Hi, I would like to implement aggregation in C#, therefore I'd need to implement the queryinterface COM function of a class. I am not sure whether this can be done, and if yes where to start. If...
4
by: Frederik Vanderhaegen | last post by:
Hi, Can anyone explain me the difference between aggregation and composition? I know that they both are "whole-part" relationships and that composition parts are destroyed when the composition...
5
by: Nice Chap | last post by:
Aggregation in COM was defined as 'Exposing an interface of an inner object by the outer object as though it were an interface of the outer object'. Is this type of aggregation possible in vb.net(...
23
by: SenthilVel | last post by:
Hi Can any one let me know the websites/Pdf for learning Aggragation in C#?? Thanks Senthil
1
by: ninjutsu28 | last post by:
hi im juz a student so pls bear with my terminologies..juz wana ask how to perform aggregation in vb.net code.. in my model, i have rectangle, shape, drawing classes..rectangle class inherits...
7
by: Bruce One | last post by:
In C#, how would people implement a relationship between Customer class and Request class, considering a customer may have 0-n requests and a request must belong to 1 and only 1 customer...? ...
0
by: Karigar | last post by:
I have been so far developing COM servers and clients in C++. I am new to C#/NET way of doing COM and was wondering if it is possible to accomplish aggregation in .NET platform. By aggregation I...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.