Welcome to dbForumz.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Grouping records from 2 (or more) different tables

 
   Database Forums (Home) -> MSEQ RSS
Next:  SQL 2000 DTS FTP task  
Author Message
Raf

External


Since: Nov 26, 2007
Posts: 8



(Msg. 1) Posted: Fri Dec 07, 2007 5:07 am
Post subject: Grouping records from 2 (or more) different tables
Archived from groups: microsoft>public>sqlserver>mseq (more info?)

Hello,

I log all actions of the users in a table. Each day, I create this 'logtable'.
It looks something like this:
Tablename: 20071207 (for logs of dec 07, 2007)
Recnr
Userid
ActionID

Now, i want to create statistics on the actions that users perform.
If I only use 1 table, there is no problem: I use the Group by statement and
the job is done. The problems start when I want to create statistics on more
than one table. I think I should use the union (or union all) statement, but
I just can't get it working combined with the group by statement.

Can anyone help me please.

Regards,

Raf

 >> Stay informed about: Grouping records from 2 (or more) different tables 
Back to top
Login to vote
Hugo Kornelis

External


Since: Jan 31, 2006
Posts: 335



(Msg. 2) Posted: Fri Dec 07, 2007 7:58 pm
Post subject: Re: Grouping records from 2 (or more) different tables [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Fri, 7 Dec 2007 05:07:01 -0800, Raf wrote:

>Hello,
>
>I log all actions of the users in a table. Each day, I create this 'logtable'.
>It looks something like this:
>Tablename: 20071207 (for logs of dec 07, 2007)
> Recnr
> Userid
> ActionID
>
>Now, i want to create statistics on the actions that users perform.
>If I only use 1 table, there is no problem: I use the Group by statement and
>the job is done. The problems start when I want to create statistics on more
>than one table. I think I should use the union (or union all) statement, but
>I just can't get it working combined with the group by statement.

Hi Raf,

It appears that you have just found out why it is not a good idea to
create a new table for each day Smile
The recommended method is to use one table, and to have the current date
as an extra column (often also part of the primary key) in that table.

If your table is extremely large (and note that a few million rows is
not extremely large for SQL Server), than you might wish to look into
table partitioning (SQL Server 2005 only - a feature that allows you to
divide a table into logical partitions that are handled seperately) or
into partitioned views (all versions - a feature that allows you to
create seperate tables and one view that combines the data).

For a quick fix until you have time to reallly fix the issues, you can
use either

SELECT ....
FROM (SELECT Userid, ActionID, CAST('20071207') AS TheDate
FROM [20071207]
UNION ALL
SELECT Userid, ActionID, CAST('20071208') AS TheDate
FROM [20071208]) AS d
GROUP BY ....;

You can also try if you get better performance by pre-aggregating before
union'ing, for example

SELECT ActionID, SUM(CountPerDay) AS TotalCount
FROM (SELECT ActionID, COUNT(*)
FROM [20071207]
GROUP BY ActionID
UNION ALL
SELECT ActionID, COUNT(*)
FROM [20071208]
GROUP BY ActionID) AS d
GROUP BY ActionID;

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis

 >> Stay informed about: Grouping records from 2 (or more) different tables 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Grouping by Distinct - Hi, I have 2 columns of data, one has an Agent code and the other has information about the Agent. There are duplicates of the Agent code in the 1st column, but different info in the second. For example: Col 1 Col 2 Agent 1 Data 1 Agen...

Fixed length records - I have to build a text file that has 80 character records. I have a number of fixed length fields in the file but need to add a 'filler' at the end of the fields to get to 80. Do I have to create a dummy variable to do that and if so, how? Thanks -- ...

Problem with select every TOP 1 records from different gro.. - Hi Expert, I came across a situation where the query result returns all non-NULL records even I use TOP 1 statement. Here is the SQL statement: SELECT CONTRACT_NUMBER, STEP, STATUS_END_DATE, CIS_PK FROM dbo.[VIEW1] T1 WHERE ..

SELECT DISTINCT records based only on two columns - I'm working on a mailing list and want to select records where the both the SSN number and Address fields are unique, since I may want to send a piece of mail to more than one address for a person, but not multiple pieces to the same address for that....

GROUP BY's on 3 tables in one SELECT? - Hi there, I'd like to ask you for help with following: having 3 tables: T1 (Person_ID, Product_ID, Costs) T2 (Person_ID, Product_ID, Balancies) T3 (Product_ID, Product_Type) I have simple GROUP BY query: SELECT T1.Person_ID, T1.Product_ID, MAX(Costs)..
   Database Forums (Home) -> MSEQ All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]