Solved

# SQL Query

Posted on 2012-08-15
683 Views
Last Modified: 2012-08-15
Can anyone provide me with suggestions for combining the two SQL Queries so i will get 5 Columns and the information will match for each day?

---Spots Encoded Daily
Select Count(I.InhouseID) As 'SPOTS Enc',
DATEPART(yy, IMCC.EncodeDateTime) As 'YEAR',
DATEPART(m, IMCC.EncodeDateTime) As 'MONTH',
DATEPART(dd, IMCC.EncodeDateTime) As 'DAY'

from dbo._Inhouse I Join dbo._InhouseMCC IMCC
ON I.InHouseID = IMCC.InHouseID

Where IMCC.EncodeDateTime BETWEEN '08-05-2012' AND '08-16-2012'
AND (DATEDIFF(DAY,I.FirstAirDate,DATEADD(dd,1,IMCC.EncodeDateTime))= 0)
Group By DATEPART(yy, IMCC.EncodeDateTime),
DATEPART(m, IMCC.EncodeDateTime),
DATEPART(dd, IMCC.EncodeDateTime)

--- Count for Spots Encoded After 3
(Select Count(I.InhouseID) As 'SPOTS Enc > 3'
from dbo._Inhouse I Join dbo._InhouseMCC IMCC
ON I.InHouseID = IMCC.InHouseID
Where IMCC.EncodeDateTime BETWEEN '08-05-2012' AND '08-16-2012' And DATEPART(hh, IMCC.EncodeDateTime) Between 14 and 23
AND (DATEDIFF(DAY,I.FirstAirDate,DATEADD(dd,1,IMCC.EncodeDateTime))= 0)
Group By DATEPART(yy, IMCC.EncodeDateTime),
DATEPART(m, IMCC.EncodeDateTime),
DATEPART(dd, IMCC.EncodeDateTime))
0
Question by:SpotIT
2 Comments

LVL 13

Accepted Solution

Would something like this work?

Select Count(I.InhouseID) As 'SPOTS Enc',
SUM(CASE WHEN DATEPART(hh, IMCC.EncodeDateTime) Between 14 and 23 THEN 1 ELSE 0 END) As 'SPOTS Enc > 3',
DATEPART(yy, IMCC.EncodeDateTime) As 'YEAR',
DATEPART(m, IMCC.EncodeDateTime) As 'MONTH',
DATEPART(dd, IMCC.EncodeDateTime) As 'DAY'

from dbo._Inhouse I Join dbo._InhouseMCC IMCC
ON I.InHouseID = IMCC.InHouseID

Where IMCC.EncodeDateTime BETWEEN '08-05-2012' AND '08-16-2012'
AND (DATEDIFF(DAY,I.FirstAirDate,DATEADD(dd,1,IMCC.EncodeDateTime))= 0)
Group By DATEPART(yy, IMCC.EncodeDateTime),
DATEPART(m, IMCC.EncodeDateTime),
DATEPART(dd, IMCC.EncodeDateTime)

This is just a wild guess, but looking at your table structure would help...
0

Author Closing Comment

Thanks for the help!
0

## Join & Write a Comment Already a member? Login.

In today’s complex data management environments, it is not unusual for UNIX servers to be dedicated to a particular department, purpose, or database.  As a result, a SAS® data analyst often works with multiple servers, each with its own data storage…
Read about achieving the basic levels of HRIS security in the workplace.
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

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

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

#### Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!