Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Need help with grouping two columns together

Author  Topic 

panthagani
Yak Posting Veteran

58 Posts

Posted - 2010-11-04 : 11:17:10
Here is my table:
CREATE TABLE RA_AUDIT_RESULTS
(
SNO INT IDENTITY (1,1),
AEVENTID INT,
NUSERUID INT,
NID INT,
DCHANGETIME DATETIME,
TEVENTTEXT NVARCHAR (2000)
)

Inserted sample data:
INSERT INTO RA_AUDIT_RESULTS VALUES(39448,5226,101620, getdate(), 'A')
INSERT INTO RA_AUDIT_RESULTS VALUES(39449,5226,101620, getdate(), 'B')
INSERT INTO RA_AUDIT_RESULTS VALUES(39450,5226,101620, getdate(), 'C')
INSERT INTO RA_AUDIT_RESULTS VALUES(39451,5226,101620, getdate(), 'D')
INSERT INTO RA_AUDIT_RESULTS VALUES (39452,5226,101629, getdate(), 'E')
INSERT INTO RA_AUDIT_RESULTS VALUES(39453,5226,101629, getdate(), 'F')
INSERT INTO RA_AUDIT_RESULTS VALUES(39454,5226,101629, getdate(), 'G')

For each combination of NUSERID and NID, I need to increase a "transaction" by 1. So, NUSERID = 5227 AND NID = 101630 is counted as "one transaction". Multiple combinations of NUSERID and NID should ALSO be counted as one "transaction". So
NUSERID = 5226 AND NID = 101620 is counted as one transaction. NUSERID = 5226 AND NID = 101629 is counted as one transaction. Overall, there are three "transactions" in this result set. Hope I was able to convey what I am looking for.

Please get me started on the SQL query. Thanks!

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-11-04 : 11:49:11
Thank you very much for providing DDL and sample data!
Now, to make your problem more clear, it would be fine if you could post the wanted output.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

panthagani
Yak Posting Veteran

58 Posts

Posted - 2010-11-04 : 12:21:45
Thank you for the response. Here is the output:

5226 2

Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-11-04 : 12:29:29
I'll be back...


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-11-04 : 12:41:32
SELECT
NUSERUID,
count(distinct NID)
FROM RA_AUDIT_RESULTS
GROUP BY NUSERUID


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -