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
 SQL STATEMENT

Author  Topic 

Sandeep42
Starting Member

1 Post

Posted - 2015-05-07 : 08:22:43
Sir

I want to count the values of 2 field on particular date

I have user table with 3 field
1.from_userid
2.to_userid
3.Date

Now i want to count the no. of files from_userid have send, no. of files to user_id have received on particular day.
The data is like this

From_userid to_userid date
3953 6274 10/22/2014
3953 6152 10/22/2014
1112 2710 10/22/2014
3953 1851 10/23/2014
3953 4302 10/23/2014
4302 2710 10/23/2014

pl. help how to write sql command for this.

Sandeep

Test

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2015-05-07 : 08:52:53
not clear what you're need here.

Using the data you posted, what should the results be?

Gerald Britton, MCSA
Toronto PASS Chapter
Go to Top of Page

Ifor
Aged Yak Warrior

700 Posts

Posted - 2015-05-07 : 09:01:18
Maybe:

-- *** Test Data ***
-- Please pprovide consumable test data in future
CREATE TABLE #t
(
From_UserId int NOT NULL
,To_UserID int NOT NULL
,[date] date NOT NULL
);
INSERT INTO #t
VALUES (3953, 6274, '20141022')
,(3953, 6152, '20141022')
,(1112, 2710, '20141022')
,(3953, 1851, '20141023')
,(3953, 4302, '20141023')
,(4302, 2710, '20141023');
-- *** End Test Data ***

WITH SentFiles
AS
(
SELECT [date], From_UserId, COUNT(*) AS Files
FROM #t
GROUP BY [date], From_UserId
)
,ReceivedFiles
AS
(
SELECT [date], To_UserID, COUNT(*) AS Files
FROM #t
GROUP BY [date], To_UserID
)
SELECT COALESCE(S.[date], R.[date]) AS [date]
,COALESCE(S.From_UserId, R.To_UserID) AS UserID
,COALESCE(S.Files, 0) AS FilesSent
,COALESCE(R.Files, 0) AS FilesReceived
FROM SentFiles S
FULL JOIN ReceivedFiles R
ON S.[date] = R.[date]
AND S.From_UserId = R.To_UserID
ORDER BY [date], UserID;
Go to Top of Page
   

- Advertisement -