SQL Splitting SUM Rows into Columns based on Dates - sql

I am trying to SUM values into columns based on dates.
This is my current SQL
SELECT DISTINCT
FORMAT(dbo.DR_INVLINES.TRANSDATE, 'MMM') AS Month,
dbo.DR_ACCS.NAME,
SUM(dbo.DR_INVLINES.QUANTITY * dbo.STOCK_ITEMS.X_LITERAGE) AS SUMQTY,
FORMAT(dbo.DR_INVLINES.TRANSDATE, 'yy') AS Year
FROM
dbo.DR_INVLINES
INNER JOIN
dbo.DR_TRANS ON dbo.DR_INVLINES.HDR_SEQNO = dbo.DR_TRANS.SEQNO
INNER JOIN
dbo.STOCK_ITEMS ON dbo.DR_INVLINES.STOCKCODE = dbo.STOCK_ITEMS.STOCKCODE
INNER JOIN
dbo.DR_ACCS ON dbo.DR_INVLINES.ACCNO = dbo.DR_ACCS.ACCNO
GROUP BY
FORMAT(dbo.DR_INVLINES.TRANSDATE, 'yy'),
FORMAT(dbo.DR_INVLINES.TRANSDATE, 'MMM'),
dbo.STOCK_ITEMS.STOCKGROUP, dbo.DR_ACCS.NAME
HAVING
(dbo.STOCK_ITEMS.STOCKGROUP = 3)
This query returns this result set:
Mth Name SUMQTY Year
-----------------------------
Apr Company 1 1000 16
Apr Company 2 30790.4 16
Apr Company 3 1840 16
Apr Company 1 6502.9 17
Apr Company 2 2000 17
Apr Company 3 1000 17
What I am trying to achieve is
Mth Name 2016 2017
-------------------------------
Apr Company 1 800 200
Apr Company 2 15000 13000
Apr Company 3 600 569
Apr Company 1 5000 1500
Apr Company 2 2000 1986
Apr Company 3 1000 2543
Can someone please help with this..... I have been racking my brain for ages on this one ;-)

If I understand your question, you wanted to display YEAR as column,
SELECT FORMAT(dbo.DR_INVLINES.TRANSDATE, 'MMM') AS Month,
dbo.DR_ACCS.NAME,
SUM(CASE WHEN YEAR(dbo.DR_INVLINES.TRANSDATE) = 2016
THEN dbo.DR_INVLINES.QUANTITY * dbo.STOCK_ITEMS.X_LITERAGE
ELSE 0
END ) AS [2016],
SUM(CASE WHEN YEAR(dbo.DR_INVLINES.TRANSDATE) = 2017
THEN dbo.DR_INVLINES.QUANTITY * dbo.STOCK_ITEMS.X_LITERAGE
ELSE 0
END ) AS [2017]
FROM dbo.DR_INVLINES
INNER JOIN dbo.DR_TRANS
ON dbo.DR_INVLINES.HDR_SEQNO = dbo.DR_TRANS.SEQNO
INNER JOIN dbo.STOCK_ITEMS
ON dbo.DR_INVLINES.STOCKCODE = dbo.STOCK_ITEMS.STOCKCODE
INNER JOIN dbo.DR_ACCS
ON dbo.DR_INVLINES.ACCNO = dbo.DR_ACCS.ACCNO
WHERE dbo.STOCK_ITEMS.STOCKGROUP = 3
GROUP BY FORMAT(dbo.DR_INVLINES.TRANSDATE, 'yy'),
FORMAT(dbo.DR_INVLINES.TRANSDATE, 'MMM'),
dbo.STOCK_ITEMS.STOCKGROUP,
dbo.DR_ACCS.NAME

Have you tried group by?
SELECT FORMAT(il.TRANSDATE, 'MMM') AS Month, a.NAME,
SUM(CASE WHEN year(il.TRANSDATE) = 2016 THEN anil.QUANTITY * si.X_LITERAGE END) AS SUMQTY_2017,
SUM(CASE WHEN year(il.TRANSDATE) = 2017 THEN anil.QUANTITY * si.X_LITERAGE END) AS SUMQTY_2016
FROM dbo.DR_INVLINES il INNER JOIN
dbo.DR_TRANS t
ON il.HDR_SEQNO = t.SEQNO INNER JOIN
dbo.STOCK_ITEMS si
ON il.STOCKCODE = si.STOCKCODE INNER JOIN
dbo.DR_ACCS a
ON il.ACCNO = a.ACCNO
WHERE si.STOCKGROUP = 3
GROUP BY FORMAT(il.TRANSDATE, 'MMM'), a.NAME;
Note the changes:
Table aliases make the query easier to write and to read.
The having clause is on an unaggregated column, so it should be a where clause.
You had an extra column in the group by.
I would recommend using datepart() or datename() over format().

Related

sql sales data grouped by year in separate columns in postgresql

I have two input tables:
analysis (an_id, an_name, an_cost, an_price, an_group)
orders (ord_id, ord_datetime, ord_an) # orders of analysis (sales)
For every analysis_id I need to show the amount of orders for years 2020 and 2019.
Expected output:
an
year2019
year2020
1
32
41
2
29
28
3
42
37
4
26
35
5
32
34
logic in my query:
step1 - get orders table data only for years 2019,2020 - use CTE and extract()
step2 - aggregate by year
My query:
WITH helper AS (
SELECT an_id,
ord_id,
EXTRACT(year from ord_datetime) as year
FROM analysis a
INNER JOIN orders o ON o.ord_an = a.an_id
WHERE EXTRACT(year FROM ord_datetime) in (2019.0,2020.0)
)
SELECT an_id,
CASE WHEN year = 2019.0 THEN COUNT(ord_id) ELSE 0 END AS year2019,
CASE WHEN year = 2020.0 THEN COUNT(ord_id) ELSE 0 END AS year2020
FROM helper
GROUP BY year, an_id
ORDER BY an_id
The current output of my query:
an_id
year2019
year2020
1
32
0
1
0
41
2
29
0
2
0
28
3
42
0
The issue in your query may be inside your GROUP BY clause, because you're grouping on the year too. Instead consider the following approach, where you invert the position of the COUNT aggregate function and the CASE statement:
SELECT a.an_id,
COUNT(CASE WHEN EXTRACT(year FROM o.ord_datetime) = 2019 THEN o.ord_id END) AS year2019,
COUNT(CASE WHEN EXTRACT(year FROM o.ord_datetime) = 2020 THEN o.ord_id END) AS year2020
FROM analysis a
INNER JOIN orders o
ON o.ord_an = a.an_id
GROUP BY a.an_id
Note: the ELSE part of your CASE statement is not necessary, as long as values will be defaulted to NULL (and not counted in by the COUNT).

calculate multiple total sales of months

My desired output is shown below.I have tried to achieve it like this
*new_card_total = total sales
SELECT DATENAME(MONTH, cl.nc_timestamp) as MonthName,
COUNT(*) as new_card_qty,
ISNULL(sum(cl.nc_deposit),0) as new_card_total
FROM dbfastshosted.dbo.fh_mf_new_card_logs cl
INNER JOIN dbfastshosted.dbo.fh_sales_map m
on cl.nc_log_id = m.nc_log_id
INNER JOIN dbfastshosted.dbo.fh_sales_logs sl
on m.sales_id = sl.sales_id
INNER JOIN dbfastsconfigdataref.dbo.cdf_terminal_user_account h
on cl.created_user_id = h.terminal_user_id
INNER JOIN dbfastsconfigdataref.dbo.cdf_terminal t
on h.terminal_id = t.terminal_id
INNER JOIN dbfastsconfigdataref.dbo.cdf_cuid c
on cl.cu_id = c.cu_id
INNER JOIN dbfastsconfigdataref.dbo.cdf_card_role cr
on cr.id = c.card_role_id
INNER JOIN dbfastshosted.dbo.fh_mf_top_up_logs tl
on tl.tu_log_id = m.tu_log_id
WHERE YEAR(cl.nc_timestamp)= 2017
and cl.currency_id = 1
and (cr_log_id is null or cr_log_id = 0)
and top_up_status = 1
GROUP BY DATENAME(MONTH,cl.nc_timestamp), DATEPART(MONTH, cl.nc_timestamp)
union all
SELECT DATENAME(MONTH, cl.nc_timestamp) as MonthName,
COUNT(*) as new_card_qty,
ISNULL(sum(cl.nc_deposit),0) as new_card_total
FROM dbfastshosted.dbo.fh_mf_new_card_logs cl
INNER JOIN dbfastshosted.dbo.fh_sales_map m
on cl.nc_log_id = m.nc_log_id
INNER JOIN dbfastshosted.dbo.fh_sales_logs sl
on m.sales_id = sl.sales_id
INNER JOIN dbfastsconfigdataref.dbo.cdf_terminal_user_account h
on cl.created_user_id = h.terminal_user_id
INNER JOIN dbfastsconfigdataref.dbo.cdf_terminal t
on h.terminal_id = t.terminal_id
INNER JOIN dbfastsconfigdataref.dbo.cdf_cuid c
on cl.cu_id = c.cu_id
INNER JOIN dbfastsconfigdataref.dbo.cdf_card_role cr
on cr.id = c.card_role_id
INNER JOIN dbfastshosted.dbo.fh_trans tr
on tr.trans_id = m.trans_id
WHERE YEAR(cl.nc_timestamp)= 2017
and cl.currency_id = 1
and (cr_log_id is null or cr_log_id = 0)
GROUP BY DATENAME(MONTH,cl.nc_timestamp), DATEPART(MONTH, cl.nc_timestamp)
with output :
monthname new_card_qty new_card_value
-----------------------------------------------------------
jan 100 1000
feb 200 2000
march 300 3000
march 400 5000
april 500 6000
april 500 8000
and i would like to have output like this:
monthname new_card_qty new_card_total
-----------------------------------------------------------
jan 100 1000
feb 200 2000
march 700 8000
april 1000 13000
I have tried many ways but couldnt make it.Can you please take a look on this? I really need help. Thanks!
It can be achieved by Subquery and group by as below:
SELECT MonthName, SUM(new_card_qty), SUM(new_card_value)
FROM
(SELECT DATENAME(MONTH, cl.nc_timestamp) as MonthName, COUNT(*) as new_card_qty, ISNULL(sum(cl.nc_deposit),0) as new_card_total
FROM dbfastshosted.dbo.fh_mf_new_card_logs cl ........
union all
SELECT DATENAME(MONTH, cl.nc_timestamp) as MonthName, COUNT(*) as new_card_qty, ISNULL(sum(cl.nc_deposit),0) as new_card_total
FROM dbfastshosted.dbo.fh_mf_new_card_logs cl ......) AS A
GROUP BY MonthName

Return zero value for all the month in series with count zero

This is my query:
SELECT STAFF.stf_first_name + '' + STAFF.stf_last_name As Name,
DATENAME(month,RES_HAB_DATA.reshabdata_data_date) As Month,
SUM(case when RES_HAB_DATA.reshabdata_duration > 0
then (RES_HAB_DATA.reshabdata_duration/15) else 0 end) As ServiceDeliveryTime,
MONTH(RES_HAB_DATA.reshabdata_data_date) As MonthNumber
FROM RES_HAB_DATA
JOIN RES_HAB ON RES_HAB_DATA.reshab_id = RES_HAB.reshab_id
JOIN STAFF ON RES_HAB_DATA.staff_id = STAFF.stf_id
WHERE RES_HAB.serv_id = 30
AND RES_HAB_DATA.reshabdata_data_date >= '1/1/2015'
GROUP BY STAFF.stf_last_name,
STAFF.stf_first_name,
DATENAME(month,RES_HAB_DATA.reshabdata_data_date),
MONTH(RES_HAB_DATA.reshabdata_data_date)
ORDER BY MonthNumber
Which produces result set as:
Name Month ServiceDeliveryTime MonthNumber
----------------------------------------------------------------------------
mb January 52 1
MikeCasey January 10 1
MikeCasey February 4 2
PrecisionCareSupport February 0 2
MikeCasey March 4 3
PrecisionCareSupport March 0 3
MikeCasey April 8 4
PrecisionCareSupport April 0 4
MikeCasey May 16 5
MikeCasey July 4 7
PrecisionCareSupport July 1 7
PrecisionCareSupport August 0 8
MikeCasey September 10 9
MikeCasey October 12 10
I am generating a chart and would like to generate series for that chart but the series should be formed in a way that each series label must have all the tick values(zero if missing respective month). In Simple words,I want resultset as:
Name Month ServiceDeliveryTime MonthNumber
----------------------------------------------------------------------------
mb January 52 1
mb February 0 2
mb March 0 3
mb April 0 4
- - 0 5
Upto December then series will continue for Client MikeCasey upto December and so on...for all the series Labels.If any of the tick is missing for that client there will be value zero for that month.
How Can I produce this result set ? I want some uniform solution because there can be number of such queries for different charts.
Mr Shaw, try this
;WITH
(SELECT STAFF.stf_first_name + '' + STAFF.stf_last_name As Name,
DATENAME(month,RES_HAB_DATA.reshabdata_data_date) As Month,
SUM(case when RES_HAB_DATA.reshabdata_duration > 0
then (RES_HAB_DATA.reshabdata_duration/15) else 0 end) As ServiceDeliveryTime,
MONTH(RES_HAB_DATA.reshabdata_data_date) As MonthNumber
FROM RES_HAB_DATA
JOIN RES_HAB ON RES_HAB_DATA.reshab_id = RES_HAB.reshab_id
JOIN STAFF ON RES_HAB_DATA.staff_id = STAFF.stf_id
WHERE RES_HAB.serv_id = 30
AND RES_HAB_DATA.reshabdata_data_date >= '1/1/2015'
GROUP BY STAFF.stf_last_name,
STAFF.stf_first_name,
DATENAME(month,RES_HAB_DATA.reshabdata_data_date),
MONTH(RES_HAB_DATA.reshabdata_data_date)
) AS mytable
SELECT
myTableName.Name
,mytableMonth.Month
,ISNULL(mytable.ServiceDeliveryTime,0)
,mutableMonth.MonthNumber
FROM
(SELECT DISTINCT Name from mytable) mytableName
CROSS JOIN (SELECT DISTINCT Month,MonthNumber FROM mytable) mytableMonth
LEFT INNER JOIN mytable ON mytableName.Name = mytable.Name AND mytableMonth.Month = mytable.Month AND mytableMonthNumber = mytable.MonthNumber
ORDER BY mytableName.Name, mytableMonth.MonthNumber
I have taken all distinct months and names from your data and done a cross join.
;WITH mytable(Name,Month,ServiceDeliveryTime,MonthNumber) AS
(
SELECT STAFF.stf_first_name + '' + STAFF.stf_last_name As Name,
DATENAME(month,RES_HAB_DATA.reshabdata_data_date) As Month,
SUM(case when RES_HAB_DATA.reshabdata_duration > 0
then (RES_HAB_DATA.reshabdata_duration/15) else 0 end) As ServiceDeliveryTime,
MONTH(RES_HAB_DATA.reshabdata_data_date) As MonthNumber
FROM RES_HAB_DATA
JOIN RES_HAB ON RES_HAB_DATA.reshab_id = RES_HAB.reshab_id
JOIN STAFF ON RES_HAB_DATA.staff_id = STAFF.stf_id
WHERE RES_HAB.serv_id = 30
AND RES_HAB_DATA.reshabdata_data_date >= '1/1/2015'
GROUP BY STAFF.stf_last_name,
STAFF.stf_first_name,
DATENAME(month,RES_HAB_DATA.reshabdata_data_date),
MONTH(RES_HAB_DATA.reshabdata_data_date)
)
SELECT
myTableName.Name
,mytableMonth.Month_Name
,ISNULL(mytable.ServiceDeliveryTime,0) as ServiceDeliveryTime
,mytableMonth.id
FROM
(SELECT DISTINCT Name from mytable) mytableName
CROSS JOIN (SELECT DISTINCT Month_Name,id FROM MyMonths) mytableMonth
LEFT JOIN mytable ON mytableName.Name = mytable.Name AND mytableMonth.Month_Name = mytable.Month AND mytable.MonthNumber = mytable.MonthNumber
ORDER BY mytableName.Name, mytableMonth.id
MyMonths table is already created table with id as MonthNumber and Month_Name as Month.
Cheers!

Count records that span multiple date range

ACCOUNT Amount DATE
1 50 01-2010
1 100 03-2010
1 100 02-2011
2 100 01-2011
2 50 05-2011
2 50 09-2011
3 100 03-2012
3 100 03-2013
Is there a query structure that will allow me to count distinct accounts that has spanned current and past year? For example, account 1 has amounts in 2011 and 2010 so it should be counted once under 2011. Account 2 only has amounts in 2011 so it doesn't get counted while account 3 has amounts in 2013 and 2012, so it gets counted as 1 under 2013:
2010 2011 2012 2013
0 1 0 1
First, you need to know the years where you have data for an account:
select account, year(date) as yr
from t
group by account, year(date)
Next, you need to see if two years are in sequence. You can do this in 2012 with lag/lead. Instead, we'll just use a self join:
with ay as (
select account, year(date) as yr
from t
group by account, year(date)
)
select ay.account, ay.yr
from ay join
ay ayprev
on ay.account = ayprev.account and
ay.yr = ayprev.yr + 1
Next, if you want to count the number of accounts by year, just put this into an aggregation:
with ay as (
select account, year(date) as yr
from t
group by account, year(date)
)
select yr, count(*) as numaccounts
from (select ay.account, ay.yr
from ay join
ay ayprev
on ay.account = ayprev.account and
ay.yr = ayprev.yr + 1
) ayy
group by yr
Assuming you have a record id (call this ID)
SELECT COUNT(*),Year FROM Table t3
INNER JOIN (
SELECT record_id, Year(t1.Date) as Year FROM Table t1
INNER JOIN Table t2
WHERE Year(t1.Date)-1=Year(t2.Date) AND t1.Account == t2.Account
) x ON x.record_id = t3.record_id
GROUP BY Year
Use Below Query :
SELECT YEAR(T1.Date) AS D, COUNT(*) AS C
FROM YourTable AS T1
INNER JOIN YourTable T2 ON T2.Account = T1.Account AND YEAR(T2)=YEAR(T1)+1
GROUP BY T1.Account, YEAR(T1.Date)

Outer join - oracle

I have 2 tabels
Current Ecpense table
-----------------------
Month-----Type-------spent
Feb 12 Shopping 100
Feb 12 Food 200
Jan 12 Shopping 456
Jan 12 Food 452
Jan 12 Fuel 120
Jan 12 Rent 900
Previous Expense
-----------------------
Type------ spent
Shopping 100
Food 100
Fuel 100
Rent 100
Now i want to join these two tables, the expected result is;
Month-----Type-------spent-----Previous Spent
Feb 12 Shopping 100 100
Feb 12 Food 200 100
Feb 12 Fuel 0 100
Feb 12 Rent 0 100
Jan 12 Shopping 456 100
Jan 12 Food 452 100
Jan 12 Fuel 120 100
Jan 12 Rent 900 100
Is there a way to do this?
Try:
select m.month,
p.type,
coalesce(c.spent,0) spent,
p.spent previous_spent
from (select distinct month from current_expense) m
cross join previous_expense p
left join current_expense c
on m.month = c.month and p.type = c.type
Common Oracle syntax
Select a.*, b.spent 'previous Sent'
from current_expense a, previous_expense b
where a.type = b.type
or the same thing in standard
Select a.*, b.spent 'previous Sent'
from current_expense as a
inner join previous_expense as b on a.type = b.type
I tend to write in SQL92 (more SQLness and less Oracle-ness)
Here is my answer:
select
z.month as month ,
z.type as type ,
nvl(c.spent,0) as spent ,
nvl(p.spent,0) as previous_spent
from
(select
x.month as month ,
y.type as type
from
(select distinct month
from current_expense) x
cross join
(select distinct type
from current_expense) y) z
left outer join current_expense c
on z.month = c.month and
z.type = z.type
left outer join previous_expense p
on z.type = p.type;