SQL - Transpose query data with additional rows added in [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have come once more to seek your guidance.
I am not actually sure how possible this is, but I can only hope.
I have an already heavily joined and aggregated query which pulls out results as shown in the attached picture.
As mentioned, this is an already heavily editing query with multiple joins and aggregates to take half hour data and sum it hourly.
I am wondering if it is at all possible to now transpose the data so that there are 24 seperate hourly entries for each date?
So instead of having one single line for 2014-01-01 with each hourly reading in a seperate column, is it at all possible to change it so there are just two columns. One for date/time and one with the hourly total.
So it would look more like:
and then continue down for each hour of each day.
I am using SQL Server 2014 if that helps.
Thank you in advance!

Assuming your time columns are named time0, time1, ..., time23 you could write
SELECT DateAdd(HOUR, 0, date) as DateTime, time0 as total from table
UNION
SELECT DateAdd(HOUR, 1, date) as DateTime, time1 as total from table
UNION
...
SELECT DateAdd(HOUR, 23, date) as DateTime, time23 as total from table

Related

SQL - IF clause within WHERE [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
Improve this question
Good Day,
I am trying to apply if or case statement in SQL.
Ex. The marketing plan extended to more stores after a certain date:
First phase starting from Aug 1: only 1 store
Second phase starting from Sept 1: 3 stores
Last phase starting from Oct 1: all stores
So I have everything set except for the WHERE clause where I need to include if or case statement with the following logic:
WHERE
1=1
AND
Pseudo code, something along this line:
IF DATE >=20220801 AND <=20220831, STORE IN (1)
ELIF DATE >=20220901 AND <=20220930, STORE IN (1, 2, 3)
ELIF DATE >= 20221001, all stores
If anyone can point me to the right direction I would much appreciate.
Simply combine your conditions with AND and OR:
... WHERE (DATE >= '01-AUG-22' AND DATE < '01-SEP-22' AND STORE IN (1))
OR (DATE >= '01-SEP-22' AND DATE < '01-OCT-22' AND STORE IN (1, 2, 3))
OR (DATE >= '01-OCT-22')

How could I add a column which contains dates in my SQL query to get the monthly data [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 months ago.
Improve this question
I have two tables with many columns. One contains 50k custIDs, date & Payments received for every id and the other table contains 5k custIDs. I need to find How much payment received per CustId on monthly basis for these 5k CustIDs ?
Assuming your dates are stored as date...
select i.custId, i.custName,
to_char(p.payment_date,'yyyy-mm') as payment_month,
sum(p.amount_paid) as total_amount_paid
from customers i
join payments p
on i.custId = p.custId
group by i.custId, i.custName, to_char(p.payment_date,'yyyy-mm')

How to use group and sum same data based on different criteria on same table [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I am trying to sum data in a table.
Data in my table:
Month Osl Sale
10 8-02-01-01 38440.5
10 8-02-01-03 14961
10 8-03-02-01 10388.3
10 8-05-04-01 81666.6
10 8-05-04-05 29431.8
10 8-07-01-09 9821.4
10 8-09-01-01 7567.5
And my expected output is:
I think union all is the simplest method:
select month, osl, sale
from t
union all
select month, left(osl, 7), sum(sale)
from t
group by month, left(osl, 7);
Not all databases support left(). In those that don't, either substr() or substring() can extract the first seven characters.
Unsure of your RDMS, but I would suggest something along the lines of:
select * from table1
union all
select t.month, left(t.osl, len(t.osl)-3), sum(t.sale)
from table1 t
group by t.month, left(t.osl, len(t.osl)-3)
Change table1 to your table name as appropriate.

Date calculation subtracting date [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a field with sales_start_date( values like 2014-06-17 ,2015-07-23...)
I need to do calculation based on sales_start date..
I have to take the most recent month-end date(i have to force this value to 2016-07-31) and subtract the sale_start_date to get an integer count of the days elapsed. If number of days passed is less than 2 default the value 2
sales_start_date
2016-01-01
2016-07-30
Output
Calculated field
155
2
Can any one help me in writing case statement.
Your help is much appreciated.
Thank You,
Swathi.
You basically need to use DATEDIFF
SELECT DATEDIFF(day,<enter start date column here>,<most recent month-end date column here>) AS [Calculated field]
FROM <your table here>
SQL-server 2012 + you have the EOMONTH() to help you get to the end of the month more easily.
;WITH cteData AS (
SELECT CAST('2016-01-01' AS DATE) as Start_date
UNION ALL
SELECT '2016-07-30'
)
SELECT
EOMONTH(DATEADD(MONTH,-1,GETDATE())) as EndOfPreviousMonth
,DATEDIFF(DAY,Start_date,EOMONTH(DATEADD(MONTH,-1,GETDATE()))) + 1 as DaysDifferent
FROM
cteData

SQL Comparison of Dates [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Okay, I have a situation when I need to compare a column from a table with today's date. If the value in a particular row is earlier than today (in other words, the date has already passed, I need to mark a virtual column with the phrase 'lapsed'
Below is the SQL (SQL Server 2012) that I have been using:
SELECT
datediff(day, sysdatetime(), policy_expiration_dt) As 'DayDiff'
,Case When 'DayDiff' < 0 Then 'Lapsed'
FROM TABLE_NAME
The first column that includes the datediff function makes the comparison and the second column marks it as lapsed if the date has passed.
basically the order of how an sql statement is executed does not allow you to use the alias. so you will need to use the datediff() function in the CASE statement.
The order of sql queries execution is:
1. FROM
2. ON
3. OUTER
4. WHERE
5. GROUP BY
6. CUBE | ROLLUP
7. HAVING
8. SELECT
9. DISTINCT
10. ORDER BY
11. TOP
you need to do this:
SELECT DATEDIFF(day, SYSDATETIME(), policy_expiration_dt) AS 'DayDiff'
, CASE
WHEN DATEDIFF(day, SYSDATETIME(), policy_expiration_dt) < 0 THEN 'Lapsed'
END
FROM TABLE_NAME;