[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2016-10-12
3
Medium Priority
?
58 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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

656 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