SUM and Grouping by date and material - sql

Still learning SQL forgive me.
I have 3 tables. a material table, a material_req table and a material_trans table. I want to group by material and then group columns by year.
so it would be [material, 2019, 2018, 2017, 2016, total (total being the total qty used for each material.
I have tried to place the date in the select statement, and grouped by the date also. but then the returned result is a lot of the same material with a lot of dates. I only need the year. maybe try the same and return just the year?
SELECT material_req.Material
-- , Material_Trans_Date
, SUM(-1 * material_trans.Quantity) AS 'TOTAL'
,Standard_Cost
FROM
Material_Req inner join Material_Trans
ON
Material_Req.Material_Req = Material_Trans.Material_Req
LEFt JOIN Material
ON
Material.Material = Material_Req.Material
WHERE
material_trans.Material_Trans_Date between '20180101' AND GETDATE()
-- Material_Trans_Date between '20180101' AND '20181231'
-- Material_Trans_Date between '20170101' AND '20171231'
-- Material_Trans_Date between '20160101' AND '20161231'
GROUP BY
material_req.Material ,Standard_Cost
ORDER BY
Material_Req.Material, Standard_Cost
expected results should by grouped by material, 2019, 2018, 2017,2016, Standard_Cost. the years column will have the sum of qty for each material for that year.
results look like this current_results

If you are using SQL Server then you might try this:
SELECT material_req.Material
, SUM(CASE WHEN DATEPART(YEAR, Material_Trans_Date) = '2019' THEN material_trans.Quantity ELSE 0 END) [2019 TOTAL]
, SUM(CASE WHEN DATEPART(YEAR, Material_Trans_Date) = '2018' THEN material_trans.Quantity ELSE 0 END) [2018 TOTAL]
,Standard_Cost
FROM
Material_Req inner join Material_Trans
ON
Material_Req.Material_Req = Material_Trans.Material_Req
LEFt JOIN Material
ON
Material.Material = Material_Req.Material
WHERE
material_trans.Material_Trans_Date between '20180101' AND GETDATE()
GROUP BY
material_req.Material ,Standard_Cost
ORDER BY
Material_Req.Material, Standard_Cost

Related

Calculate the percentage change with respect to the previous year, quarter and category

Using ORACLE SQL, I have a query that gives me the output in the following table(posting an image). However, I need to figure out a way to get the percentage change between year, quarter and metric in an additional column.
Example: Year 2022, Q1, apple against Year 2021, Q1, apple.
I'm relatively new to SQL so I'm not sure if I need to sort the output differently to use the function LEAD, or if there is a better way to do it in general.
My current query with my attempt at the percent change with lead (that didn't work) is like this:
`
SELECT s.YEAR
, t.quarter
, CASE WHEN fruits IN ('tangerine','lemon') THEN 'orange'
ELSE fruits
END metric
, COUNT(DISTINCT s.ID) AS COUNT
-- , ROUND((COUNT(UNIQUE s.ID) - LEAD(COUNT(UNIQUE s.ID)) OVER (ORDER BY t.quarter))/COUNT(UNIQUE s.ID)*100,2) pct_change
FROM s
JOIN sc
ON S.KEY = SC.KEY
JOIN c
ON SC.KEY = C.KEY
JOIN t
ON s.quarter = t.quarter
WHERE S.YEAR BETWEEN '2021' AND '2022'
AND s.quarter IN ('1','2','3')
GROUP BY S.YEAR
, t.quarter
,CASE WHEN fruits IN ('tangerine','lemon') THEN 'orange'
ELSE fruits
END
`
With my query as (
...your original query as above goes here...
)
Select curr.year, curr.quarter, curr.metric
, curr.count as current_count
, Case When prior.count is null then prior.count else if prior.count = 0 then null else (curr.count - prior.count)/ prior.count*100 as pct_chg
From my_query curr Left Outer Join my_query prior
On curr.year-1 = prior.year and
curr.qtr=prior.qtr and
curr.metric=prior.metric

Is it possible to add second where condition to select this same data but from other date range?

I have two tables in SQL Server.
I want to select DeptCode, DeptName, YearToDate, PeriodToDate (2 months for example) and group it by DeptCode.
There is a result which I want to get:
In YTD column I want to get sum of totalCost since 01/01/actualYear.
In PTD column I want to get the sum from last two months.
I created a piece of code which shows me correct YTD cost but I don't know how I can add next one for getting total cost for other date range. Is it possible to do this?
SELECT
d.DeptCode,
d.DeptName,
SUM(s.TotalCost) as YTD
FROM [Departments] AS d
INNER JOIN Shipments AS s
ON d.DeptCode= s.DeptCode
WHERE s.ShipmentDate BETWEEN DateAdd(yyyy, DateDiff(yyyy, 0, GetDate()), 0)
AND GETDATE()
GROUP BY d.DeptCode, d.DeptName
Your expected output doesn't match 2 months, but here's the code to accomplish what you want. You just have to add a SUM(CASE...) on the 2nd condition.
SELECT
d.DeptCode,
d.DeptName,
SUM(s.TotalCost) as YTD,
SUM(CASE WHEN s.ShipmentDate >= DATEADD(month, -2, GETDATE()) then s.TotalCost else 0 END) as PTD
FROM [Departments] AS d
INNER JOIN Shipments AS s
ON d.DeptCode= s.DeptCode
WHERE Year(s.ShipmentDate) = Year(GETDATE())
GROUP BY d.DeptCode, d.DeptName
Just add one more column that returns 0 when not in the two-month range, e.g. SUM(CASE WHEN (date check) THEN (amount) ELSE 0 END). Check out the fifth line:
SELECT
d.DeptCode,
d.DeptName,
SUM(s.TotalCost) as YTD,
SUM(CASE WHEN DateDiff(MONTH, s.ShipmentDate, GetDate()) < 2 THEN s.TotalCost ELSE 0 END) PTD,
FROM [Departments] AS d
INNER JOIN Shipments AS s
ON d.DeptCode= s.DeptCode
WHERE s.ShipmentDate BETWEEN DateAdd(yyyy, DateDiff(yyyy, 0, GetDate()), 0)
AND GETDATE()
GROUP BY d.DeptCode, d.DeptName
Try this one :
nbr_last2month_ AS
(
SELECT DISTINCT
Sum(s.[TotalCost]) AS 'PTD',
s.DeptCode,
s.DeptName
FROM [Shipements] s
LEFT JOIN [Departements] d ON d.[DeptCode] = s.[DeptCode]
WHERE Year(date_) LIKE Year(GETDATE())
AND MONTH(ShipementDate) LIKE Month(Getdate()) - 2
Group by DeptCode
),
nbr_YTD_ AS
(
SELECT DISTINCT
Sum(s.[TotalCost]) AS 'YTD',
s.DeptCode,
s.DeptName
FROM [Shipements] s
LEFT JOIN [Departements] d ON d.[DeptCode] = s.[DeptCode]
WHERE Year(ShipementDate) LIKE Year(GETDATE())
Group by DeptCode
),
SELECT
A.DeptCode,
A.DeptName,
YTD,
PTD
FROM nbr_YTD_ A
LEFT JOIN nbr_last2month_ B on B.DeptCode = A.DeptCode
ORDER BY DeptCode

how to return total sold p / year for several years in columnar format?

The SQL below give me the columns for Account, Name and the Total Sale for the year of 2015.But how, if possible, can I add another column for the previous year of 2014 ?
select a.AcctNo, b.Name, Sum(a.TotSold) as [ Total Sold ]
from Orders as A
Join Accounts as b on a.AcctNo = b.AcctNo
where (a.PurchaseDate between '1/1/2015' and '12/31/2015' )
Group by a.AcctNo, b.Name
You can use conditional aggregation:
select o.AcctNo, a.Name,
Sum(case when year(PurchaseDate) = 2015 then TotSold else 0 end) as tot_2015,
Sum(case when year(PurchaseDate) = 2014 then TotSold else 0 end) as tot_2014
from Orders o Join
Accounts a
on a.AcctNo = o.AcctNo
where PurchaseDate >= '2014-01-01'
PurchaseDate < '2016-01-01'
Group by o.AcctNo, a.Name ;
Notes:
Use ISO standard formats for date constants.
When using dates, try not to use between. It doesn't work as expected when there is a time component. The above inequalities work regardless of a time component.
Use table aliases that are abbreviations of the table name. That makes the query easier to follow.

SQL query of sales by customer over multiple years for top 50 customers in one year

I have a query in SQL server that successfully returns the top 50 customers for a given year by sales. I want to expand it to return their sales for the additional years when they may or may not be in the top 50.
SELECT TOP 50 CU.CustomerName, SUM(ART.SalesAnalysis) AS '2011'
FROM ARTransaction AS ART, Customer AS CU
WHERE ART.CustomerN = CU.CustomerN AND ART.PostingDate BETWEEN '2010-12-31' AND '2012-01-01'
GROUP By CU.CustomerName
ORDER BY SUM(ART.SalesAnalysis) DESC
I tried adding nested SELECT statements but they return strange results and I'm not sure why (might not ever work, but the results have me flabbergasted anyway). When included the values of every row is changed and customers are duplicated.
(SELECT SUM(ART.SalesAnalysis)
WHERE ART.PostingDate BETWEEN '2011-12-31' AND '2013-01-01') AS '2012'
I tried to put a TOP statement in a nested SELECT in HAVING but that tells me
"Msg 8114, Level 16, State 5, Line 1
Error converting data type varchar to numeric."
SELECT CU.CustomerName, SUM(ART.SalesAnalysis) AS '2011'
FROM ARTransaction AS ART
JOIN Customer AS CU ON ART.CustomerN = CU.CustomerN
GROUP BY CU.CustomerNAme
HAVING CU.CustomerNAme IN
(SELECT TOP 50 CU.CustomerName
FROM ARTransaction
JOIN Customer ON ARTransaction.CustomerN = Customer.CustomerN
WHERE ARTransaction.SalesAnalysis BETWEEN '2010-12-31' AND '2012-01-01'
GROUP BY Customer.CustomerN
ORDER BY SUM(ART.SalesAnalysis) DESC)
If I understand correctly, you are looking for the top 50 sales for customers based on 2011 data - and want to see all years data for those top 50 from 2011, regardless of those customers being in the top 50 for other years.
Try this, it might need to be tweaked a bit as I don't know the schema, but if I understand the question correctly, this should do the trick.
WITH Top50 AS (
SELECT TOP 50
CU.CustomerN
,SUM(ART.SalesAnalysis) AS SalesTotal
FROM
ARTransaction art
INNER JOIN
Customer cu
ON cu.CustomerN = art.CustomerN
WHERE
ART.PostingDate BETWEEN CAST('2011-01-01' AS DATETIME)
AND CAST('2011-12-31' AS DATETIME)
GROUP BY
CU.CustomerN
ORDER BY
SUM(ART.SalesAnalysis) DESC)
SELECT
c.CustomerName
,SUM(a.SalesAnalysis) AS TotalSales
,YEAR(a.PostingDate) AS PostingDateYear
FROM
ARTransaction a
INNER JOIN
Customer c
ON c.CustomerN = a.CustomerN
INNER JOIN
Top50 t
ON t.CustomerN = a.CustomerN
GROUP BY
c.CustomerName
,YEAR(a.PostingDate)
ORDER BY
PostingDateYear
you could use something like below... you will be looking at the all the data for all the years you want to look at and then just getting the top 50 for the 2011 year
SELECT TOP 50
CU.CustomerName,
SUM(case when year(ART.PostingDate) = 2011 -- or you could use case when ART.PostingDate BETWEEN '2011-01-01' AND '2011-12-31'
then ART.SalesAnalysis
else 0 end) AS 2011,
SUM(case when year(ART.PostingDate) = 2012
then ART.SalesAnalysis
else 0 end) AS 2012
FROM
ARTransaction ART,
inner join Customer CU
on ART.CustomerN = CU.CustomerN
WHERE ART.PostingDate BETWEEN '2011-01-01 AND '2012-12-31'
GROUP By CU.CustomerName
ORDER BY
SUM(case when year(ART.PostingDate) = 2011
then ART.SalesAnalysis
else 0 end) DESC

Repeat Customers Each Year (Retention)

I've been working on this and I don't think I'm doing it right. |D
Our database doesn't keep track of how many customers we retain so we looked for an alternate method. It's outlined in this article. It suggests you have this table to fill in:
Year Number of Customers Number of customers Retained in 2009 Percent (%) Retained in 2009 Number of customers Retained in 2010 Percent (%) Retained in 2010 ....
2008
2009
2010
2011
2012
Total
The table would go out to 2012 in the headers. I'm just saving space.
It tells you to find the total number of customers you had in your starting year. To do this, I used this query since our starting year is 2008:
select YEAR(OrderDate) as 'Year', COUNT(distinct(billemail)) as Customers
from dbo.tblOrder
where OrderDate >= '2008-01-01' and OrderDate <= '2008-12-31'
group by YEAR(OrderDate)
At the moment we just differentiate our customers by email address.
Then you have to search for the same names of customers who purchased again in later years (ours are 2009, 10, 11, and 12).
I came up with this. It should find people who purchased in both 2008 and 2009.
SELECT YEAR(OrderDate) as 'Year',COUNT(distinct(billemail)) as Customers
FROM dbo.tblOrder o with (nolock)
WHERE o.BillEmail IN (SELECT DISTINCT o1.BillEmail
FROM dbo.tblOrder o1 with (nolock)
WHERE o1.OrderDate BETWEEN '2008-1-1' AND '2009-1-1')
AND o.BillEmail IN (SELECT DISTINCT o2.BillEmail
FROM dbo.tblOrder o2 with (nolock)
WHERE o2.OrderDate BETWEEN '2009-1-1' AND '2010-1-1')
--AND o.OrderDate BETWEEN '2008-1-1' AND '2013-1-1'
AND o.BillEmail NOT LIKE '%#halloweencostumes.com'
AND o.BillEmail NOT LIKE ''
GROUP BY YEAR(OrderDate)
So I'm just finding the customers who purchased in both those years. And then I'm doing an independent query to find those who purchased in 2008 and 2010, then 08 and 11, and then 08 and 12. This one finds 2008 and 2010 purchasers:
SELECT YEAR(OrderDate) as 'Year',COUNT(distinct(billemail)) as Customers
FROM dbo.tblOrder o with (nolock)
WHERE o.BillEmail IN (SELECT DISTINCT o1.BillEmail
FROM dbo.tblOrder o1 with (nolock)
WHERE o1.OrderDate BETWEEN '2008-1-1' AND '2009-1-1')
AND o.BillEmail IN (SELECT DISTINCT o2.BillEmail
FROM dbo.tblOrder o2 with (nolock)
WHERE o2.OrderDate BETWEEN '2010-1-1' AND '2011-1-1')
--AND o.OrderDate BETWEEN '2008-1-1' AND '2013-1-1'
AND o.BillEmail NOT LIKE '%#halloweencostumes.com'
AND o.BillEmail NOT LIKE ''
GROUP BY YEAR(OrderDate)
So you see I have a different query for each year comparison. They're all unrelated. So in the end I'm just finding people who bought in 2008 and 2009, and then a potentially different group that bought in 2008 and 2010, and so on. For this to be accurate, do I have to use the same grouping of 2008 buyers each time? So they bought in 2009 and 2010 and 2011, and 2012?
This is where I'm worried and not sure how to proceed or even find such data.
Any advice would be appreciated! Thanks!
How about a cross-tab on a per customer basis to help you out...
From this, you can start to analyze a bit more in bulk by comparing a customer's
current year to the previous and have a total customers count for each respective year.
From that you can run whatever percentages you want in your final output
This should get you a whole set of all years in question, and you can just keep adding years as need be for comparison. It should be very quick, especially if you have an index on ( BillEMail, OrderDate ).
The premise is that the inner query just blows through all the records, and on a customer basis sets a flag of 1 if there are ANY orders within the given year (via MAX()). It does it via case/when so each year is detected for a customer. Once that has been determined, the outer query then rolls those up comparing each customer with if they had a sale in one year vs the prior, if so, SUM() 1 vs 0 and you have your counts of retention.
SELECT
SUM( case when PreQry.C2011 = 1 and PreQry.C2012 = 1 then 1 else 0 end ) as Retain2011_2012,
SUM( case when PreQry.C2010 = 1 and PreQry.C2011 = 1 then 1 else 0 end ) as Retain2010_2011,
SUM( case when PreQry.C2009 = 1 and PreQry.C2010 = 1 then 1 else 0 end ) as Retain2009_2010,
SUM( case when PreQry.C2008 = 1 and PreQry.C2009 = 1 then 1 else 0 end ) as Retain2008_2009,
SUM( PreQry.C2012 ) CustCount2012,
SUM( PreQry.C2011 ) CustCount2011,
SUM( PreQry.C2010 ) CustCount2010,
SUM( PreQry.C2009 ) CustCount2009,
SUM( PreQry.C2008 ) CustCount2008
from
( select
O.BillEMail as customer,
MAX( CASE when YEAR( O.OrderDate ) = 2012 then 1 else 0 end ) as C2012,
MAX( CASE when YEAR( O.OrderDate ) = 2011 then 1 else 0 end ) as C2011,
MAX( CASE when YEAR( O.OrderDate ) = 2010 then 1 else 0 end ) as C2010,
MAX( CASE when YEAR( O.OrderDate ) = 2009 then 1 else 0 end ) as C2009,
MAX( CASE when YEAR( O.OrderDate ) = 2008 then 1 else 0 end ) as C2008
from
dbo.tblOrder O
where
O.OrderDate >= '2008-01-01'
AND O.BillEmail NOT LIKE '%#halloweencostumes.com'
AND O.BillEmail NOT LIKE ''
group by
O.BillEMail ) as PreQry
Now, if you wanted to detect how many were "NEW" for a given year, you could just add additional columns such as testing the previous year sale flag = 0 vs current year = 1 such as
SUM( case when PreQry.C2011 = 0 and PreQry.C2012 = 1 then 1 else 0 end ) as NewIn2012,
SUM( case when PreQry.C2010 = 0 and PreQry.C2011 = 1 then 1 else 0 end ) as NewIn2011,
SUM( case when PreQry.C2009 = 0 and PreQry.C2010 = 1 then 1 else 0 end ) as NewIn2010,
SUM( case when PreQry.C2008 = 0 and PreQry.C2009 = 1 then 1 else 0 end ) as NewIn2009
If I understand your problem right, then you sounds like you've gotten mixed up in the details. It depends on what you want your definition of 'retain' to be. How about 'also bought in some previous year'? Then, for year X, a customer is retained if they also bought from you in a previous year.
For 2012, for example:
SELECT YEAR(OrderDate) as 'Year',COUNT(distinct(billemail)) as Customers
FROM dbo.tblOrder o with (nolock)
WHERE o.BillEmail IN (SELECT DISTINCT o1.BillEmail
FROM dbo.tblOrder o1 with (nolock)
WHERE o1.OrderDate BETWEEN '2012-1-1' AND '2013-1-1')
AND o.BillEmail IN (SELECT DISTINCT o2.BillEmail
FROM dbo.tblOrder o2 with (nolock)
WHERE o2.OrderDate < '2012-1-1')
AND o.BillEmail NOT LIKE '%#halloweencostumes.com'
AND o.BillEmail NOT LIKE ''
GROUP BY YEAR(OrderDate)
Does this work?
Edit
You can take this a step farther and abstract out the year so 1 query will suffice:
SELECT YEAR(O.OrderDate) as 'Year',COUNT(distinct(billemail)) as Customers
FROM dbo.tblOrder o with (nolock)
WHERE o.BillEmail IN (SELECT DISTINCT o1.BillEmail
FROM dbo.tblOrder o1 with (nolock)
WHERE year(o1.OrderDate)=YEAR(O.OrderDate)
AND o.BillEmail IN (SELECT DISTINCT o2.BillEmail
FROM dbo.tblOrder o2 with (nolock)
WHERE year(o2.OrderDate) < year(o.orderdate)
AND o.BillEmail NOT LIKE '%#halloweencostumes.com'
AND o.BillEmail NOT LIKE ''
GROUP BY YEAR(OrderDate)
This should give you, for each year in which you had orders, the count of distinct customers, and the count of customers who also purchased in a previous year. However, it's not in the same format as the table you want to populate.