Solved

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

Posted on 2016-10-12
3
56 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 500 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
I have a large data set and a SSIS package. How can I load this file in multi threading?
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

724 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