HOURLY SALES REPORT - sql-server

Good morning guys.
I need your help again.
Im doing a sales report but I cant get the output I want.
My code is this:
Select Cast(TIME As Varchar(100))+ '-' + CAST(DATEADD( MINUTE, 59, TIME)As varchar(100)) As TIME, SUM (TOTAL) as HOURLY_SALES
From tblSales
Where Mall = 'Mall1' and ORDATE = '6/2/2014' and VOID = 'N'
Group By OTIME
and its given me this output:
Time HOURLY_SALES
12:00-Jan 1 1900 12:59PM 295.00
13:00-Jan 1 1900 1:59PM 2122.86
14:00-Jan 1 1900 2:59PM 2230.00
15:00-Jan 1 1900 3:59PM 1800.00
16:00-Jan 1 1900 4:59PM 3090.00
17:00-Jan 1 1900 5:59PM 880.00
18:00-Jan 1 1900 6:59PM 652.86
19:00-Jan 1 1900 7:59PM 1890.00
20:00-Jan 1 1900 8:59PM 2272.86
21:00-Jan 1 1900 9:59PM 520.00
I dont know where the date come from.
Please help me to remove the date.
The output shoulbe like this:
Time HOURLY_SALES
12:00- 12:59PM 295.00
13:00- 1:59PM 2122.86
14:00- 2:59PM 2230.00
15:00- 3:59PM 1800.00
16:00- 4:59PM 3090.00
17:00- 5:59PM 880.00
18:00- 6:59PM 652.86
19:00- 7:59PM 1890.00
20:00- 8:59PM 2272.86
21:00- 9:59PM 520.00
I'm using visual studio 2010 and ms sql server 2008
Hope you'll help me.
Thank you very much.

How about doing this instead?
select (cast(datepart(hour, [time]) as varchar(255)) + ':00 -' +
cast(datepart(hour, [time]) as varchar(255)) + ':59'
) as [time],
SUM (TOTAL) as HOURLY_SALES
From tblSales
Where Mall = 'Mall1' and ORDATE = '6/2/2014' and VOID = 'N'
Group By cast(datepart(hour, [time]) AS VARCHAR(255));

Related

Retrieve data 14 days prior to their "date of manufacture" irrespective of Year in SQL Server

I need all the rows that are 14 days next to my current date and that should not depend upon the Year. For example if today is 2nd of September, then the query should return all the rows of which u_dateofmanufacture is (2+14) 16th September, no matter which year it belongs. the focus is only on the date and the month.
I have also attached the screenshot and the column "date of Manufacture" is highlighted.
According to the screenshot only the rows 2,3 and 4 should be returned. They all have different year but the day and month is same(09-16).
I am using SQL Server.
declare #date DATE = dateadd(d,14,getdate())
select * from your table where month(u_dateofmanufacture)= month(#date) and day(u_dateofmanufacture) = day(#date)
You have a problem with leap years and the first 14 days of the year. If we skip these problems, you can use:
where month(datecol + 14) * 100 + day(datecol + 14) >= month(getdate()) * 100 + day(getdate()) and
month(datecol) * 100 + day(datecol) < month(getdate()) * 100 + day(getdate())
I would use a join on a virtual table containing all the possible years
SELECT t.*
FROM YourTable t
JOIN (
SELECT
starting = DATEADD(day, -14, DATEFROMPARTS(v.year, MONTH(GETDATE(), DAY(GETDATE())),
ending = DATEFROMPARTS(v.year, MONTH(GETDATE(), DAY(GETDATE()))
FROM (VALUES
(2023),(2022),(2022),(2021),(2020),(2019),(2018),(2017),(2016),(2015),(2014),(2013),(2012),(2011)
) v(year)
) v ON t.u_dateofmanufacture >= v.starting
AND t.u_dateofmanufacture < v.ending
you can use a query like below
select * from yourtable
where month(u_dateofmanufacture)= month(dateadd(d,14,getdate()))
and day(u_dateofmanufacture) =day(dateadd(d,14,getdate()))
optimally
declare #seekdate = dateadd(d,14,getdate())
select * from yourtable
where month(u_dateofmanufacture)= month(#seekdate )
and day(u_dateofmanufacture) =day(#seekdate)

Calcuation using SQL Server date for YYYYMM previous month

I am using Microsoft SQL Server Management Studio and I am trying to do the following: I want to make a calculation that looks for the last 2 months and the "current" month.
The month and year are defined as follows:
SELECT
ID,
dc.Year * 100 + dc.MonthOfYear AS YYYMM,
dc.Year * 100 + dc.MonthOfYear - 1 AS PrevMonth,
COUNT(1) AS Count_sales,
SUM(sales) AS TotalSales
FROM
xx (NOLOCK) dc
GROUP BY
dc.Year * 100 + dc.[Month Of Year]
The problem occurs when I have 202101 because the previous month is taken as 202102 any know how to handle when the year changes as well please?
I have tried to using the following but in SQL Server, the add_months function doesn't exists:
Hive SQL Integer YYYYMM previous Months
Any suggestions please?
It will not be the prettiest of code but you can use case here:
select
ID
,dc.Year*100+dc.MonthOfYear as YYYMM
,case when dc.MonthOfYear = 1 then dc.Year-1 else dc.Year end * 100 + dc.MonthOfYear - (case when dc.MonthOfYear = 1 then -11 else 1 end) as PrevMonth
,dc.MonthOfYear-case when dc.MonthOfYear = 1 then -11 else 1 end as PrevMonthXX
,count(1) as Count_sales
,sum(sales) as TotalSales
from xx (nolock) dc
group by dc.Year*100+dc.[Month Of Year]
It is most often easier to save as a regular date and then just take out year and month if you need it seperatly later on. But sometimes you just have do make do with what you have..

Need to save ISO_WEEK and YEAR

I need to split and save ISO_WEEK and YEAR between two dates in a table.
To get that data I use below query.
DECLARE #_start DATE = '2019-01-01'
DECLARE #_end DATE = '2019-12-31'
SELECT DATEPART(YEAR,dateadd(week, number, #_start)) AS YEAR
,DATEPART(ISO_WEEK,dateadd(week, number, #_start)) AS CW
FROM (SELECT x.number FROM master..spt_values x WHERE type = 'P'
AND #_end >= dateadd(week, number, #_start)) date_part
This logic fails on last week of the year where I get CW as 1 and Year as 2019. But actually, CW is 1 and YEAR is 2020.
YEAR CW
----------- -----------
2019 1
2019 2
. .
. .
2019 52
2019 1
Any suggestions to handle this?
Try this
DECLARE #_start DATE = '2019-01-01'
DECLARE #_end DATE = '2019-12-31'
SELECT DATEPART(year, s.Sunday) AS YEAR
, DATEPART(iso_week, s.Sunday) AS CW
FROM (
SELECT DATEADD(week, x.number, DATEADD(day, 7 - (DATEPART(dw, #_start) + ##DATEFIRST - 1) % 7, #_start)) AS Sunday
FROM master..spt_values x
WHERE type = 'P'
) s
WHERE #_end >= s.Sunday
This first offsets #_start to following Sunday and then loop all Sundays from then on.
The idea is that DATEPART(YEAR, ...) for Sundays returns less surprising year for DATEPART(ISO_WEEK, ...)

SQL query to fetch all results in one query

Test data:
id date company location
----------------------------------
1 03/01/2016 ABC india
2 03/25/2016 ABC us
3 02/24/2016 ABC india
4 02/25/2016 ABC us
5 03/02/2016 ABC india
Query #1
select count(id)
from table
where company = 'ABC'
and date between 03/01/2016 and 03/31/2016
Query #2
select count(id)
from table
where company = 'ABC'
and date between 02/01/2016 and 02/29/2016
Need to calculate location wise count for current and previous months.
How to write a SQL query to return location wise like the below in one query?
Expected result:
company currentmonth Previousmonth location
ABC 2 1 india
ABC 1 1 us
Try:
select company,
sum(case when month(date)=month(getdate())
then 1 else 0 end) as currentMonth,
sum(case when month(date)=month(dateadd(MONTH,-1,getdate()))
then 1 else 0 end) as Previuosmonth,
location
from yourTable
where month(date) between month(dateadd(MONTH,-1,getdate())) and month(getdate())
AND company='ABC'
group by company, location
Since you made two queries and filter both with current month (march) and previous. I decided to use the function MONTH to extract the month from the current date (getdate()) of the system and subtract 1 that way you will always have the current month and the previous one.
EDIT
As pointed out by #DhananjayaKuppu I fixed the problem for when you have a month as January, since the month function returns an integer it would return 0 in my calculation. Now it is fixed.
You may try some thing like subtract current date yearmm format (ex: 201603) and yearmm format of date from table (ex:201603) if it result to 0 treat as current month, else previous month, hope it will help:
SELECT company,
location,
sum(iif(d = 0, 0, 1)) cm,
sum(iif(d = 0, 1, 0)) pm
FROM (SELECT company,
location,
CONVERT(INT,
CONVERT(CHAR(4), GetDate(), 120) +
CONVERT(CHAR(2), GetDate(), 101)
) -
CONVERT(INT,
CONVERT(CHAR(4), date, 120) +
CONVERT(CHAR(2), date, 101)
) as d
FROM table) vw
GROUP BY company, location

Calculate total running time from start/stop timestamps

I have a table containing a number of timestamps per day, they represents start and stop events.
ID TimeStamp
----------------------
1 2008-01-01 07:00:00
1 2008-01-01 08:15:00
1 2008-01-01 10:00:00
1 2008-01-01 11:00:00
1 2008-01-02 10:30:00
1 2008-01-02 12:00:00
I would like to calcuate the total running time per day, like this:
ID Date RunningTime
-------------------------
1 2008-01-01 02:15:00
1 2008-01-02 01:30:00
Do anyone have a nice T-SQL solution for my problem?
WITH q AS
(
SELECT *,
CONVERT(DATETIME, CONVERT(VARCHAR(8), TimeStamp, 112), 112) AS dte,
ROW_NUMBER() OVER (PARTITION BY id, CONVERT(DATETIME, CONVERT(VARCHAR(8), TimeStamp, 112), 112) ORDER BY TimeStamp) AS rn
FROM mytable
)
SELECT qb.id, qb.dte, SUM(DATEDIFF(second, qb.TimeStamp, qe.TimeStamp))
FROM q qb
JOIN q qe
ON qe.id = qb.id
AND qe.dte = qb.dte
AND qe.rn = qb.rn + 1
WHERE qb.rn % 2 = 1
GROUP BY
qb.id, qb.dte
This assumes that every record open on a certain day should also be closed on the same day.

Resources