SQL query to compare product sales by month - sql

I have a Monthly Status database view I need to build a report based on. The data in the view looks something like this:
Category | Revenue | Yearh | Month
Bikes 10 000 2008 1
Bikes 12 000 2008 2
Bikes 12 000 2008 3
Bikes 15 000 2008 1
Bikes 11 000 2007 2
Bikes 11 500 2007 3
Bikes 15 400 2007 4
... And so forth
The view has a product category, a revenue, a year and a month. I want to create a report comparing 2007 and 2008, showing 0 for the months with no sales. So the report should look something like this:
Category | Month | Rev. This Year | Rev. Last Year
Bikes 1 10 000 0
Bikes 2 12 000 11 000
Bikes 3 12 000 11 500
Bikes 4 0 15 400
The key thing to notice is how month 1 only has sales in 2008, and therefore is 0 for 2007. Also, month 4 only has no sales in 2008, hence the 0, while it has sales in 2007 and still show up.
Also, the report is actually for financial year - so I would love to have empty columns with 0 in both if there was no sales in say month 5 for either 2007 or 2008.
The query I got looks something like this:
SELECT
SP1.Program,
SP1.Year,
SP1.Month,
SP1.TotalRevenue,
IsNull(SP2.TotalRevenue, 0) AS LastYearTotalRevenue
FROM PVMonthlyStatusReport AS SP1
LEFT OUTER JOIN PVMonthlyStatusReport AS SP2 ON
SP1.Program = SP2.Program AND
SP2.Year = SP1.Year - 1 AND
SP1.Month = SP2.Month
WHERE
SP1.Program = 'Bikes' AND
SP1.Category = #Category AND
(SP1.Year >= #FinancialYear AND SP1.Year <= #FinancialYear + 1) AND
((SP1.Year = #FinancialYear AND SP1.Month > 6) OR
(SP1.Year = #FinancialYear + 1 AND SP1.Month <= 6))
ORDER BY SP1.Year, SP1.Month
The problem with this query is that it would not return the fourth row in my example data above, since we didn't have any sales in 2008, but we actually did in 2007.
This is probably a common query/problem, but my SQL is rusty after doing front-end development for so long. Any help is greatly appreciated!
Oh, btw, I'm using SQL 2005 for this query so if there are any helpful new features that might help me let me know.

The Case Statement is my best sql friend. You also need a table for time to generate your 0 rev in both months.
Assumptions are based on the availability of following tables:
sales: Category | Revenue | Yearh |
Month
and
tm: Year | Month (populated with all
dates required for reporting)
Example 1 without empty rows:
select
Category
,month
,SUM(CASE WHEN YEAR = 2008 THEN Revenue ELSE 0 END) this_year
,SUM(CASE WHEN YEAR = 2007 THEN Revenue ELSE 0 END) last_year
from
sales
where
year in (2008,2007)
group by
Category
,month
RETURNS:
Category | Month | Rev. This Year | Rev. Last Year
Bikes 1 10 000 0
Bikes 2 12 000 11 000
Bikes 3 12 000 11 500
Bikes 4 0 15 400
Example 2 with empty rows:
I am going to use a sub query (but others may not) and will return an empty row for every product and year month combo.
select
fill.Category
,fill.month
,SUM(CASE WHEN YEAR = 2008 THEN Revenue ELSE 0 END) this_year
,SUM(CASE WHEN YEAR = 2007 THEN Revenue ELSE 0 END) last_year
from
sales
Right join (select distinct --try out left, right and cross joins to test results.
product
,year
,month
from
sales --this ideally would be from a products table
cross join tm
where
year in (2008,2007)) fill
where
fill.year in (2008,2007)
group by
fill.Category
,fill.month
RETURNS:
Category | Month | Rev. This Year | Rev. Last Year
Bikes 1 10 000 0
Bikes 2 12 000 11 000
Bikes 3 12 000 11 500
Bikes 4 0 15 400
Bikes 5 0 0
Bikes 6 0 0
Bikes 7 0 0
Bikes 8 0 0
Note that most reporting tools will do this crosstab or matrix functionality, and now that i think of it SQL Server 2005 has pivot syntax that will do this as well.
Here are some additional resources.
CASE
https://web.archive.org/web/20210728081626/https://www.4guysfromrolla.com/webtech/102704-1.shtml
SQL SERVER 2005 PIVOT
http://msdn.microsoft.com/en-us/library/ms177410.aspx

#Christian -- markdown editor -- UGH; especially when the preview and the final version of your post disagree...
#Christian -- full outer join -- the full outer join is overruled by the fact that there are references to SP1 in the WHERE clause, and the WHERE clause is applied after the JOIN. To do a full outer join with filtering on one of the tables, you need to put your WHERE clause into a subquery, so the filtering happens before the join, or try to build all of your WHERE criteria onto the JOIN ON clause, which is insanely ugly. Well, there's actually no pretty way to do this one.
#Jonas: Considering this:
Also, the report is actually for financial year - so I would love to have empty columns with 0 in both if there was no sales in say month 5 for either 2007 or 2008.
and the fact that this job can't be done with a pretty query, I would definitely try to get the results you actually want. No point in having an ugly query and not even getting the exact data you actually want. ;)
So, I'd suggest doing this in 5 steps:
1. create a temp table in the format you want your results to match
2. populate it with twelve rows, with 1-12 in the month column
3. update the "This Year" column using your SP1 logic
4. update the "Last Year" column using your SP2 logic
5. select from the temp table
Of course, I guess I'm working from the assumption that you can create a stored procedure to accomplish this. You might technically be able to run this whole batch inline, but that kind of ugliness is very rarely seen. If you can't make an SP, I suggest you fall back on the full outer join via subquery, but it won't get you a row when a month had no sales either year.

The trick is to do a FULL JOIN, with ISNULL's to get the joined columns from either table. I usually wrap this into a view or derived table, otherwise you need to use ISNULL in the WHERE clause as well.
SELECT
Program,
Month,
ThisYearTotalRevenue,
PriorYearTotalRevenue
FROM (
SELECT
ISNULL(ThisYear.Program, PriorYear.Program) as Program,
ISNULL(ThisYear.Month, PriorYear.Month),
ISNULL(ThisYear.TotalRevenue, 0) as ThisYearTotalRevenue,
ISNULL(PriorYear.TotalRevenue, 0) as PriorYearTotalRevenue
FROM (
SELECT Program, Month, SUM(TotalRevenue) as TotalRevenue
FROM PVMonthlyStatusReport
WHERE Year = #FinancialYear
GROUP BY Program, Month
) as ThisYear
FULL OUTER JOIN (
SELECT Program, Month, SUM(TotalRevenue) as TotalRevenue
FROM PVMonthlyStatusReport
WHERE Year = (#FinancialYear - 1)
GROUP BY Program, Month
) as PriorYear ON
ThisYear.Program = PriorYear.Program
AND ThisYear.Month = PriorYear.Month
) as Revenue
WHERE
Program = 'Bikes'
ORDER BY
Month
That should get you your minimum requirements - rows with sales in either 2007 or 2008, or both. To get rows with no sales in either year, you just need to INNER JOIN to a 1-12 numbers table (you do have one of those, don't you?).

About the markdown - Yeah that is frustrating. The editor did preview my HTML table, but after posting it was gone - So had to remove all HTML formatting from the post...
#kcrumley I think we've reached similar conclusions. This query easily gets real ugly. I actually solved this before reading your answer, using a similar (but yet different approach). I have access to create stored procedures and functions on the reporting database. I created a Table Valued function accepting a product category and a financial year as the parameter. Based on that the function will populate a table containing 12 rows. The rows will be populated with data from the view if any sales available, if not the row will have 0 values.
I then join the two tables returned by the functions. Since I know all tables will have twelve roves it's allot easier, and I can join on Product Category and Month:
SELECT
SP1.Program,
SP1.Year,
SP1.Month,
SP1.TotalRevenue AS ThisYearRevenue,
SP2.TotalRevenue AS LastYearRevenue
FROM GetFinancialYear(#Category, 'First Look', 2008) AS SP1
RIGHT JOIN GetFinancialYear(#Category, 'First Look', 2007) AS SP2 ON
SP1.Program = SP2.Program AND
SP1.Month = SP2.Month
I think your approach is probably a little cleaner as the GetFinancialYear function is quite messy! But at least it works - which makes me happy for now ;)

I could be wrong but shouldn't you be using a full outer join instead of just a left join? That way you will be getting 'empty' columns from both tables.
http://en.wikipedia.org/wiki/Join_(SQL)#Full_outer_join

Using pivot and Dynamic Sql we can achieve this result
SET NOCOUNT ON
IF OBJECT_ID('TEMPDB..#TEMP') IS NOT NULL
DROP TABLE #TEMP
;With cte(Category , Revenue , Yearh , [Month])
AS
(
SELECT 'Bikes', 10000, 2008,1 UNION ALL
SELECT 'Bikes', 12000, 2008,2 UNION ALL
SELECT 'Bikes', 12000, 2008,3 UNION ALL
SELECT 'Bikes', 15000, 2008,1 UNION ALL
SELECT 'Bikes', 11000, 2007,2 UNION ALL
SELECT 'Bikes', 11500, 2007,3 UNION ALL
SELECT 'Bikes', 15400, 2007,4
)
SELECT * INTO #Temp FROM cte
Declare #Column nvarchar(max),
#Column2 nvarchar(max),
#Sql nvarchar(max)
SELECT #Column=STUFF((SELECT DISTINCT ','+ 'ISNULL('+QUOTENAME(CAST(Yearh AS VArchar(10)))+','+'''0'''+')'+ 'AS '+ QUOTENAME(CAST(Yearh AS VArchar(10)))
FROM #Temp order by 1 desc FOR XML PATH ('')),1,1,'')
SELECT #Column2=STUFF((SELECT DISTINCT ','+ QUOTENAME(CAST(Yearh AS VArchar(10)))
FROM #Temp FOR XML PATH ('')),1,1,'')
SET #Sql= N'SELECT Category,[Month],'+ #Column +'FRom #Temp
PIVOT
(MIN(Revenue) FOR yearh IN ('+#Column2+')
) AS Pvt
'
EXEC(#Sql)
Print #Sql
Result
Category Month 2008 2007
----------------------------------
Bikes 1 10000 0
Bikes 2 12000 11000
Bikes 3 12000 11500
Bikes 4 0 15400

Related

How to display Row data to column in SQL?

I have a table like this
Id
Year
Category
Type
Item
Price
1
2010
Cloth
Small
Red
25
2
2010
Cloth
Large
Blue
30
3
2010
Laptop
Small
Blue
15
4
2011
Cloth
Small
Red
22
5
2011
Cloth
Large
Blue
28
6
2011
Laptop
Large
Red
33
7
2012
Laptop
Small
Blue
35
I want the output in this format. I want to bring year as columns and display the prices for those years.
Important Note: In output, all years have price and other data. In this example, it would be long, so I have kept only few rows. But in my database, basically all years will have data. There will be price for each year for each category, type and Item
Some important things to note:
This table has over 50,000 records
There are maximum 2 possible value for Category i.e Cloth and Laptop. Similarly 2 for Type like Small and Large. And for item also 2 i.e Red and Blue. But for Year it can be 2000 to 2021.
How can I select the records and display the result in this way in MS-SQL ?
I'd generally advise against that kind of structure, but if that is a specific requirement, you could find a scalable solution with PIVOT (Microsoft documentation) and a stored procedure with variables (fully dynamic example).
If you're find hand-coding these, a CASE statement would work, where you group by Category, ID and Type and then aggregate as:
CASE(WHEN Year = 2012 THEN Price END) AS `2012`
CASE(WHEN Year = 2011 THEN Price END) AS `2011`
Etc.
You can try to use condition aggregate function to make the pivot
;WITH CTE AS (
SELECT Category,
Type,
Item,
MAX(CASE WHEN Year = 2010 THEN Price END) '2010',
MAX(CASE WHEN Year = 2011 THEN Price END) '2011',
MAX(CASE WHEN Year = 2012 THEN Price END) '2012'
FROM T
GROUP BY Category,Type,Item
)
SELECT ROW_NUMBER() OVER(ORDER BY Category) newID,*
FROM T
EDIT
if there are a lot of rows need to do you might try to use dynamic pivot.
DECLARE #sql nvarchar(max) = N'',
#s1 nvarchar(max) = '';
;WITH CTE AS (
SELECT MIN(Year) s_y,MIN(Year) s_e
FROM T
UNION ALL
SELECT s_y + 1 , s_e
FROM CTE
WHERE s_y + 1 <= s_e
)
SELECT #s1 += CONCAT('MAX(CASE WHEN Year = ',s_y,' THEN Price END) [',s_y,'],')
FROM CTE
SET #sql = N'SELECT Category,
Type,' + #s1 + N'Item FROM T GROUP BY Category,Type,Item'
PRINT #sql;
EXEC sys.sp_executesql #sql;

Forcing empty rows from query

I have a table containing monthly statistics for clients.
Columns are CustNo, Year, Month, Trips
Some customers do not have any trips in some months and therefore there are combinations of CustNo, Year and Month that have no rows in that table.
I am trying to write a Query that shows 0 for those combinations of CustNo, Year and Month that have no trips, instead of producing an empty row.
To start with I have created a ValidPeriods table that has a Year and a Month column containing those periods that are valid.
I can then Query like this:
SELECT v.ValidYear, v.ValidMonth, tc.CustNo, tc.Trips
FROM ValidPeriods v
LEFT OUTER JOIN TempTrips AS tc ON v.ValidYear = tc.Year
AND v.ValidMonth = tc.Month
WHERE tc.CustNo IN (1001230, 1001286, 1001292)
This will give me rows for all periods, with 1 row with NULL values for those periods where there are no customers in the list that have any trips.
But how do I get one row for each customer in the list for all periods?
Ideally I want this:
2016 1 1001230 0
2016 1 1001286 14
2016 1 1001292 23
2016 2 1001230 7
2016 2 1001286 0
2016 2 1001292 4
etc...
Generate the rows using cross join. Then fill in the values using left join:
SELECT ym.ValidYear, ym.ValidMonth, c.CustNo, COALESCE(tt.Trips, 0)
FROM ValidPeriods ym CROSS JOIN
(VALUES (1001230), (1001286), (1001292)) c(CustNo) LEFT JOIN
TempTrips tt
ON tt.ValidYear = ym.ValidYear AND tt.ValidMOnth = ym.ValidMonth AND
tt.CustNo = c.CustNo;

SQL query to duplicate each row 12 times

I have a table which has columns site,year and sales . this table is unique on site+year eg
site year sales
-------------------
a 2012 50
b 2013 100
a 2006 35
Now what I want to do is make this table unique on site+year+month. Thus each row gets duplicated 12 times, a month column is added which is labelled from 1-12 and the sales values get divided by 12 thus
site year month sales
-------------------------
a 2012 1 50/12
a 2012 2 50/12
...
a 2012 12 50/12
...
b 2013 1 100/12
...
a 2006 12 35/12
I am doing this on python currently and it works like a charm, but I need to do this in SQL (ideally PostgreSQL since I will be using this as a datasource for tableau)
It would be very helpful if someone can provide the explanations with the solution as well, since I am a novice at this
You can use generate_series() for that
select t.site, t.year, g.month, t.sales / 12
from the_table t
cross join generate_series(1,12) as g (month)
order by t.site, t.year, g.month;
If the column sales is an integer, you should cast that to a numeric to avoid the integer division: t.sales::numeric / 12
Online example: http://rextester.com/GUWPI39685
Try this approach (For T-SQL - MS SQL) :
DECLARE #T TABLE
(
[site] VARCHAR(5),
[year] INT,
sales INT
)
INSERT INTO #T
VALUES('A',2012,50),('B',2013,100),('C',2006,35)
;WITH CTE
AS
(
SELECT
MonthSeq = 1
UNION ALL
SELECT
MonthSeq = MonthSeq+1
FROM CTE
WHERE MonthSeq <12
)
SELECT
T.[site],
T.[year],
[Month] = CTE.MonthSeq,
sales = T.[sales]/12
FROM CTE
CROSS JOIN #T T
ORDER BY T.[site],CTe.MonthSeq

Sum values in one column and add to another table

My Table(BOB) is look like this:
Year Month Value
2010 1 100
2010 2 100
2010 3 100
2010 4 100
2010 5 100
I would like to add YTD values to another table (BOB2)
more exactly I want to see BOB 2 table like
Year Month Value
2010 1 100
2010 2 200
2010 3 300
2010 4 400
2010 5 500
See the answer below. I have simplified the query.
select
concat(cast(t1.year as char), cast(t1.month as char)) period_current,
sum(t1.amount) amount
from bob t1
left join bob t2 on
(t2.year + t2.month) <= (t1.year + t1.month)
group by
(t1.year + t1.month);
What the query is doing is using t1 as the base table and joining on the period (year + month) then you want to sum the amounts prior to that including the current amount. I haven't added in all the edge cases, but this gives you something to start from. If you are restricting your query to a single year, this should be enough.
Well, I think I understand what you are trying to do.. but if not, please re-phrase your question... You can accomplish what you have asked by using the following SQL.
--INSERT INTO BOB2 (Year, ID, Value)
SELECT a.Year, a.ID, (SELECT SUM(b.Value)
FROM BOB b
WHERE b.ID <= a.ID) as RunningTotalValue
FROM BOB a
ORDER BY a.Value;
Here is a SQLFiddle for you to look at.
EDIT: Change the ID column to "Month" after seeing the edit to your post.

Summing two tables into one table in Jet SQL

Another SQL question if I may.
I have two tables (generated from TRANSFORM - PIVOT queries in Jet SQL)
Category ID Account Jan Feb ... Dec
1 1 Cash 10 20 30
1 2 Card 100 200 300
1 3 Savings 200 400 600
and
Category ID Account Jan Feb ... Dec
1 1 Cash -5 -10 -20
1 2 Card -100 -200
1 3 Savings -100 -400
Category, ID and Account will always be the same in two tables. There will be no accounts that occur in one table that don't occur in others. There may be NULL values in either table but there will always be a matching cell in each table.
What I would like is
Category ID Account Jan Feb ... Dec
1 1 Cash 5 10 10
1 2 Card 100 100 100
1 3 Savings 100 400 200
I have played around with UNION and JOIN queries but can't get there.
Thanks again,
Andy
Edit - The original queries are
TRANSFORM Sum(Items.amount) AS total
SELECT Accounts.accCategory, Accounts.ID, Accounts.comment AS Account
FROM Accounts INNER JOIN Items ON Accounts.ID = Items.accFrom
WHERE (((Year([idate]))=2013) AND ((Items.category)<>3 Or (Items.category) Is Null) AND ((Accounts.accCategory)=6 OR (Accounts.accCategory)=7) AND ((Accounts.curr)=1))
GROUP BY Accounts.accCategory, Accounts.ID, Accounts.comment
PIVOT Format(idate,'mmm') IN ('Jan','Feb','Mar','Apr', 'May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
and
TRANSFORM Sum(Items.amount) AS total
SELECT Accounts.accCategory, Accounts.ID, Accounts.comment AS Account
FROM Accounts INNER JOIN Items ON Accounts.ID = Items.accFrom
WHERE (((Year([idate]))=2013) AND ((Items.category)=3) AND ((Items.comment)='Monthly') AND ((Accounts.accCategory)=6) AND ((Accounts.curr)=1))
GROUP BY Accounts.accCategory, Accounts.ID, Accounts.comment
PIVOT Format(idate,'mmm') In ('Jan','Feb','Mar','Apr', 'May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
One approach using the join of the 2 tables deb and cred is as follows:
SELECT cred.accCategory, cred.ID, cred.Account, [deb].[Jan]+[cred].[Jan] AS tot_Jan
FROM cred INNER JOIN deb ON (cred.Account = deb.Account) AND (cred.ID = deb.ID) AND (cred.accCategory = deb.accCategory);
This screenshot shows how the query is constructed:
And here are the results (excuse the table formatting):
accCategory ID Account tot_Jan
1 1 Cash 5
1 2 Card 100
1 3 Savings 100
Be warned that the JOIN is an equi-JOIN so it means the same number of records must exist in BOTH tables else you will find that credits where a debit does not exist will be dropped! So make sure that both input tables are consistent with the same key values (the 3 GROUP BY fields in your earlier PIVOT query).
The alternative approach is the UNION with a GROUP BY to calculate the total from it. I will paste that shortly.
========== EDIT ============
You can build this up in stages. First I did SELECT * FROM cred UNION SELECT * FROM deb and looked at in Design View.
Went back into the SQL view and made sure the UNION query has brackets around it and SELECT sub.* FROM before the brackets and AS SUB after the brackets.
Then I modified the query to include the GROUP BY and Sum(Jan) bits. The final SQL is here and a screenshot follows.
SELECT sub.accCategory, sub.ID, sub.Account, Sum(sub.Jan) AS SumOfJan
FROM (SELECT * FROM cred UNION SELECT * FROM deb) AS sub
GROUP BY sub.accCategory, sub.ID, sub.Account;