I have a following table:-
CREATE TABLE #TmpData (
RType Varchar(10),
Client VARCHAR(10),
TRequest INT,
Due INT,
Month INT
);
INSERT INTO #TmpData VALUES ('P', 'Name1',54,67,1),('P', 'Name5',5,47,1),('A', 'Name6',3,90,1 ),('A', 'Name3',4,43,1 ),('P', 'Name1',0,10,2)
,('P', 'Name2',1,78,2),('P', 'Name4',54,67,3),('P', 'Name3',5,47,3),('A', 'Name6',3,90,3 ),('P', 'Name8',3,233,3 ),('P', 'Name1',23,457,4)
,('P', 'Name7',3,6,4 ),('P', 'Name8',65,548,4 ),('P', 'Name2',3,56,5 ),('P', 'Name3',65,87,6 ),('P', 'Name4',3,56,7 ),('P', 'Name9',65,87,7 )
SELECT * FROM #TmpData
I want to Convert its data row wise to column and show it monthwise. I am attaching the screenshot of my resultset which I need.
Result Set
Here for example I have taken 7 months but it can go upto 12 months.
Please Help.
Thanks
You can try to use CASE WHEN with SUM function.
CASE WHEN compare the month then SUM that TRequest or Due group by by
RType,Client
SELECT RType,
Client,
SUM(CASE WHEN Month = 1 THEN TRequest ELSE 0 END) 'Jan Totle',
SUM(CASE WHEN Month = 1 THEN Due ELSE 0 END) '#of Request',
SUM(CASE WHEN Month = 2 THEN TRequest ELSE 0 END) 'Feb Totle',
SUM(CASE WHEN Month = 2 THEN Due ELSE 0 END) '#of Request',
SUM(CASE WHEN Month = 3 THEN TRequest ELSE 0 END) 'March Totle',
SUM(CASE WHEN Month = 3 THEN Due ELSE 0 END) '#of Request',
SUM(CASE WHEN Month = 4 THEN TRequest ELSE 0 END) 'April Totle',
SUM(CASE WHEN Month = 4 THEN Due ELSE 0 END) '#of Request',
SUM(CASE WHEN Month = 5 THEN TRequest ELSE 0 END) 'May Totle',
SUM(CASE WHEN Month = 5 THEN Due ELSE 0 END) '#of Request',
SUM(CASE WHEN Month = 6 THEN TRequest ELSE 0 END) 'June Totle',
SUM(CASE WHEN Month = 6 THEN Due ELSE 0 END) '#of Request',
SUM(CASE WHEN Month = 7 THEN TRequest ELSE 0 END) 'July Totle',
SUM(CASE WHEN Month = 7 THEN Due ELSE 0 END) '#of Request',
SUM(CASE WHEN Month = 8 THEN TRequest ELSE 0 END) 'Aug Totle',
SUM(CASE WHEN Month = 8 THEN Due ELSE 0 END) '#of Request',
SUM(CASE WHEN Month = 9 THEN TRequest ELSE 0 END) 'Sep Totle',
SUM(CASE WHEN Month = 9 THEN Due ELSE 0 END) '#of Request',
....
FROM TmpData
GROUP BY RType,
Client
sqlfiddle
EDIT
if you want to make month column dynamic, you can use dynamic pivot.
DECLARE #cols AS NVARCHAR(MAX),
#Pivotcols AS NVARCHAR(MAX),
#query AS NVARCHAR(MAX);
SET #Pivotcols = STUFF((SELECT DISTINCT
',SUM(CASE WHEN Month = ' + CAST(Month AS VARCHAR(5)) + ' THEN TRequest ELSE 0 END) as ''' + FORMAT(DATEADD(mm,Month,'1900-01-01'),'MMM') + ' Totle ''',
',SUM(CASE WHEN Month = ' + CAST(Month AS VARCHAR(5)) + ' THEN Due ELSE 0 END) as ''' + FORMAT(DATEADD(mm,Month,'1900-01-01'),'MMM') + ' Totle '''
FROM TmpData
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'');
set #query = 'SELECT RType,Client, '+ #Pivotcols + ' FROM TmpData GROUP BY RType,Client'
execute(#query)
SQLFIDDLE dynamic pivot
Related
How to make persen_sukses or persen_gagal only get 2 decimal ?
I try use round but get some error.
May be anyone can help me.
Here is my sql
SELECT * ,(CASE WHEN all_trx.trx_gagal = 0 THEN 100 WHEN all_trx.trx_sukses = 0 THEN 0 ELSE (CAST(all_trx.trx_sukses AS float)/CAST(all_trx.trx_total AS float)*100) END) AS persen_sukses,
(CASE WHEN all_trx.trx_sukses = 0 THEN 100 WHEN all_trx.trx_gagal = 0 THEN 0 ELSE (CAST(all_trx.trx_gagal AS float)/CAST(all_trx.trx_total AS float)*100) END) AS persen_gagal
FROM
(SELECT kode_produk AS Produk,
COUNT(CASE WHEN status = '20' THEN 1 END) AS trx_sukses,
COUNT(CASE WHEN status > '20' THEN 1 END) AS trx_gagal,
COUNT(CASE WHEN status >= '20' THEN 1 END) AS trx_total
FROM transaksi WHERE CAST(tgl_entri AS DATE) = CAST(GETDATE() AS DATE) GROUP BY kode_produk)
AS all_trx
SELECT *
,(CASE
WHEN all_trx.trx_gagal = 0 THEN 100
WHEN all_trx.trx_sukses = 0 THEN 0
--ELSE (CAST(all_trx.trx_sukses AS float)/CAST(all_trx.trx_total AS float)*100)
ELSE CAST(all_trx.trx_sukses*100.0 / all_trx.trx_total as numeric(18,2) )
END) AS persen_sukses
,(CASE
WHEN all_trx.trx_sukses = 0 THEN 100
WHEN all_trx.trx_gagal = 0 THEN 0
--ELSE (CAST(all_trx.trx_gagal AS float)/CAST(all_trx.trx_total AS float)*100)
ELSE CAST(all_trx.trx_gagal*100.0 / all_trx.trx_total as numeric(18,2) )
END) AS persen_gagal
FROM
(
SELECT
kode_produk AS Produk,
COUNT(CASE WHEN status = '20' THEN 1 END) AS trx_sukses,
COUNT(CASE WHEN status > '20' THEN 1 END) AS trx_gagal,
COUNT(CASE WHEN status >= '20' THEN 1 END) AS trx_total
FROM transaksi
WHERE CAST(tgl_entri AS DATE) = CAST(GETDATE() AS DATE)
GROUP BY kode_produk
) AS all_trx
Dear i am able to fix the issue in your query, Pleae note the only change i did is ELSE part of Case Statement. I executed the query and now returning the value upto two decimal places only. If you have to increase or decrease the digits after the decimal sign update the value of numeric(18,2) here 2 is number of digit after decimal.
I have a table in SQL Server with these columns:
id int pk
date datetime
value numeric
This is my select query
SELECT
O.Date AS DATE,
O.Value AS VALUE
FROM
Orders O
WHERE
YEAR(Date) = #Year
and this is my data
I want this output:
JAN FEB MAR APR MAY JUNE JULY AUG SEPT OCT NOV DEC
-----------------------------------------------------------------------
600 1200 600 600 0 0 0 0 0 0 0 0
Doesn't need a subquery. Something like this should work
select sum(case when dt.mo=1 then o.[Value] else 0 end) 'Jan',
sum(case when dt.mo=2 then o.[Value] else 0 end) 'Feb',
sum(case when dt.mo=3 then o.[Value] else 0 end) 'Mar',
sum(case when dt.mo=4 then o.[Value] else 0 end) 'Apr',
sum(case when dt.mo=5 then o.[Value] else 0 end) 'May',
sum(case when dt.mo=6 then o.[Value] else 0 end) 'Jun',
sum(case when dt.mo=7 then o.[Value] else 0 end) 'Jul',
sum(case when dt.mo=8 then o.[Value] else 0 end) 'Aug',
sum(case when dt.mo=9 then o.[Value] else 0 end) 'Sep',
sum(case when dt.mo=10 then o.[Value] else 0 end) 'Oct',
sum(case when dt.mo=11 then o.[Value] else 0 end) 'Nov',
sum(case when dt.mo=12 then o.[Value] else 0 end) 'Dec'
from Orders o
cross apply (select month(o.[Date]) mo) dt
where year(o.[DAte])=#Year;
You may try this-
Select
max(case when [Month] = 'Jan' then Value end) As "JAN"
,max(case when [Month] = 'Feb' then Value end) As "FEB"
,.....
,max(case when [Month] = 'Dec' then Value end) As "DEC"
from
(SELECT
Format(O.Date, 'MMM') as [Month],
Max(O.Value) AS VALUE
FROM Orders O
WHERE YEAR(Date) = #Year
Group by
Format(O.Date, 'MMM'))T;
I have following data in my table:
uniqueId d_date amount
1 2018-02-01 100.25
2 2019-03-01 456.5
3 2018-02-01 455
4 2019-05-01 200.48
5 2018-06-01 100
6 2019-07-01 200
7 2018-12-01 6950
8 2019-02-01 60
9 2020-01-20 100
Now when I enter start date = '2018-03-12' then my fiscal year must start with march 2018 to feb 2019 and so on.
If i enter start date = '2019-05-12' then my fiscal year must start with May 2019 to April 2020
I have tried below query but it is not working properly and also it calculate past year which is 2017 I do not want any data from past year from my entered custom date. So if entered start date = '2018-03-12' then is must start calculation for 2018 and above years only. No past year.
Declare #startdate as date
Declare #monthDate as int
Declare #ownmonth as int
set #startdate = '2018-03-12'
set #monthDate = month(#startdate)
set #ownmonth = 1
select
year(dateadd(month, -#monthDate, d_date)) year,
sum(case when month(d_date) = case when #monthDate+1 > 12 then #ownmonth else #monthDate+1 End then amount end) ,
sum(case when month(d_date) = case when #monthDate+2 > 12 then #ownmonth+1 else #monthDate+2 End then amount end) ,
sum(case when month(d_date) = case when #monthDate+3 > 12 then #ownmonth+2 else #monthDate+3 End then amount end) ,
sum(case when month(d_date) = case when #monthDate+4 > 12 then #ownmonth+3 else #monthDate+4 End then amount end) ,
sum(case when month(d_date) = case when #monthDate+5 > 12 then #ownmonth+4 else #monthDate+5 End then amount end) ,
sum(case when month(d_date) = case when #monthDate+6 > 12 then #ownmonth+5 else #monthDate+6 End then amount end) ,
sum(case when month(d_date) = case when #monthDate+7 > 12 then #ownmonth+6 else #monthDate+7 End then amount end) ,
sum(case when month(d_date) = case when #monthDate+8 > 12 then #ownmonth+7 else #monthDate+8 End then amount end) ,
sum(case when month(d_date) = case when #monthDate+9 > 12 then #ownmonth+8 else #monthDate+9 End then amount end) ,
sum(case when month(d_date) = case when #monthDate+10 > 12 then #ownmonth+9 else #monthDate+10 End then amount end) ,
sum(case when month(d_date) = case when #monthDate+11 > 12 then #ownmonth+10 else #monthDate+11 End then amount end) ,
sum(case when month(d_date) = case when #monthDate+12 > 12 then #ownmonth+11 else #monthDate+12 End then amount end) ,
sum(amount) total
from mytable
group by year(dateadd(month, -#monthDate, amount))
order by year
But above query does not show proper year & month wise data
Now I want output with fiscal year calculation:
Year Mar Apr May Jun July Aug Sept Oct Nov Dec Jan Feb Total
2018 - - - 100 - - - - - 6950 - 60 7110
2019 456.5 - 200.48 - 200 - - - - - 100 - 956.98
I can not use PIVOT as it is not supported in my compact SQL Server version.
How can I do this?
Your rule for a fiscal year is the year's March until the following year's February:
date | fiscal year
... | ...
2018-02-28 | 2017
2018-03-01 | 2018
... | 2018
2019-02-28 | 2018
2019-03-01 | 2019
... | ...
That means when we subtract two months from a date, we get a date the year of which is the fiscal year:
date | date - 2 months | fiscal year
... | ... | ...
2018-02-28 | 2017-12-28 | 2017
2018-03-01 | 2018-01-01 | 2018
... | ... | 2018
2019-02-28 | 2018-12-28 | 2018
2019-03-01 | 2019-01-01 | 2019
... | ... | ...
select
year(dateadd(month, -2, d_date)) as fiscal_year,
sum(case when month(d_date) = 3 then amount else 0 end) as mar,
sum(case when month(d_date) = 4 then amount else 0 end) as apr,
sum(case when month(d_date) = 5 then amount else 0 end) as may,
sum(case when month(d_date) = 6 then amount else 0 end) as jun,
sum(case when month(d_date) = 7 then amount else 0 end) as jul,
sum(case when month(d_date) = 8 then amount else 0 end) as aug,
sum(case when month(d_date) = 9 then amount else 0 end) as sep,
sum(case when month(d_date) = 10 then amount else 0 end) as oct,
sum(case when month(d_date) = 11 then amount else 0 end) as nov,
sum(case when month(d_date) = 12 then amount else 0 end) as dec,
sum(case when month(d_date) = 1 then amount else 0 end) as jan,
sum(case when month(d_date) = 2 then amount else 0 end) as feb,
sum(amount) as total
from mytable
group by year(dateadd(month, -2, d_date))
order by year(dateadd(month, -2, d_date));
If you want to limit this to the fiscal year a given date resides in, add:
where year(dateadd(month, -2, d_date)) = year(dateadd(month, -2, #given_date))
And well, if you want to limit this to the fiscal years beginning with that year, that would of course be:
where year(dateadd(month, -2, d_date)) >= year(dateadd(month, -2, #given_date))
UPDATE: You want a fiscal year to start with the first day of the month of a given date. I.e. If the given date is 1990-04-23, then a fiscal year starts with April. This changes above query only slightly, because rather than subtracting 2 months (for March), we must generalize this to subtracting one month less than the given month.
I am using a modulo operation when comparing months in order not to end up with months 13, 14, etc.
select
year(dateadd(month, - month(#startdate) + 1, d_date)) as fiscal_year,
sum(case when month(d_date) = (month(#startdate) + 0) % 12 then amount else 0 end) as first,
sum(case when month(d_date) = (month(#startdate) + 1) % 12 then amount else 0 end) as second,
sum(case when month(d_date) = (month(#startdate) + 2) % 12 then amount else 0 end) as third,
sum(case when month(d_date) = (month(#startdate) + 3) % 12 then amount else 0 end) as fourth,
sum(case when month(d_date) = (month(#startdate) + 4) % 12 then amount else 0 end) as fith,
sum(case when month(d_date) = (month(#startdate) + 5) % 12 then amount else 0 end) as sixth,
sum(case when month(d_date) = (month(#startdate) + 6) % 12 then amount else 0 end) as seventh,
sum(case when month(d_date) = (month(#startdate) + 7) % 12 then amount else 0 end) as eighth,
sum(case when month(d_date) = (month(#startdate) + 8) % 12 then amount else 0 end) as nineth,
sum(case when month(d_date) = (month(#startdate) + 9) % 12 then amount else 0 end) as tenth,
sum(case when month(d_date) = (month(#startdate) + 10) % 12 then amount else 0 end) as eleventh,
sum(case when month(d_date) = (month(#startdate) + 11) % 12 then amount else 0 end) as twelfth,
sum(amount) as total
from mytable
group by year(dateadd(month, - month(#startdate) + 1, d_date))
order by year(dateadd(month, - month(#startdate) + 1, d_date));
And again, if we want our results start from the fiscal year of the given date, we'd add:
where year(dateadd(month, - month(#startdate) + 1, d_date)) >= year(#startdate)
I have the following SQL query which Display 12 months of summary report from the given date. Output shows starting from January to December for whatever the given date. I want the outcome to start from the given date's month.
If the given date is '2016-05-01' I want the output to be like this
May 16 |June 16| July 16| ........... | Jan 17 | Feb 17 | March 17 | April 17 |
How can I achieve this?
Can some one suggest?
SELECT Name,SUM(Amount) AS PremiumTot,TotType,
sum(case when month(Dates) = 1 then Tot else 0 end) Jan,
sum(case when month(Dates) = 2 then Tot else 0 end) Feb,
sum(case when month(Dates) = 3 then Tot else 0 end) March,
sum(case when month(Dates) = 4 then Tot else 0 end) April,
sum(case when month(Dates) = 5 then Tot else 0 end) May,
sum(case when month(Dates) = 6 then Tot else 0 end) June,
sum(case when month(Dates) = 7 then Tot else 0 end) July,
sum(case when month(Dates) = 8 then Tot else 0 end) Aug,
sum(case when month(Dates) = 9 then Tot else 0 end) Sep,
sum(case when month(Dates) = 10 then Tot else 0 end) Oct,
sum(case when month(Dates) = 11 then Tot else 0 end) Nov,
sum(case when month(Dates) = 12 then Tot else 0 end) Dece
FROM
(
SELECT InvoiceMasterID,Dates ,Name,CompanyCommission AS Tot ,0 AS Expences,Amount,1 as TotType
FROM CommissionView
UNION ALL
SELECT InvoiceMasterID,Dates,Name, 0 AS Tot ,-AgentCommission AS Expences,Amount,2 as TotType
FROM CommissionViewCredit
) a
WHERE Dates between #fromDates AND Datesadd(yy,1,#fromDates)
GROUP BY Name,TotType
Seems, you want to pivot data. So, use PIVOT table!
If you want to create dynamic columns from given date, use CTE (Common Table Expressions)!
--declare variables for given date range
DECLARE #startDate DATE = '2016-05-01'
DECLARE #endDate DATE = DATEADD(mm,11,#startDate)
--declare variable to store months as: [month1], [month2], [etc.]
DECLARE #Months VARCHAR(1000) = ''
--use cte to create range of dates
;WITH MyDates AS
(
SELECT #startDate AS MyDate
UNION ALL
SELECT DATEADD(mm,1,MyDate) AS MyDate
FROM MyDates
WHERE MyDate<#endDate
)
SELECT #Months = STUFF((SELECT '],[' + CONVERT(VARCHAR(7), MyDate, 121)
FROM MyDates
FOR XML PATH('')), 1, 2, '') + ']'
--PRINT #Months:
-- prints: [2016-05],[2016-06], ... ,[2017-04]
DECLARE #qry NVARCHAR(MAX) = ''
SET #qry = N'SELECT ' + #Months +
' FROM ( ' +
' SELECT CONVERT(VARCHAR(7), Dates, 121) AS MyDate, CompanyCommission AS Tot ' +
'FROM CommissionView ' +
') AS DT ' +
'PIVOT (SUM(Tot) FOR MyDate IN(' + #Months + ')) AS PT'
EXEC (#qry)
For further information, please see:
Dynamic PIVOT
Pivots with dynamic columns
CAST and CONVERT
Good luck!
The below SQL is returning 'Cannot perform an aggregate function on an expression containing an aggregate or a subquery.', can anyone help?
SELECT
sum(case when Frequency = 'Monthly' then ISNULL(SUM(Amount),0.0) else 0 end) +
sum(case when Frequency = '4 Weekly' then ISNULL(SUM(Amount),0.0) / 2 else 0 end) +
sum(case when Frequency = 'Fortnightly' then ISNULL(SUM(Amount),0.0) / 3 else 0 end) +
sum(case when Frequency = 'Weekly' then ISNULL(SUM(Amount),0.0) / 5 else 0 end)
FROM TableWHERE Id = 1
If you want conditional aggregation, you only want one sum():
SELECT sum(case when Frequency = 'Monthly' then Amount else 0 end) +
sum(case when Frequency = '4 Weekly' then Amount / 2 else 0 end) +
sum(case when Frequency = 'Fortnightly' then Amount / 3 else 0 end) +
sum(case when Frequency = 'Weekly' then Amount,0.0) / 5 else 0 end)
FROM Table
WHERE Id = 1;
I think you want to do something like
sum(case when Frequency = 'Monthly' then ISNULL(Amount,0.0) else 0 end)
SELECT
sum(case when Frequency = 'Monthly' then ISNULL(Amount,0.0) else 0 end) +
sum(case when Frequency = '4 Weekly' then ISNULL(Amount,0.0) / 2 else 0 end) +
sum(case when Frequency = 'Fortnightly' then ISNULL(Amount,0.0) / 3 else 0 end) +
sum(case when Frequency = 'Weekly' then ISNULL(Amount,0.0) / 5 else 0 end)
FROM Table
WHERE Id = 1