group by week, get first day of week, pivot by weekday - sql

I need to make a query that will show how much of each item/unit combination each customer ordered each week (group by week) while showing the first day the week, and also showing the quantity ordered each day of that week (pivot by weekday). So far i have this, but I'm not sure how to group this by week.
SELECT customer_name, item_code, item_desc, unit, delivery_date, [Monday], [Tuesday], [Wednesday], [Thursday], [Friday], [Saturday], [Sunday]
FROM (
SELECT customer_name, item_code , item_desc , unit, delivery_date, DATENAME(dw, delivery_date) AS DayWeek, qty
FROM order_items oi inner join orders on localID = local_order_id
) AS ordersItems
pivot (
SUM(qty) FOR DayWeek IN ([Monday], [Tuesday], [Wednesday], [Thursday], [Friday], [Saturday], [Sunday])
) AS pvt

The following query groups by customer/item/unit/week# combination. It returns the total number of orders as well as the # of orders broken down by the day of the week. The CASE statement is used along with the SUM function to get the total number of orders for each day of the week
EDIT: Revised query to group by Week Starting Date (instead of week # previously)
SELECT customer_name, item_code, item_desc, unit,
CASE SIGN(7-(DATEPART(dw, action)+2))
WHEN -1 THEN CAST(DATEADD(dd, 7-(DATEPART(dw, action)+2), action) AS DATE)
WHEN 0 THEN CAST(action AS DATE)
WHEN 1 THEN CAST(DATEADD(dd, -(DATEPART(dw, action)+2), action) AS DATE)
END Week_Starting_Friday
SUM(qty) Total_Orders,
SUM(
CASE DATENAME(dw, delivery_date) WHEN 'Monday' THEN qty ELSE 0 END
) [Monday],
SUM(
CASE DATENAME(dw, delivery_date) WHEN 'Tuesday' THEN qty ELSE 0 END
) [Tuesday],
SUM(
CASE DATENAME(dw, delivery_date) WHEN 'Wednesday' THEN qty ELSE 0 END
) [Wednesday],
SUM(
CASE DATENAME(dw, delivery_date) WHEN 'Thursday' THEN qty ELSE 0 END
) [Thursday],
SUM(
CASE DATENAME(dw, delivery_date) WHEN 'Friday' THEN qty ELSE 0 END
) [Friday],
SUM(
CASE DATENAME(dw, delivery_date) WHEN 'Saturday' THEN qty ELSE 0 END
) [Saturday],
SUM(
CASE DATENAME(dw, delivery_date) WHEN 'Sunday' THEN qty ELSE 0 END
) [Sunday]
FROM order_items oi inner join orders on localID = local_order_id
GROUP BY customer_name, item_code, item_desc, unit,
CASE SIGN(7-(DATEPART(dw, action)+2))
WHEN -1 THEN CAST(DATEADD(dd, 7-(DATEPART(dw, action)+2), action) AS DATE)
WHEN 0 THEN CAST(action AS DATE)
WHEN 1 THEN CAST(DATEADD(dd, -(DATEPART(dw, action)+2), action) AS DATE)
END
ORDER BY 5, customer_name, item_code, item_desc, unit;
References:
CASE statement on MSDN
DATENAME on MSDN

Related

Calculate MTD using CTE and Projected Sales in SQL

I am trying to add MTD Sales in a SQL Query. I figured out how to do that with JOINS but i want to use a CTE to calculate MTD sales and then use that to calculate projected_sales.Formula for projected sales is (MTD/wkdaysinmonth*wkdaystodate)[which is also stored in CTE Table). Is there a way to make it easy? I wrote the following code;
Input:
Email PaymentAmount orderdate
xyz#gmail.com 10 11/01/2018
xyz#gmail.com 20 11/09/2018
sample output:
EmailAddress MTD Projected_sales
xyz#gmail.com 30 0.19
where Projected sales is calculated as number of days passed=7 and total number of business days in november 22. {[30/7*22]=0.19} (Present date = 11/09/2018)
with dates as(
select dateadd(d,-day(getdate())+1,convert(date,getdate())) as startofmonth,
dateadd(d,-1,dateadd(m,1,dateadd(d,-day(getdate())+1,convert(date,getdate())))) as endofmonth,
convert(date,getdate()) as today
)
,daycounts as(
select dates.*,
(DATEDIFF(dd, startofmonth, endofmonth) + 1)
-(DATEDIFF(wk, startofmonth, endofmonth) * 2)
-(CASE WHEN DATENAME(dw, startofmonth) = 'Sunday' THEN 1 ELSE 0 END)
-(CASE WHEN DATENAME(dw, endofmonth) = 'Saturday' THEN 1 ELSE 0 END) as wkdaysinmonth,
(DATEDIFF(dd, startofmonth, today) + 1)
-(DATEDIFF(wk, startofmonth, today) * 2)
-(CASE WHEN DATENAME(dw, startofmonth) = 'Sunday' THEN 1 ELSE 0 END)
-(CASE WHEN DATENAME(dw, today) = 'Saturday' THEN 1 ELSE 0 END) as wkdaystodate
from dates
)
SELECT DISTINCT Customers.EmailAddress as email,
o1.YTD
FROM
Customers
INNER JOIN
Orders
ON
Orders.CustomerID= Customers.CustomerID
JOIN
(SELECT
c.EmailAddress,
SUM(Orders.PaymentAmount) AS YTD
FROM
Customers c
JOIN
Orders
ON c.CustomerID=Orders.CustomerID
WHERE
Orders.OrderDate BETWEEN '01/01/2018 00:00' AND GETDATE()
GROUP BY
EmailAddress) AS o1 ON o1.EmailAddress = Customers.EmailAddress
WHERE
Orders.OrderDate >= (GETDATE()-7)
You can try to use cte recursive create a calendar table for orderdate startDate to endDate.
Then OUTER JOIN base on calendar table and do condition aggregate function in subquery get workdate.
;WITH cte
AS (SELECT email,
Dateadd(day, 1, Eomonth(Min(orderdate), -1)) minDt,
Dateadd(day, 1, Eomonth(Max(orderdate))) maxDt
FROM t
GROUP BY email
UNION ALL
SELECT email,
Dateadd(day, 1, mindt),
maxdt
FROM cte
WHERE Dateadd(day, 1, mindt) < maxdt),
cte2
AS (SELECT *,
Count(CASE
WHEN Datename(dw, t1.mindt) NOT IN ('Sunday', 'Saturday' )
THEN
1
END) OVER( ORDER BY t1.mindt) workdt
FROM cte t1)
SELECT t1.email,
t2.total,
Max(diffdt) / ( Max(workdt) * Max(workdtmax) * 1.0 ) Projected_sales
FROM (SELECT *,
Max(workdt)
OVER(
partition BY email
ORDER BY workdt DESC) workdtMax,
Datediff(day, Min(mindt) OVER(partition BY email ORDER BY workdt)
, Max(mindt) OVER(partition BY email ORDER BY workdt DESC)) + 1 diffdt
FROM cte2) t1
LEFT JOIN (SELECT email,
Sum(paymentamount) total,
Min(orderdate) minDt,
Max(orderdate) maxDt
FROM t
GROUP BY email) t2
ON t1.mindt BETWEEN t2.mindt AND t2.maxdt
AND t1.email = t2.email
WHERE t2.total IS NOT NULL
GROUP BY t1.email,
t2.total
sqlfiddle
Reuslt
email total Projected_sales
xyz#gmail.com 30 0.19480519480519
You can generate a "calendar" table that has the weekdays for each day in the month.
Your calculation for the projected doesn't make sense to me. So, I've also included what I consider to be a better calculation:
with dates as (
select distinct dte,
(case when datename(weekday, dte) not in ('Saturday', 'Sunday') then 1 else 0 end) as num_weekdays,
dte as month_start
from t cross apply
(values (dateadd(day, 1 - day(orderdate), orderdate))) v(dte)
union all
select dateadd(day, 1, d.dte),
(case when datename(weekday, dte) not in ('Saturday', 'Sunday') then 1 else 0 end) + num_weekdays,
d.month_start
from dates d
where dte < dateadd(day, -1, dateadd(month, 1, month_start))
),
d as (
select d.*, max(num_weekdays) over (partition by month_start) as month_weekdays
from dates d
)
select d.month_start, t.email,
sum(paymentamount) as mtd,
sum(paymentamount) * max(month_weekdays) / max(d.num_weekdays) as my_projected,
sum(paymentamount) * 1.0 / (max(month_weekdays) * max(d.num_weekdays)) as your_projected
from t join
d
on t.orderdate = d.orderdate
group by d.month_start, t.email;
Here is a db<>fiddle.

sql difference in two dates

In sql server, I have two tables:
Tran_Ex
Transactions
They both have customer_id which is the key to join the tables.
I want to find the difference in WORKING DAYS of the Date_Reported (from transactions) from the Date_Received (from the Tran_ex). I would like an extra column with these figures:
eg
Date Reported | Date Received | Difference in days
Thanks in advance
Use DATEDIFF() function()
You can get Working Day (Monday to Friday) difference from this query, for bank holidays you need seperate logic.
Select Date_Reported,
Date_Received ,
(DATEDIFF(dd, Date_Reported, Date_Received) + 1)
-(DATEDIFF(wk, Date_Reported, Date_Received) * 2)
-(CASE WHEN DATENAME(dw, Date_Reported) = 'Sunday' THEN 1 ELSE 0 END)
-(CASE WHEN DATENAME(dw, Date_Received) = 'Saturday' THEN 1 ELSE 0 END)
AS Working_days_Difference
from Tran_Ex as tx
inner join
Transactions as tr
on(tx.customer_id = tr.customer_id)
Modified the Query on suggestions based on #scsimon for not using shorthands.
SELECT Date_Reported,
Date_Received ,
datediff(day,((CASE WHEN Datename(weekday, Date_Reported) = 'Sunday' THEN 1 ELSE 0 END ) - (CASE WHEN Datename(weekday, Date_Received ) = 'Saturday' THEN 1 ELSE 0 END )),Datediff(day,(Datediff(week, Date_Reported, Date_Received ) * 2 ),
(Datediff(day, Date_Reported, Date_Received ) + 1 )))
AS Working_days_Difference
from Tran_Ex as tx
inner join
Transactions as tr
on(tx.customer_id = tr.customer_id)
Use DATEDIFF() function :
select t.Date_Reported, t1.Date_Received,
datediff(day, t.Date_Reported, t1.Date_Received) [Difference in days]
from Tran_Ex tx
inner join Transactions t on t.customer_id = tx.customer_id;

SQL - Average on DateDiff with taking out weekends

I have an SQL statement that calculates how many days late our vendors pay for invoices. It only includes weekdays and it works perfectly. My customer is now asking if I can have an average of those days late per vendor. Here is my code that works:
DATEDIFF(dd, PURCHTABLE.CONFIRMEDDLV, MAX(VENDPACKINGSLIPJOUR.DELIVERYDATE))
-(DATEDIFF(wk, PURCHTABLE.CONFIRMEDDLV, MAX(VENDPACKINGSLIPJOUR.DELIVERYDATE)) * 2)
-(CASE WHEN DATENAME(dw, PURCHTABLE.CONFIRMEDDLV) = 'Sunday'
THEN 1
ELSE 0
END)
-(CASE WHEN DATENAME(dw, MAX(VENDPACKINGSLIPJOUR.DELIVERYDATE)) = 'Saturday'
THEN 1
ELSE 0
END) AS 'DAYS LATE'
If I try to put this in an AVG(), but it tells me I cannot perform an aggregate function on an aggregate function.
UPDATE:
This is my original SQL:
SELECT
PURCHTABLE.PURCHNAME AS 'VENDOR NAME',
PURCHTABLE.ORDERACCOUNT AS 'VENDOR NUMBER',
COUNT(DISTINCT PURCHTABLE.PURCHID) AS 'PURCHASE ORDER',
COUNT(PURCHLINE.LINENUMBER) AS 'NUMBER OF LINES',
SUM(PURCHLINE.LINEAMOUNT) AS 'PO PRICE TOTAL',
DATEDIFF(dd, PURCHTABLE.CONFIRMEDDLV, MAX(VENDPACKINGSLIPJOUR.DELIVERYDATE))-(DATEDIFF(wk, PURCHTABLE.CONFIRMEDDLV, MAX(VENDPACKINGSLIPJOUR.DELIVERYDATE)) * 2)-(CASE WHEN DATENAME(dw, PURCHTABLE.CONFIRMEDDLV) = 'Sunday' THEN 1 ELSE 0 END)-(CASE WHEN DATENAME(dw, MAX(VENDPACKINGSLIPJOUR.DELIVERYDATE)) = 'Saturday' THEN 1 ELSE 0 END) AS 'DAYS LATE'
FROM
PURCHTABLE
JOIN
PURCHLINE ON PURCHLINE.PURCHID = PURCHTABLE.PURCHID
JOIN
VENDPACKINGSLIPJOUR ON VENDPACKINGSLIPJOUR.PURCHID = PURCHTABLE.PURCHID
WHERE
PURCHTABLE.DELIVERYDATE >= '2017-01-01'
AND
PURCHTABLE.DELIVERYDATE <= '2017-01-20'
AND
PURCHTABLE.ORDERACCOUNT = 'VN03526'
GROUP BY
PURCHTABLE.PURCHNAME,
PURCHTABLE.ORDERACCOUNT,
PURCHTABLE.DELIVERYDATE,
PURCHTABLE.CONFIRMEDDLV
So I don't quite understand how I change that to what you are saying.....I'm tried this but it's not working.
SELECT
PURCHTABLE.PURCHNAME AS 'VENDOR NAME',
PURCHTABLE.ORDERACCOUNT AS 'VENDOR NUMBER'
FROM
(SELECT
PURCHTABLE.PURCHNAME AS 'VENDOR NAME',
PURCHTABLE.ORDERACCOUNT AS 'VENDOR NUMBER',
COUNT(DISTINCT PURCHTABLE.PURCHID) AS 'PURCHASE ORDER',
COUNT(PURCHLINE.LINENUMBER) AS 'NUMBER OF LINES',
SUM(PURCHLINE.LINEAMOUNT) AS 'PO PRICE TOTAL',
DATEDIFF(dd, PURCHTABLE.CONFIRMEDDLV, MAX(VENDPACKINGSLIPJOUR.DELIVERYDATE))-(DATEDIFF(wk, PURCHTABLE.CONFIRMEDDLV, MAX(VENDPACKINGSLIPJOUR.DELIVERYDATE)) * 2)-(CASE WHEN DATENAME(dw, PURCHTABLE.CONFIRMEDDLV) = 'Sunday' THEN 1 ELSE 0 END)-(CASE WHEN DATENAME(dw, MAX(VENDPACKINGSLIPJOUR.DELIVERYDATE)) = 'Saturday' THEN 1 ELSE 0 END) AS 'DAYS LATE'
FROM
PURCHTABLE
JOIN
PURCHLINE ON PURCHLINE.PURCHID = PURCHTABLE.PURCHID
JOIN
VENDPACKINGSLIPJOUR ON VENDPACKINGSLIPJOUR.PURCHID = PURCHTABLE.PURCHID
WHERE
PURCHTABLE.DELIVERYDATE >= '2017-01-01'
AND
PURCHTABLE.DELIVERYDATE <= '2017-01-20'
AND
PURCHTABLE.ORDERACCOUNT = 'VN03526') A
GROUP BY
PURCHTABLE.PURCHNAME,
PURCHTABLE.ORDERACCOUNT,
PURCHTABLE.DELIVERYDATE,
PURCHTABLE.CONFIRMEDDLV
The code you have at the moment will be part of a larger query, such as:
select VendorID
,VendorName
...
...
,<Your Days Late Code> as DaysLate
,InvoiceAmount
...
from tables
group by VendorID
,VendorName
all you need to do to average over all of this is pick the other columns you want to do the average by and wrap the whole thing in another select statement:
select VendorID
,VendorName
,avg(DaysLate) as AverageDaysLate
,sum(InvoiceAmount) as TotalInvoiceAmount
from(
select VendorID
,VendorName
...
...
,<Your Days Late Code> as DaysLate
,InvoiceAmount
...
from tables
group by VendorID
,VendorName
) a
group by VendorID
,VendorName

SQL: Create Multiple Selects Based One Date

I've been searching for help on this but I can't seem to find the solution. What I have is:
Table called Details_Orders.
Columns: CustomerName, InvoiceDate, SaleAmount and SaleCost.
What I want to do is select a date ('1-1-2015') and display the Customer Name, the Date ('1-1-2015'), the sum of SaleAmount for that Date, and the Sum of the SaleCost. And here's the part I'm having problems with, I want the next columns to display the previous months SaleAmount and SaleCost, then the previous YEARS SaleAmount and SaleCost.
I'm having trouble figuring out how to code the Previous Time Periods Select statements. Any help on this would be greatly appreciated.
Select
CustomerName,
InvoiceDate,
Sum(SaleAmount),
Sum(SaleCost),
*PREVIOUS MONTH Sum(SalesAmount) DATEADD(MONTH,-1,'1-1'2015'),
PREVIOUS MONTH Sum(SalesCost) DATEADD(MONTH,-1,'1-1'2015'),
PREVIOUS YEAR Sum(SalesAmount) DATEADD(YEAR,-1,'1-1'2015'),
PREVIOUS YEAR Sum(SalesCost) DATEADD(YEAR,-1,'1-1'2015')*
From Details_Orders
WHERE InvoiceDate='1-1-2015'
Basic
You want to use conditional aggregation, and also need a group by. It is a bit unclear what the exact definitions of previous month and year are. Here is one interpretation:
Select CustomerName,
Sum(case when InvoiceDate = thedate then SaleAmount else 0 end),
Sum(case when InvoiceDate = thedate then SaleCost else 0 end),
Sum(case when year(InvoiceDate)*12 + month(InvoiceDate) =
year(thedate)*12 + month(thedate) - 1
then SaleAmount else 0
end),
Sum(case when year(InvoiceDate)*12 + month(InvoiceDate) =
year(thedate)*12 + month(thedate) - 1
then SaleCost else 0
end),
Sum(case when InvoiceDate < thedate and
InvoiceDate >= dateadd(year, -1, thedate
then SaleAmount else 0
end),
Sum(case when InvoiceDate < thedate and
InvoiceDate >= dateadd(year, -1, thedate
then SaleCost else 0
end)
From (select cast('2015-01-01' as date) as thedate
) params cross join
Details_Orders o
group by CustomerName;

Count by day, count by week, in a grouped select statement

I am trying to count instances of a status by current day and current week, grouped by town.
(The table has just 3 columns: Town, status, status_date)
SELECT
MAX(dbo.Clients.Town) AS Town,
CASE
WHEN MAX(datepart(wk, status_date)) = DATEPART(wk, getdate()) THEN COUNT(Town)
ELSE 0
END AS wkTotal,
CASE
WHEN MAX(CONVERT(date, status_date, 106)) = CONVERT(date, getdate(), 106) THEN COUNT(Town)
ELSE 0
END AS dayTotal
FROM
dbo.Clients
WHERE
dbo.Clients.Status LIKE 'Status 1%'
AND MONTH(GETDATE()) = MONTH(dbo.Clients.Status_date)
AND YEAR(GETDATE())= YEAR(dbo.Clients.Status_date)
GROUP BY
dbo.Clients.Town
ORDER BY
dbo.Clients.Town
This code just returns a month count for both day total and week total columns
Hope you can help.
I surgest you do this:
SELECT dbo.Clients.Town AS Town,
count(*) AS wkTotal,
sum(CASE WHEN datepart(dayofyear, status_date) = DATEPART(dayofyear, getdate()) THEN 1 ELSE 0 END) AS dayTotal
FROM dbo.Clients
WHERE dbo.Clients.Status LIKE 'Status 1%' AND
datepart(week, GETDATE()) = datepart(week, dbo.Clients.Status_date)
AND YEAR(GETDATE())= YEAR(dbo.Clients.Status_date)
GROUP BY dbo.Clients.Town
ORDER BY dbo.Clients.Town