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.
Related
Assume a table of purchase transactions with columns CustId, Amount, DatePosted where Amount is the value of the transaction, and DatePosted is a DATETIME value. Given a specific CustId, how would I write a select such that it returns a single row with the following columns: CustId, total value of transactions in the last 3 days, last 60 days, 1 year, 2 years (5 columns total).
Example table:
CustId
Amount
DatePosted
1234
698.02
2023-01-23Z12:34:56
1234
582.69
2022-12-15Z19:57:23
1234
7775.22
2022-12-02Z02:34:32
1234
18.72
2022-01-23Z12:34:56
1234
2.27
2021-01-23Z12:34:56
Expected output given the sample data above when searching using CustId=1234:
CustId
3-day Total
60-day Total
1 year Total
2 year Total
1234
698.02
9055.93
9074.65
9076.92
You could get all purchase data for the last 2 years, then using SUM with SQL CASE expression to calculate total value for each time-range.
SELECT
CustId,
SUM(CASE WHEN DatePosted >= Last3Day THEN Amount ELSE 0 END) AS [3-day Total],
SUM(CASE WHEN DatePosted >= Last60Day THEN Amount ELSE 0 END) AS [60-day Total],
SUM(CASE WHEN DatePosted >= Last1Year THEN Amount ELSE 0 END) AS [1 year Total],
SUM(CASE WHEN DatePosted >= Last2Year THEN Amount ELSE 0 END) AS [2 year Total]
FROM
<your data table>,
(SELECT
DATEADD(DAY, -3, GETDATE()) AS Last3Day,
DATEADD(DAY, -60, GETDATE()) AS Last60Day,
DATEADD(YEAR, -1, GETDATE()) AS Last1Year,
DATEADD(YEAR, -2, GETDATE()) AS Last2Year) timerange
WHERE DatePosted >= Last2Year
GROUP BY CustId;
Demo: http://sqlfiddle.com/#!18/9eecb/179880
This query assumes 2 year max. If you want to go further back then change the where clause as well. No need to use coalesce or a derived table. SQL server query planner may be smart enough to provide similar performance for all these solutions but this is easier to understand:
SELECT
CustId,
SUM(CASE WHEN DatePosted >= DATEADD(day, -3, GETDATE()) THEN Amount ELSE 0 END) AS [3-day Total],
SUM(CASE WHEN DatePosted >= DATEADD(day, -60, GETDATE()) THEN Amount ELSE 0 END) AS [60-day Total],
SUM(CASE WHEN DatePosted >= DATEADD(year, -1, GETDATE()) THEN Amount ELSE 0 END) AS [1 year Total],
SUM(Amount) AS [2 year Total]
FROM PurchaseTransactions
WHERE CustId = 1234 AND DatePosted >= DATEADD(year, -2, GETDATE())
GROUP BY CustId
This is set up so that you can set #CustID = null and the query will return results for all customers in the set.
EDIT: Updated my query below to give you more flexibility across your desired ranges should you wish to derive additional heuristics. (Counts, averages, etc.)
Also removed coalesce as it's simply not needed here.
DECLARE #CustID BIGINT;
SELECT table1.custID,
SUM([3Day].amount) AS [3DayTotal],
COUNT([3Day].amount) AS [3DayCount]
SUM([60Day].amount) AS [60DayTotal],
SUM([1Year].amount) AS [1YearTotal],
Sum([2Year].amount) AS [2YearTotal],
AVG([2Year].amount) AS [2YearAverage]
FROM table1 LEFT OUTER JOIN
(SELECT custID, Amount FROM table1 WHERE DatePosted > DATEADD(DAY, -3, GETDATE())) AS [3Day] ON table1.CustID = [3Day].CustID LEFT OUTER JOIN
(SELECT custID, Amount FROM table1 WHERE DatePosted > DATEADD(DAY, -60, GETDATE())) AS [60Day] ON table1.CustID = [60Day].CustID LEFT OUTER JOIN
(SELECT custID, Amount FROM table1 WHERE DatePosted > DATEADD(YEAR, -1, GETDATE())) AS [1Year] ON table1.CustID = [1Year].CustID LEFT OUTER JOIN
(SELECT custID, Amount FROM table1 WHERE DatePosted > DATEADD(YEAR, -2, GETDATE()) AS [2Year] ON table1.CustID = [2Year].CustID
WHERE table1.CustID = #CustID
OR #CustID IS NULL
GROUP BY table1.CustID
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;
I have Table1 and I need a query to populate Table2:
Problem here is with Date column. I want to know the process of location/partner combination per day. Main issue here is that I can't pick DateCreated and make it as default date since it doesn't necessarily cover whole date range, like in this example where it doesn't have 2015-01-07 and 2015-01-09. Same case with other dates.
So, my idea is to first select dates from some table which contains needed date range and then perform calculation for each day/location/partner combination from cte but in that case I can't figure out how to make a join for LocationId and PartnerId.
Columns:
Date - CreatedItems - number of created items where Table1.DateCreated = Table2.Date
DeliveredItems - number of delivered items where Table1.DateDateOut = Table2.Date
CycleTime - number of days delivered item was in the location (DateOut - DateIn + 1)
I started with something like this but it's very like that I completely missed the point with it:
with d as
(
select date from DimDate
where date between DATEADD(DAY, -365, getdate()) and getdate()
),
cr as -- created items
(
select
DateCreated,
LocationId,
PartnerId,
CreatedItems = count(*)
from Table1
where DateCreated is not null
group by DateCreated,
LocationId,
PartnerId
),
del as -- delivered items
(
select
DateOut,
LocationId,
ParnerId,
DeliveredItems = count(*),
CycleTime = DATEDIFF(Day, DateOut, DateIn)
from Table1
where DateOut is not null
and Datein is not null
group by DateOut,
LocationId,
PartnerId
)
select
d.Date
from d
LEFT OUTER JOIN cr on cr.DateCreated = d.Date -- MISSING JOIN PER LocationId and PartnerId
LEFT OUTER JOIN del on del.DateCompleted = d.Date -- MISSING JOIN PER LocationId and PartnerId
with range(days) as (
select 0 union all select 1 union all select 2 union all
select 3 union all select 4 union all select 5 union all
select 6 /* extend as necessary */
)
select dateadd(day, r.days, t.DateCreated) as "Date", locationId, PartnerId,
sum(
case
when dateadd(day, r.days, t.DateCreated) = t.DateCreated
then 1 else 0
end) as CreatedItems,
sum(
case
when dateadd(day, r.days, t.DateCreated) = t.Dateout
then 1 else 0
end) as DeliveredItems,
sum(
case
when dateadd(day, r.days, t.DateCreated) = t.Dateout
then datediff(days, t.DateIn, t.DateOut) + 1 else 0
end) as CycleTime
from
<yourtable> as t
inner join range as r
on r.days between 0 and datediff(day, t.DateCreated, t.DateOut)
group by dateadd(day, r.days, t.DateCreated), LocationId, PartnerId;
If you only want the end dates (rather than all the dates in between) this is probably a better approach:
with range(dt) as (
select distinct DateCreated from T union
select distinct DateOut from T
)
select r.dt as "Date", locationId, PartnerId,
sum(
case
when r.dt = t.DateCreated
then 1 else 0
end) as CreatedItems,
sum(
case
when r.dt = t.Dateout
then 1 else 0
end) as DeliveredItems,
sum(
case
when r.dt = t.Dateout
then datediff(days, t.DateIn, t.DateOut) + 1 else 0
end) as CycleTime
from
<yourtable> as t
inner join range as r
on r.dt in (t.DateCreated, t.DateOut)
group by r.dt, LocationId, PartnerId;
If to specify WHERE clause? Something Like that:
WHERE cr.LocationId = del.LocationId AND
cr.PartnerId = del.PartnerId
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
here is my query taking nearly 20 mins. pls suggest me changes to increase performance
SELECT DISTINCT
CONVERT(varchar(10),x.notice_date,120) Date,
Y.branch_name,
count(case when x.status='broken' and x.branch_name=y.branch_name then 1 end)
Broken,
count(case when x.type='Lote' and x.branch_name=y.branch_name then 1 end)
Lost,
( SELECT COUNT(A.car_no)
FROM DB2.dbo.z_mat A
WHERE DateAdd(Day, DateDiff(Day, 0,a.notice_date), 0)
= DateAdd(Day, DateDiff(Day, 0,x.notice_date), 0)
AND a.branch_name=y.branch_name
) mat,
( SELECT COUNT(B.car_no)
FROM DB2.dbo.z_cat B
WHERE DateAdd(Day, DateDiff(Day, 0,b.notice_date), 0)
= DateAdd(Day, DateDiff(Day, 0,x.notice_date), 0)
AND b.branch_name=y.branch_name
) cat,
( SELECT COUNT(C.car_no)
FROM DB2.dbo.z_pat C
WHERE DateAdd(Day, DateDiff(Day, 0,c.notice_date), 0)
= DateAdd(Day, DateDiff(Day, 0,x.notice_date), 0)
AND c.branch_name=y.branch_name
) pat
FROM DB1.dbo.Cars x
, DB2.dbo.Branch Y
WHERE DateAdd(Day, DateDiff(Day, 0,x.notice_date), 0)
> '2011-01-01'
GROUP BY CONVERT(varchar(10),x.notice_date,120)
, DateAdd(Day, DateDiff(Day, 0,x.notice_date), 0)
, y.branch_name
This might help. Give it a try.
SELECT
DISTINCT CONVERT(VARCHAR(10), car.notice_date, 120) AS NoticeDate
, brc.branch_name
, COUNT(CASE WHEN car.status = 'broken' AND car.branch_name = brc.branch_name THEN 1 END) Broken
, COUNT(CASE WHEN car.status = 'Lote' AND car.branch_name = brc.branch_name THEN 1 END) Lost
, mat.mat_count
, cat.cat_count
, pat.pat_count
FROM DB1.dbo.Cars car
, DB2.dbo.Branch brc
CROSS APPLY (
SELECT COUNT(mat.car_no) mat_count
FROM DB2.dbo.z_mat mat
WHERE DATEDIFF(d, mat.notice_date, car.notice_date) = 0
AND mat.branch_name = brc.branch_name
) mat
CROSS APPLY (
SELECT COUNT(cat.car_no) cat_count
FROM DB2.dbo.z_cat cat
WHERE DATEDIFF(d, cat.notice_date, car.notice_date) = 0
AND cat.branch_name = brc.branch_name
) cat
CROSS APPLY (
SELECT COUNT(pat.car_no) pat_count
FROM DB2.dbo.z_pat pat
WHERE DATEDIFF(d, pat.notice_date, car.notice_date) = 0
AND pat.branch_name = brc.branch_name
) pat
WHERE car.notice_date > '2011-01-01'
GROUP BY CONVERT(VARCHAR(10), car.notice_date, 120)
, brc.branch_name
SELECT CONVERT(varchar(10),x.notice_date,120) Date,Y.branch_name,
COUNT(case when x.status='broken' then 1 end) Broken,
COUNT(case when x.type='Lote' then 1 end) Lost,
SUM(mat) mat, SUM(cat) cat,SUM(pat) pat
FROM DB1.dbo.Cars x
JOIN DB2.dbo.Branch Y ON x.branch_name=y.branch_name
-- group by date and branch name for z_mat table
LEFT JOIN (select COUNT(car_no) mat,branch_name,DateAdd(Day, DateDiff(Day,notice_date), 0)) notice_date from DB2.dbo.z_mat GROUP BY branch_name,DateAdd(Day, DateDiff(Day,notice_date), 0)) AS a
ON a.branch_name = y.branch_name AND DateAdd(Day, DateDiff(Day, 0,x.notice_date), 0) = a.notice_date
-- group by date and branch name for z_cat table
LEFT JOIN (select COUNT(car_no) cat,branch_name,DateAdd(Day, DateDiff(Day,notice_date), 0)) notice_date from DB2.dbo.z_cat GROUP BY branch_name,DateAdd(Day, DateDiff(Day,notice_date), 0)) AS b
ON b.branch_name = y.branch_name AND DateAdd(Day, DateDiff(Day, 0,x.notice_date), 0) = b.notice_date
-- group by date and branch name for z_pat table
LEFT JOIN (select COUNT(car_no) pat,branch_name,DateAdd(Day, DateDiff(Day,notice_date), 0)) notice_date from DB2.dbo.z_pat GROUP BY branch_name,DateAdd(Day, DateDiff(Day,notice_date), 0)) AS c
ON c.branch_name = y.branch_name AND DateAdd(Day, DateDiff(Day, 0,x.notice_date), 0) = c.notice_date
WHERE DateAdd(Day, DateDiff(Day, 0,x.notice_date), 0)>'2011-01-01'
GROUP BY CONVERT(varchar(10),x.notice_date,120),DateAdd(Day, DateDiff(Day, 0,x.notice_date), 0),y.branch_name