SQL query to duplicate each row 12 times - sql

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

Related

Formatting SQL Server table data for reporting

I have a table with the columns as below -
There are rows showing the allocation of various people under various project. The months (columns) can extend to Dec,20 and continue on from Jan,21 in the same pattern as above.
One Staff can be tagged to any number of projects in a given month.
Now to prepare a report on this I would like to format the data as below -
So basically for each project that a staff is assigned to, I would like to duplicate each of the 12 months for each year and show the designated allocation.
The name of the table containing the data is [Staff Allocation] and it has the following fields - [Staff ID],[Project ID],[Jan,20],[Feb,20],[Mar,20],[Apr,20] and so on as per the image above.
Is there any way to do this?
Any help on this is highly appreciated.
Adding in the sample data as below -
Staff ID
Project ID
Jan,20
Feb,20
Mar,20
Apr,20
May,20
Jun,20
Jul,20
1
20
0
0
0
100
80
10
0
1
30
0
0
0
0
20
90
100
2
20
100
100
100
0
0
0
0
3
50
80
100
0
0
0
0
0
3
60
15
0
0
0
20
0
0
3
70
5
0
100
100
80
0
0
create table test(StaffID int, ProjectID int, Jan20 int, Feb20 int, Mar20 int, Apr20 int, May20 int, Jun20 int, Jul20 int)
insert into test values
(1,20,0,0,0,100,80,10,0),
(1,30,0,0,0,0,20,90,100),
(2,20,100,100,100,0,0,0,0),
(3,50,80,100,0,0,0,0,0),
(3,60,15,0,0,0,20,0,0),
(3,70,5,0,100,100,80,0,0)
Select * from test
I get the sense (by your column names) that the source table will expand over time.
Here is an option that will dynamically unpivot your data without actually using Dynamic SQL
Example or dbFiddle
Select A.[Staff ID]
,A.[Project ID]
,[Month] = left([Key],3)
,[Year] = '20'+right([Key],2)
,Allocation = try_convert(int,B.[value])
From YourTable A
Cross Apply (
Select [Key]
,[Value]
From OpenJson( (Select A.* For JSON Path,Without_Array_Wrapper ) )
Where [Key] not in ('Staff ID','Project ID')
) B
Results
Here is an example of how you can unpivot your columns into rows by using cross apply and a values table constructor.
select StaffId, ProjectId, v.*
from t
cross apply(values
('Jan', 2020, Jan20),
('Feb', 2020, Feb20),
('Mar', 2020, Mar20),
('Apr', 2020, Apr20),
('May', 2020, May20),
('Jun', 2020, Jun20),
('Jul', 2020, Jul20)
)v([Month], [Year], Allocation);
Demo DB<>Fiddle
you need Unpivot and since your column names are in format of MMMYY.you can use Right and Left Functions as follows:
select [StaffID]
,[ProjectID]
,left(indicatorname,3) Month
,concat('20',right(indicatorname,2)) Year
,Allocation
from test
unpivot
(
Allocation
for indicatorname in ([Jan20]
,[Feb20]
,[Mar20]
,[Apr20]
,[May20]
,[Jun20]
,[Jul20]
)
) unpiv;
dbfiddle

LAG function alternative. I need the results for the missing year in between

I have this table so far. However, I would like to obtain the results for 2019 which there are no records so it becomes 0. Are there any alternatives to the LAG funciton.
ID
Year
Year_Count
1
2018
10
1
2020
20
Whenever I use the LAG function in SQL it gives me the results for 2018. However, I would like to get 0 for 2019 and then 10 for 2018
LAG(YEAR_COUNT) OVER (PARTITION BY ID ORDER BY YEAR) AS previous_year_count
untested notepad scribble
CASE
WHEN 1 = YEAR - LAG(YEAR) OVER (PARTITION BY ID ORDER BY YEAR)
THEN LAG(YEAR_COUNT) OVER (PARTITION BY ID ORDER BY YEAR)
ELSE 0
END AS previous_year_count
I'll add on to Nick's comment here with an example.
The YEARS CTE here is creating that table of years as he suggested, the RECORDS table is matching the above posted. Then they get joined together with COALESCE to fill in the null values left by the LEFT JOIN (filled ID with 0, not sure what your case would be).
You would need to LEFT JOIN onto the YEAR table and select the YEAR variable from the YEAR table in the final query, otherwise you'd only end up with only 2018/2020 or those years and some null values
WITH
YEARS AS
(
SELECT 2016 AS YEAR UNION ALL
SELECT 2017 UNION ALL
SELECT 2018 UNION ALL
SELECT 2019 UNION ALL
SELECT 2020 UNION ALL
SELECT 2021 UNION ALL
SELECT 2022
)
,
RECORDS AS
(
SELECT 1 ID, 2018 YEAR, 10 YEAR_COUNT UNION ALL
SELECT 1, 2020, 20)
SELECT
COALESCE(ID, 0) AS ID,
Y.YEAR,
COALESCE(YEAR_COUNT, 0) AS YEAR_COUNT
FROM YEARS AS Y
LEFT JOIN RECORDS AS R
ON R.YEAR = Y.YEAR
Here is the dbfiddle so you can visualize - https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=9e777ad925b09eb8ba299d610a78b999
Vertica SQL is not an available test environment, so this may not work directly but should at least get you on the right track.
The LAG function would not work to get 2019 for a few reasons
It's a window function and can only grab from data that is available - the default for LAG in your case appears to be 1 aka LAG(YEAR_COUNT, 1)
Statements in the select typically can't add any rows data back into a table, you would need to add in data with JOINs
If 2019 does exist in a prior table and you're using group by to get year count, it's possible that you have a where clause excluding the data.

Build table with previous months (cumulative)

I'm a bit lost with the following problem that I need to solve with an SQL query, no plsql. The idea is to build a cumulative column to calculate all previous months. The input table looks like
Month
1
2
3
..
24
I need build the following table :
Month Cum_Month
1 1
2 1
2 2
3 1
3 2
3 3
..
24 1
...
24 23
All this in SQL Server 2008, thanks in advance
You can do it like this:
DECLARE #tbl TABLE ([Month] INT)
INSERT #tbl VALUES
(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),
(11),(12),(13),(14),(15),(16),(17),(18),(19),(20),(21),(22),(23),(24)
SELECT Month
, ROW_NUMBER() OVER (PARTITION BY Month ORDER BY Month) num
FROM #tbl a
JOIN
(
SELECT *
FROM master..spt_values
WHERE type = 'P'
)
b ON b.number < a.Month
master..spt_values is used to generate numbers, after numbers are generated result of the subquery is joined on the #tbl to get the number of rows that corresponds to the month. After that ROW_NUMBER is used to create appropriate ordinal numbers for each month.
Here's a pretty cool trick not using any tables:
SELECT N.Number as Month, N2.Number as Cum_Month
FROM
(SELECT Number FROM master..spt_values WHERE Number BETWEEN 1 AND 24 AND Type = 'P') N
JOIN (SELECT Number FROM master..spt_values WHERE Number BETWEEN 1 AND 24 AND Type = 'P') N2 ON N.Number >= N2.Number
ORDER BY N.Number, N2.Number
And the Fiddle.
And if you really don't want the last 24 24 (why not), just change the second query to between 1 and 23).

How do i create a query that will give me a cumulative total?

I have a table with the following columns: reportDate DATETIME and losses CURRENCY and of course the ID column.
How do I write a query that will return a table with a running total of the losses column? Each date will have multiple entries so i think they will need use Sum() for each date. I know this has to do with the DSum function but im still lost on this one. It should look something like
Month Losses Cum
----- ------ -----
Jan $3,000 $3,000
Feb $2,000 $5,000
Mar $1,500 $6,500
Having a sql statement that's not Access specific would be the most help to me, I think. But all solutions are appreciated. Thanks for the help.
I found table and field names in the edit history of your question, so used those names in this answer. You didn't provide record_matYields sample data, so I created my own and hope it is suitable:
id reportDate gainOrLoss
1 12/28/2011 $1,500.00
2 12/29/2011 $500.00
3 12/30/2011 $1,000.00
4 1/2/2012 $10.00
5 1/3/2012 $4,500.00
6 1/4/2012 $900.00
First I created qryMonthlyLosses. Here is the SQL and the output:
SELECT
Year(reportDate) AS reportYear,
Month(reportDate) AS reportMonth,
Min(y.reportDate) AS MinOfreportDate,
Sum(y.gainOrLoss) AS SumOfgainOrLoss
FROM record_matYields AS y
GROUP BY
Year(reportDate),
Month(reportDate);
reportYear reportMonth MinOfreportDate SumOfgainOrLoss
2011 12 12/28/2011 $3,000.00
2012 1 1/2/2012 $5,410.00
I used that first query to create another, qryCumulativeLossesByMonth:
SELECT
q.reportYear,
q.reportMonth,
q.MinOfreportDate,
q.SumOfgainOrLoss,
(
SELECT
Sum(z.gainOrLoss)
FROM record_matYields AS z
WHERE z.reportDate < q.MinOfreportDate
) AS PreviousGainOrLoss
FROM qryMonthlyLosses AS q;
reportYear reportMonth MinOfreportDate SumOfgainOrLoss PreviousGainOrLoss
2011 12 12/28/2011 $3,000.00
2012 1 1/2/2012 $5,410.00 $3,000.00
Finally I used qryCumulativeLossesByMonth as the data source in a query which transforms the output to match your requested format.
SELECT
q.reportYear,
MonthName(q.reportMonth) AS [Month],
q.SumOfgainOrLoss AS Losses,
q.SumOfgainOrLoss +
IIf(q.PreviousGainOrLoss Is Null,0,q.PreviousGainOrLoss)
AS Cum
FROM qryCumulativeLossesByMonth AS q;
reportYear Month Losses Cum
2011 December $3,000.00 $3,000.00
2012 January $5,410.00 $8,410.00
You could probably revise this into a single query using subqueries instead of the separate named queries. I used this step-wise approach because I hoped it would be easier to understand.
Edit: I returned the full name with the MonthName() function. If you want the abbreviated month name, pass True as a second parameter to that function. Either of these should work:
MonthName(q.reportMonth, True) AS [Month]
MonthName(q.reportMonth, -1) AS [Month]
This page looks good for you:
http://support.microsoft.com/kb/290136
FYI, I wrote the following T-SQL against SQL Server before:
create table #a (key_col int, val int)
insert into #a values (1, 10)
insert into #a values (2, 10)
insert into #a values (3, 30)
insert into #a values (4, 10)
select x.key_col,x.val,sum(y.val) as cumulated
from #a x
inner join #a y on
x.key_col >= y.key_col
group by x.key_col,x.val
order by x.key_col,x.val
drop table #a
The result:
key_col val cumulated
----------- ----------- -----------
1 10 10
2 10 20
3 30 50
4 10 60
This will do it for you in one SQL without using temporary tables
SELECT Format$([TranDate],"yyyy mm") AS mthYear, First(DSum("[GainOrLoss]","[Trans]","Format$([TranDate],'yyyy mm')='" & [mthYear] & "'")) AS ThisMonth, First(DSum("[GainOrLoss]","[Trans]","Format$([TranDate],'yyyy mm')<='" & [mthYear] & "'")) AS RunningTotal FROM trans GROUP BY Format$([TranDate],"yyyy mm");

SQL query to compare product sales by month

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