?
Solved

(SQL) Retrieve Total results in the separate rows...

Posted on 2016-10-12
3
Medium Priority
?
57 Views
Last Modified: 2016-10-13
Hi guys,

I have script which retrieve total results of two different transformation in one row for provided day

DECLARE @Tbl as TABLE (RECEIVED int, [SENT] int)	
BEGIN
		INSERT into @Tbl Select 
		[RECEIVED]		=   CASE WHEN transformId	= 21 then transformId END,
		[SENT]			=   CASE WHEN transformId	= -2 then transformId END	
     // This is date format in the table
		FROM Documents WHERE dateAdded between '2016-10-12 05:00:00.000'  and '2016-10-12 22:30:30.000'   
END
SELECT count([RECEIVED])as [TOTAL RECEIVED],count([SENT]) AS [TOTAL SENT] FROM @Tbl

Open in new window

results:
TOTAL RECEIVED      TOTAL SENT
3163                       3182



Now I need to retrieve results of last 7 days. Results should be of 7 past days, including provided day. And transformation id's totals in the separated row for every day.

Expecting results table:
DATE                TOTAL RECEIVED          TOTAL SENT
2016-10-12       3163                          3182
2016-10-11       2162                          1182
2016-10-10       3112                          2182
2016-10-09       3132                          1182
2016-10-08       3143                          2182
2016-10-07       3145                          182
2016-10-06       3167                          82



This is defiantly incorrect and gives zero values and providing in one row for all week...

DECLARE @Tbl		 as TABLE (RECEIVED int, [SENT] int)	
BEGIN
		INSERT into @Tbl Select 
            //  [Date]                      ==
		[RECEIVED]		=   CASE WHEN transformId	= 21 then transformId END,
		[SENT]			=   CASE WHEN transformId	= -2 then transformId END		
// this is wrong way to used and will gives results for 7 days in total ???
		FROM Documents WHERE dateAdded =  DATEDIFF(DD, cast('2016-10-12 00:00:00.000' as nvarchar), -6) 
END
SELECT count([RECEIVED])as [TOTAL RECEIVED],count([SENT]) AS [TOTAL SENT] FROM @Tbl

Open in new window


Any ideas? Thank you!
0
Comment
Question by:Darius
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 

Author Comment

by:Darius
ID: 41841083
Every day have default time:
'2016-10-12 05:00:00.000'  and '2016-10-12 22:30:30.000'
'2016-10-11 05:00:00.000'  and '2016-10-11 22:30:30.000'
'2016-10-10 05:00:00.000'  and '2016-10-10 22:30:30.000'
. . .
. . .
0
 
LVL 41

Accepted Solution

by:
Sharath earned 2000 total points
ID: 41841317
Can you try like this?
SELECT CONVERT(DATE, dateAdded) dateAdded,
       COUNT(CASE WHEN transformId = 21 then transformId END) [RECEIVED],
       COUNT(CASE WHEN transformId = -2 then transformId END) [SENT]
  FROM Documents
 WHERE dateAdded >= CONVERT(DATE,DATEADD(dd, -7, GETDATE()))
 GROUP BY CONVERT(DATE, dateAdded)

Open in new window

0
 

Author Comment

by:Darius
ID: 41841477
Sharath,
  Thank You!
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question