Columns to Rows in SQL Server - sql

I have a query returning a table which looks like:
Location | November | December | January | February | March | ... | October |
CT 30 70 80 90 60 30
etc.
and I'd like it to look like:
Location | Month | Value |
CT November 30
CT December 70
CT January 80
...
CT October 30
It looks like an unpivot, but I didn't pivot to get it into that form since the base table has the months as columns (the values are just sums of values grouped by location). I've seen plenty of rows-to-columns questions but I haven't found a good columns-to-rows answer, so I'm hoping someone can help me.
Thanks!

You will want to use the UNPIVOT function. This transforms the values of your columns and turns it into rows:
select location, month, value
from <yourquery here>
unpivot
(
value
for month in (January, February, March, April, May, June, July, August,
September, October, November, December)
) unpiv
See SQL Fiddle with Demo

Related

Aggregate multiple invoice numbers and invoice amount rows into one row

I have the following:
budget_id
invoice_number
April
June
August
004
11
NULL
690
NULL
004
12
1820
NULL
NULL
004
13
NULL
NULL
890
What I want to do is do the following:
budget_id
invoice_number
April
June
August
004
11, 12, 13
1820
690
890
However, when I try to do the following:
SELECT budget_id,
STRING_AGG(invoice_number, ',') AS invoice number,
April,
June,
August
FROM invoice_table
GROUP BY budget_id,
April,
June,
August
Nothing happens. The table stays exactly the same. The code above works if I'm able to comment out the months as it aggregates the invoices numbers without the months. But once I include the months, I still get 3 separate rows. I need the invoice amounts to be included with the months. Is it possible to get the invoice numbers aggregated as well as the invoice amounts in one row? I'm using Big Query if that helps.
Use below query,
SELECT budget_id,
STRING_AGG(invoice_number, ',') invoice_number,
SUM(April) April,
SUM(June) June,
SUM(August) August
FROM invoice_table
GROUP BY 1;

How to manage complex sorting in SQL?

I need to have date sorting with the partial dates. I have a table with the following columns.
Day Month Year
-- ---- -----
NULL 03 1990
26 10 1856
03 07 Null
31 NULL 2018
NULL NULL NULL
I have a grid in which One of the column is Date where I am combining the above three columns and displays the dates.
Now I want sorting on this date column in the grid. The sort order of the dates should be like following :
[blank date]
22 [day]
March
April 12
May
July 29
August
September
September 14
October
1948
October 1948
October 1 1948
July 1976
1977
July 1977
July 23 1977
December 1981
December 29 1981
I have tried various ways to achieve this. But I am not able to get the desired result. Following are some of the ways I have applied.
I have tried sorting by creating the stored procedure in which I am creating the whole date by combining 3 columns and converting them in standard date formats and comparing the values. I have also tried by creating the computed property in the model and sorting them accordingly.
How can I do this in SQL?
I think you could do:
order by coalesce(year, '0000'), coalesce(month, '00'), coalesce(day, '')
You can be more explicit, but this puts the NULL values before the other values in the column.
Note: This uses the SQL standard operator for string concatenation. Not all databases support this, so you might need to tweak the code for your database.

Oracle SQL Special Period Format

I have a special fiscal period in format YYYYMMM, for example
Feb of 2015 is 2015002
Nov of 2014 is 2014011
I need to do subtraction from the period, 2 months ago from 2015002 is 2014012, but i cant do like
SELECT '2015001' - 2 FROM DUAL
How can i do that?
You should first convert it to a date, then subtract months and convert back to the format you need.
with x(y) as (
select '2015002' from dual
)
select y,
to_date(y,'YYYY"0"MM'),
add_months(to_date(y,'YYYY"0"MM'),-2),
to_char(add_months(to_date(y,'YYYY"0"MM'),-2),'YYYY"0"MM')
from x
Results:
| Y | TO_DATE(Y,'YYYY"0"MM') | ADD_MONTHS(TO_DATE(Y,'YYYY"0"MM'),-2) | TO_CHAR(ADD_MONTHS(TO_DATE(Y,'YYYY"0"MM'),-2),'YYYY"0"MM') |
|---------|----------------------------|---------------------------------------|------------------------------------------------------------|
| 2015002 | February, 01 2015 00:00:00 | December, 01 2014 00:00:00 | 2014012 |

Teradata Default List

There is this one table which contains the amounts and states that I need. However, this table contains a year information but I want month. For example, in the table it shows information for Kentucky for 2011..and thats it. For California it shows about 5 different years. But I need it to repeat by month.
So if in 2011 Kentucky had 12 total, then I need a query that shows 12 for January, February, May....repeatedly
Right now I get this output with a dumb query I have:
Kentucky 12 January
California 800 January
This is done easily by grouping by State, Quantity and Month
I want to make sure that no matter what the Quantity is, each State has ALL months
Kentucky 12 January
Kentucky 12 February
Kentucky 12 May
California 800 January
California 800 February
California 800 May
Any idea on how to do this with Teradata SQL?
The overall query would look something like this:
SELECT
state_quantities.state,
state_quantities.quantity,
all_months.month_name
FROM state_quantities
CROSS JOIN (
...
) all_months
What goes between the brackets for all_months depends on what you mean by "all months".
If you mean all months that appear in state_quantities irrespective of state (so if you have Kentucky with January, California with February and Florida with May, you'd only get those three months) you could use something like this:
SELECT
month_name
FROM state_quantities
GROUP BY month_name
If you want all 12 months, you would join to a table containing all 12 months. In the absence of that, you could use sys_calendar.calendar (syntax below might be off):
SELECT
CAST(calendar_date AS DATE FORMAT 'MMM') AS month_name
FROM sys_calendar.calendar
GROUP BY month_name

Fast SQL question about multiple month dates

SELECT DISTINCT MonthName(Month([Date])) AS [Month], tblSupportCalls.System, Count(tblSupportCalls.System) AS [Total for System], Year([Date]) AS [Year]
FROM tblSupportCalls
WHERE (((tblSupportCalls.Date) Between Now() And Now()-7) AND ((tblSupportCalls.System) In ('Career Campus','E-PEEP')) AND ((tblSupportCalls.StartTime) Is Not Null) AND ((Hour([StartTime]))>=7))
GROUP BY tblSupportCalls.System, tblSupportCalls.Date;
Whenever I input that it gives me multiple months like:
July
July
July
I want it just to say July just 1 time and I cant figure out why ever field is repeating itself for different days. I just want it to say:
MONTH | System | Total Systems | Year
what I am looking at is
> MONTH | System | Total Systems | Year
> July CC 2 2010
> July CC 7 2010
> July CC 9 2010
> July EE 1 2010
> July EE 2 2010
Needs to be:
MONTH | System | Total Systems | Year
July CC 18 2010
July EE 03 2010
You'd want to group by your Year and Month instead of tblSupportCalls.Date.
...
GROUP BY tblSupportCalls.System, Year([Date]), Month([Date]);
You're still grouping the results by date, though (not by month). I think what you want is something like this:
GROUP BY tblSupportCalls.System, Year ([tblSupportCalls.Date]), [Month([tblSupportCalls.Date]);
I'm thinking you still want to group by Year as well, so that July 2010 shows up in a different row from July 2011.