nested select statement with SUM - sql

I would like to know if its possible to write a nested select statment?
I have the following which calculates the booked time:
SELECT description, SUM(ts.booked_time) AS booked_time_total,
CONVERT(VARCHAR(11), #testDate, 106) AS month_name, #week_ref AS week_ref
FROM timesheets ts
WHERE #testDate <= convert(datetime, end_dtm, 120) and
dateadd(wk, 1, #testDate) > convert(datetime, start_dtm, 120)
But the booked time appears to be wrong. Isnt the SUM supposed to calculate the total for each row which are within the start_dtm and end_dtm. So if I have 10 rows with 1 in the booked time you would expect the SUM to be 10.
test data:

SUM calculates the total value of the fields while COUNT is the total number of records.
SELECT description,
COUNT(ts.booked_time) AS booked_time_total,
CONVERT(VARCHAR(11), #testDate, 106) AS month_name,
#week_ref AS week_ref
FROM timesheets ts
WHERE #testDate <= convert(datetime, end_dtm, 120) and
dateadd(wk, 1, #testDate) > convert(datetime, start_dtm, 120)

I think you are looking to use COUNT rather than SUM.

Related

How to convert sql dates range to Daily Row

How do I convert a date range so each day is 1 row with the start and end time for that day?
I looked at this post about date ranges to row - but this is a different problem. The other solution linked above does not give the time from start to finish for each day - and thus does not allow for duty factor or utilization calculations, and or the build of a Gantt chart.
We would have an ID field, a Start Date, and an End Date as our base table. We want to convert this to contain the ID Field per day with how much time was consumed in that range.
This is very useful when converting a start and end dates to daily duty factor and a host of other needs like equipment utilization.
I had a lot of help from this community figuring this out. I wanted to put the final SQL script here for others to use as well.
WITH cte ([VID],[StartTime],[EndTime]) AS
( SELECT tbl.[ID] as 'VID'
,CONVERT(VARCHAR(19), tbl.[StartDT], 120) AS 'StartTime'
,CASE
WHEN tbl.[EndDT] <= CONVERT(VARCHAR(11), tbl.[StartDT]+1, 120) + '00:00:00' THEN CONVERT(VARCHAR(19), tbl.[EndDT], 120)
ELSE CONVERT(VARCHAR(11), tbl.[StartDT]+1, 120) + '00:00:00'
END as 'EndTime'
FROM [SourceTable] as tbl
WHERE DATEDIFF(DAY,tbl.[StartDT],tbl.[EndDT] )<=365
UNION ALL
SELECT tbl.[ID] as 'VID'
,CONVERT(VARCHAR(11), DATEADD(DAY, 1, cte.[StartTime]), 120) + '00:00:00' AS 'StartTime'
,CASE
WHEN CONVERT(VARCHAR(19), tbl.[EndDT], 120) < CONVERT(VARCHAR(11), DATEADD(DAY, 2, cte.[StartTime]), 120) + '00:00:00'
THEN CONVERT(VARCHAR(19), tbl.[EndDT], 120)
ELSE CONVERT(VARCHAR(11), DATEADD(DAY, 2, cte.[StartTime]), 120) + '00:00:00'
END AS 'EndTime'
FROM cte
INNER JOIN [SourceTable] as tbl
ON cte.VID = tbl.ID
WHERE CONVERT(VARCHAR(11), cte.[StartTime], 120) < CONVERT(VARCHAR(11), tbl.[EndDT], 120))
SELECT VID AS ID
,[StartTime]
,[EndTime]
,DateDiff (second,[StartTime],[EndTime]) / 3600 As 'Hours'
,DateDiff (second,[StartTime],[EndTime])/60 % 60 as 'Minutes'
,((DateDiff (Second,[StartTime],[EndTime]) / 3600)*60)+(DateDiff (second,starttime,endtime)/60 % 60) as 'Total Minutes'
,DATEPART(week,[StartTime]) AS weeknum
,MONTH([StartTime]) AS MonthNumber
,DATENAME(month,[StartTime]) AS MonthName
FROM cte order by Id, [StartTime]
option (maxrecursion 0);

Grouping by DATEPART(hh,TimeStamp) to show nightshift, how do I see data for hours past midnight?

I'm importing my data to excel and so I need to see the date as a varchar to use for a graph in excel but I also need the data for the individual hours in the day as well. My manager wants to see data for the past hour whenever he checks my chart. This is my code so far. Dayshift has been fine but I can't get the hours to go past 24 for nightshift so I can't group them in my graph in excel.
convert(VARCHAR, TimeStamp, 101) as date
,StationID as lane
,DATEPART(hh,TimeStamp)
,.185 as posSD1
,-.185 as negSD1
,.370 as posSD2
,-.370 as negSD2
,.556 as posSD3
,-.556 as negSD3
, COUNT (TrickleActual) as Count
, convert(decimal (18,3) ,AVG (TrickleActual - TrickleTarget)) as Average
FROM CherryBoxInfo
WHERE TimeStamp >= '2015-05-01' -- '2015-05-01 18:30:00'
and TimeStamp between convert(DATETIME, convert(VARCHAR, TimeStamp, 101) + ' ' + '19:00:00') and convert(DATETIME, convert(VARCHAR, DATEADD(day, 1, TimeStamp), 101) + ' ' + '04:30:00')
and (TrickleActual-TrickleTarget) BETWEEN -1 and 1
GROUP BY
convert(VARCHAR, TimeStamp, 101)
,StationID
,DATEPART(hh,TimeStamp)
ORDER BY convert(VARCHAR, TimeStamp, 101)
,StationID
,DATEPART(hh,TimeStamp)
Instead of grouping on datepart, group on date truncated to hour.
Here is how to truncate a date:
select dateadd(hh, datediff(hh, 0, #dt), 0)

how can I get last 7 days of data being my date field a number?

I need to get last 7 days data excluding Sunday being my date field a number. How can I do it? Field structure 20140425. For example is I run the statement today it should give me date range between 20140424 - 20140417 excluding 20140420.
The hitch is of course converting the number based date to a real date. This seems to work:
select convert(datetime, convert(char(10), 20140425))
To expand, your query would look like this:
select *
from [Table]
where convert(datetime, convert(char(10), [columnname])) between convert(varchar, getdate() - 8, 101) and convert(varchar, getdate() - 1, 101)
and datepart(DW, convert(datetime, convert(char(10), [columnname]))) <> 1
The convert(varchar, getdate - 1, 101) will return you 12:00am yesterday morning. My first pass didn't include that and would've only given a 6 day range.
SELECT *
FROM Table_Name
WHERE CAST(DateField AS DATE) >= DATEADD(DAY, -7, GETDATE())
AND CAST(DateField AS DATE) <= GETDATE()
AND DATEPART(DW,CAST(DateField AS DATE)) <> 1

T-SQL select daily totals BUT daily cut off is not midnight

I need to count number of sales, and sum the totals of sales by date, easy. But the curve pitch is - I need the "cut off" to be 6pm not midnight.
6pm the day before until 6pm day of.
What's throwing me is "grouping". My counts are only pulling only the true date not "6pm" info.
Sort30 Day30 Total Counter
-------- ---------- --------------------- -----------
20120810 08/10/2012 675.32 9
20120809 08/09/2012 1314.68 16
Query:
SELECT top 30 CONVERT(VARCHAR(8), chickendate, 112) AS varSort30,
CONVERT(VARCHAR(10), chickendate, 101) AS varDay30,
SUM(CAST(transAmount AS money)) AS varTotal,
Count(chickendate) AS varCounter
FROM CHICKEN
WHERE
(chickendate >= dateadd(hour, 18, dateadd(day, datediff(day, 0, chickendate), -1)) AND
chickendate < dateadd(hour, 18, dateadd(day, datediff(day, 0, chickendate), 0)))
GROUP BY CONVERT(VARCHAR(8), chickendate, 112),
CONVERT(VARCHAR(10), chickendate, 101)
ORDER BY CONVERT(VARCHAR(8), chickendate, 112) DESC
Going round and round, I feel its something staring me in the face. Thanks.
If my logic is correct, this should give you the correct results:
SELECT TOP 30
CONVERT(VARCHAR(8), modifiedChickenDate, 112) AS varSort30,
CONVERT(VARCHAR(10), modifiedChickenDate, 101) AS varDay30,
SUM(CAST(transAmount AS money)) AS varTotal,
COUNT(modifiedChickenDate) AS varCounter
FROM (
SELECT
transAmount,
DATEADD(HOUR, 6, chickendate) AS modifiedChickenDate
FROM CHICKEN
) sub
GROUP BY
CONVERT(VARCHAR(8), modifiedChickenDate, 112) AS varSort30,
CONVERT(VARCHAR(10), modifiedChickenDate, 101) AS varDay30,
ORDER BY
CONVERT(VARCHAR(8), modifiedChickenDate, 112) AS varSort30
If chickendate is a DATETIME instead of just a DATE, then you can use
GROUP BY CONVERT(VARCHAR(10), DATEADD(hh, 6, chickendate), 101)
to advance the date 6 hours (making your cutoff 6 hours earlier than midnight, or 6pm), then group on the Day of year. This only works if you're storing time information, which I'm not sure you are. Post some schema for the necessary tables. But I think you're looking for...
SELECT TOP 30 CONVERT(VARCHAR(10), DATEADD(hh, 6, chickendate), 101) as Date
, SUM(CAST(transAmount as money)) AS Total
, Count(*) as Counter
FROM Chicken
GROUP BY CONVERT(VARCHAR(10), DATEADD(hh, 6, chickendate), 101)
ORDER BY CONVERT(VARCHAR(10), DATEADD(hh, 6, chickendate), 101) DESC
How about:
GROUP BY CONVERT(VARCHAR(8), dateadd(hour, 6, chickendate), 112)
So '2012-08-09 18:00:00' is grouped by 20120810, and '2012-08-09 17:59:59' is grouped by 20120809.

How do I group DATE field by YEAR-MM in SQL Server?

I have a date field in a query and I do want to get GROUP BY report like this:
DATE COUNT
2010-01 10
2010-02 2
...
2010-12 24
2012-13 34
What is the proper syntax to obtain this on SQL Server?
All these conversions to string work, but I find this method more efficient, albeit less readable:
SELECT m = DATEADD(MONTH, DATEDIFF(MONTH, 0, [DATE]), 0), COUNT(*)
FROM dbo.TheTable
GROUP BY DATEADD(MONTH, DATEDIFF(MONTH, 0, [DATE]), 0);
If you don't want to repeat the expression, then:
;WITH x AS (SELECT m = DATEADD(MONTH, DATEDIFF(MONTH, 0, [DATE]), 0)
FROM dbo.TheTable)
SELECT m, COUNT(*)
FROM x GROUP BY m;
This way the output is still a date/time value and can be used that way for other things. And it doesn't involve any messy string conversions.
CONVERT(VARCHAR(7), CreationDate, 120) as Date
You can simply do:
select convert(char(7), #myDate, 20)
Example
declare #myDate as DateTime
set #myDate = '2012-06-23'
select convert(char(7), #myDate, 20)
Output
-------
2012-06
So the full statement would look like:
select convert(char(7), myDate, 20), count(*) as Count
from MyTable
group by convert(char(7), myDate, 20)
Update
The sample data includes the value 2012-13. I am going to assume this is a typo and that the number after the dash represents the month.
SELECT CAST(DATEPART(year, dateCol) as VARCHAR) + '-' + CAST(DATEPART(month, dateCol) as VARCHAR) as Date, count(*) As Count
FROM myTable
GROUP BY CAST(DATEPART(year, dateCol) as VARCHAR) + '-' + CAST(DATEPART(month, dateCol) as VARCHAR)