SQL Query with RollUp - sql

this is my query
select X.Memo, X.Month, Format(sum(X.[Total Sales S$]),'#,0.00') as [Rental Sales $]
, Format(sum(X.[Net GP S$]),'#,0.00') as [Rental GP $]
from Data X
group by X.Month,X.Memo
This is my result:
Expected result should be like this:
is it possible to do it in SQL? Can help me in the query?

One way would be to use case statement and get the result you mentioned.
you can add requird columns in the similar way and get the output.
SELECT MONTH,
sum(CASE WHEN Memo = 'Rental' THEN sales$ END) Rental_$,
sum(CASE WHEN Memo = 'Rental' THEN GP$ END) Rental_GP,
sum(CASE WHEN Memo = 'Sales' THEN sales$ END) Sales_$,
sum(CASE WHEN Memo = 'Sales' THEN GP$ END) Sales_GP
FROM tablea
GROUP BY MONTH
SELECT MONTH,
Format(sum(CASE WHEN Memo = 'Rental' THEN (X.[Total Sales S$]) END), '#,0.00') Rental_$,
Format(sum(CASE WHEN Memo = 'Rental' THEN (X.[Net GP S$]) END), '#,0.00') Rental_GP,
Format(sum(CASE WHEN Memo = 'Sales' THEN (X.[Total Sales S$]) END), '#,0.00') Sales_$,
Format(sum(CASE WHEN Memo = 'Sales' THEN (X.[Net GP S$]) END), '#,0.00') Sales_GP,
Format((sum(CASE WHEN Memo = 'Rental' THEN (X.[Total Sales S$]) END) + sum(CASE WHEN Memo = 'Sales' THEN (X.[Total Sales S$]) END)), '#,0.00') [Total Sales S$],
Format((sum(CASE WHEN Memo = 'Rental' THEN (X.[Net GP S$]) END) + sum(CASE WHEN Memo = 'Sales' THEN (X.[Net GP S$]) END)), '#,0.00') [Total GP S$]
FROM DATA X
GROUP BY MONTH WITH ROLLUP
ORDER BY SUM(X.MN) ASC

Related

sql query group/sum and turn rows into combined columns

I am trying to turn the source table into a summation table on a few factors.
Can someone point me in the right direction?
date and id will be the rows.
exprev and type will be combined to create a column
amount will be summed
I tried pivot table but I couldn't figure out a way to combine columns, ie. 'rent expense'
UPDATE
conditional aggregation works but I forgot to add one more scenario.
I need to add a column for the currency of the item, then change the result to have a local amount, and a converted amount to USD.
new screenshot below
update...
I added to the select and the group by.
,case when a.exprev = 'Expense' and a.type= 'rent' then a.Currency else 'nothing' end as [rent Currency]
now I am getting an extra row of inaccurate data in my result.
it has created 2 lines of data for each id when it should be only 1 line of data
for argument sake... lets say I will know the exact columns
In that case you would use conditional aggregation, like this:
SELECT [date], id
,SUM(case when exprev = 'expense' and type='rent' then amount else 0 end) [rent expense]
,SUM(case when exprev = 'revenue' and type='rent' then amount else 0 end) [rent revenue]
,SUM(case when exprev = 'expense' and type='tax' then amount else 0 end) [tax expense]
,SUM(case when exprev = 'revenue' and type='tax' then amount else 0 end) [tax revenue]
,SUM(case when exprev = 'expense' and type='insurance' then amount else 0 end) [insurance expense]
,SUM(case when exprev = 'revenue' and type='insurance' then amount else 0 end) [insurance revenue]
FROM [table]
GROUP BY [date], id

Convert column to row and sum query in different table

i trying to figure to convert sumtchtraffic and totalpayloadGb column in sitetable and sum data totalpayloadGb & sumtchtraffic where BTS_TYPE 2g, 3g and 4g in techtable
i tried like this, but i cant show totalpayloadgb & sumtchtraffic in column [SUM OF] ([sum of] is not in any table in my database)
select * from (select
sum(case when techtable.BTS_TYPE='2G' then sitetable.TotalPayloadGb else 0 end) as [Total 2G],
sum(case when techtable.BTS_TYPE='3G' then sitetable.TotalPayloadGb else 0 end) as [Total 3G],
sum (case when techtable.BTS_TYPE='4G'then sitetable.TotalPayloadGb else 0 end) as [Total 4G]
from sitetable
inner join techtable on sitetable.sitename = techtable.sitename) as t
I want show my data like this:
One way would be to total your results separately, using the same conditional aggregation you have started, and then UNION the results together. Since your first column doesn't exist anywhere else, you'll have to hard-code it into the queries.
SELECT
'TotalPayloadGb' AS SumOf,
sum(CASE WHEN t.BTS_TYPE = '2G' THEN s.TotalPayloadGb ELSE 0 END) AS [Total 2G],
sum(CASE WHEN t.BTS_TYPE = '3G' THEN s.TotalPayloadGb ELSE 0 END) AS [Total 3G],
sum(CASE WHEN t.BTS_TYPE = '4G' THEN s.TotalPayloadGb ELSE 0 END) AS [Total 4G]
FROM sitetable AS s
INNER JOIN techtable AS t
ON s.sitename = t.sitename
UNION
SELECT
'SumTCHTraffic' AS SumOf,
sum(CASE WHEN t.BTS_TYPE = '2G' THEN s.SumTCHTraffic ELSE 0 END) AS [Total 2G],
sum(CASE WHEN t.BTS_TYPE = '3G' THEN s.SumTCHTraffic ELSE 0 END) AS [Total 3G],
sum(CASE WHEN t.BTS_TYPE = '4G' THEN s.SumTCHTraffic ELSE 0 END) AS [Total 4G]
FROM sitetable AS s
INNER JOIN techtable AS t
ON s.sitename = t.sitename;
Depending on what you are trying to do, you could do like this:
SELECT tt.BTS_Type, sum(st.TotalPayloadDb)
FROM SiteTable st
INNER JOIN TechTable as tt on st.sitename = tt.sitename
This will give one row per BTS_Type value.

Running Balance in sql

I have tried the following query to get account transactions
select TranRef, TrnDate, AcNumber, AcName, DrCr, amount from Accounts
join Voucher
on Accounts.acid = voucher.accountno
join Transactions
on Transactions.TrnRef = Voucher.TranRef
where acnumber = 1010
but I want the result in following format
Use window function :
select TranRef, TrnDate, AcNumber, AcName, DrCr, amount,
(case when DrCr= 'Dr' then amount else 0 end) as Debit,
(case when DrCr= 'Cr' then amount else 0 end) as Credit,
sum(case when DrCr = 'Cr' then amount else -amount end) over (partition by v.accountno order by TranRef) as Balance
from Accounts a
inner join Voucher v on a.acid = v.accountno
inner join Transactions t on t.TrnRef = v.TranRef
where v.accountno = 1010;

SQL Server Drop NULLS in pivot

I have a base table. Base table
I want to put in linear form that is why I use pivot to have it. And my query goes like:
select
[report_date] AS 'Date'
,[30],[percent_30] AS '%30'
,[45],[percent_45] AS '%45'
,[60],[percent_60] AS '%60'
,[75],[percent_75] AS '%75'
,[90],[percent_90] AS '%90'
,[105],[percent_105] AS '%105'
,[120],[percent_120] AS '%120'
,[TOTAL] AS 'Total Sales'
--,[total_percentage] AS 'Total Percent'
from database.dbo.delivery_report_logs
PIVOT(SUM(Sale_Count)
FOR description IN ([30],[45],[60],[75],[90],[105],[120],[TOTAL])) AS pvz
But the result looks like this.
I want to remove all nulls. Any help? Thank you.
Here is my expected output:
Sample Output
select
[report_date] AS 'Date'
,[30],[percent_30] AS '%30'
,[45],[percent_45] AS '%45'
,[60],[percent_60] AS '%60'
,[75],[percent_75] AS '%75'
,[90],[percent_90] AS '%90'
,[105],[percent_105] AS '%105'
,[120],[percent_120] AS '%120'
,[TOTAL] AS 'Total Sales'
--,[total_percentage] AS 'Total Percent'
from database.dbo.delivery_report_logs
PIVOT ISNULL(SUM(Sale_Count),0)
FOR description IN ([30],[45],[60],[75],[90],[105],[120],[TOTAL])) AS pvz
This is solved. I use temporary table to pivot the data and insert it to a log table and it gives me the result I need.
you can also use a query like this that uses and aggregate with a case expression
SELECT report_date,
SUM(CASE WHEN Description = '30' THEN sale_count END) AS [30],
SUM(CASE WHEN Description = '30' THEN percent_30 END) AS [percent_30],
SUM(CASE WHEN Description = '45' THEN sale_count END) AS [30],
SUM(CASE WHEN Description = '45' THEN percent_45 END) AS [percent_45],
SUM(CASE WHEN Description = '60' THEN sale_count END) AS [30],
SUM(CASE WHEN Description = '60' THEN percent_60 END) AS [percent_60],
SUM(CASE WHEN Description = '75' THEN sale_count END) AS [30],
SUM(CASE WHEN Description = '75' THEN percent_70 END) AS [percent_70],
SUM(CASE WHEN Description = '90' THEN sale_count END) AS [30],
SUM(CASE WHEN Description = '90' THEN percent_90 END) AS [percent_90],
SUM(CASE WHEN Description = '105' THEN sale_count END) AS [30],
SUM(CASE WHEN Description = '105' THEN percent_105 END) AS [percent_105],
SUM(CASE WHEN Description = '120' THEN sale_count END) AS [30],
SUM(CASE WHEN Description = '120' THEN percent_120 END) AS [percent_120],
SUM(CASE WHEN Description = 'TOTAL' THEN sale_count END) AS [30],
SUM(CASE WHEN Description = 'TOTAL' THEN total_percentage END) AS [total_percentage]
FROM database.dbo.delivery_report_logs
GROUP BY report_date
you can also use MAX or MIN in place of SUM if there is only one value per report_date/percentate

Combining result rows

I'm trying to get the result below to be displayed group by Article.
Would this be possible?
So that in the end I will only have 2 lines.
The data from TYPE I & W will be combined.
The image to my query as below
image http://tikistudio.sg/F002.jpg
The query as below
SELECT INTSALEID as RECEIPT,
STRSALETYPE as TYPE,
STRORDERCODE as ARTICLE,
max(case when strsaletype='I' then DBLQTYSOLD else null end) as [QTY SOLD],
max(case when strsaletype='I' then CURSELLPRICE1 else null end) as [UNIT PRICE],
max(case when strsaletype='W' then CUROFFERDISCOUNT else null end) as [DISCOUNT],
max(case when strsaletype='I' then CURFOREIGNAMT else null end) as [GROSS AMOUNT]
FROM DAILYSALES
WHERE STRTRADECODE='MM01'
and DTMTRADEDATE='01-01-2015' and INTSALEID='31086'
and STRSALETYPE in ('I','W')
GROUP BY STRORDERCODE,STRSALETYPE,INTSALEID,DBLQTYSOLD,CURSELLPRICE1,
CURFOREIGNAMT,CURDISCOUNT,CUROFFERDISCOUNT
You have to remove some columns in Group By
SELECT
INTSALEID as RECEIPT,
STRORDERCODE as ARTICLE,
MAX(CASE WHEN strsaletype='I' THEN DBLQTYSOLD ELSE NULL END) as [QTY SOLD],
MAX(CASE WHEN strsaletype='I' THEN CURSELLPRICE1 ELSE NULL END) as [UNIT PRICE],
MAX(CASE WHEN strsaletype='W' THEN CUROFFERDISCOUNT ELSE NULL END) as [DISCOUNT],
MAX(CASE WHEN strsaletype='I' THEN CURFOREIGNAMT ELSE NULL END) as [GROSS AMOUNT]
FROM DAILYSALES
WHERE
STRTRADECODE='MM01'
and DTMTRADEDATE='01-01-2015' and INTSALEID='31086'
and STRSALETYPE in ('I','W')
GROUP BY STRORDERCODE,INTSALEID
If you aggregate your data one more time grouping by Receipt, and Article, taking the max value for QTY Sold, Unit Price, Gross Amount and Min Value for Discount.
Select RECEIPT, ARTICLE, MAX([QTY SOLD]) as [QTY SOLD], MAX([UNIT PRICE]) as [UNIT PRICE], MIN([DISCOUNT]) as [DISCOUNT], MAX([GROSS AMOUNT]) as [GROSS AMOUNT]
From
(select INTSALEID as RECEIPT,
STRSALETYPE as TYPE,
STRORDERCODE as ARTICLE,
max(case when strsaletype='I' then DBLQTYSOLD else null end) as [QTY SOLD],
max(case when strsaletype='I' then CURSELLPRICE1 else null end) as [UNIT PRICE],
max(case when strsaletype='W' then CUROFFERDISCOUNT else null end) as [DISCOUNT],
max(case when strsaletype='I' then CURFOREIGNAMT else null end) as [GROSS AMOUNT]
from DAILYSALES
where STRTRADECODE='MM01'
and DTMTRADEDATE='01-01-2015' and INTSALEID='31086'
and STRSALETYPE in ('I','W')
group by STRORDERCODE,STRSALETYPE,INTSALEID,DBLQTYSOLD,CURSELLPRICE1,
CURFOREIGNAMT,CURDISCOUNT,CUROFFERDISCOUNT) x
Group by RECEIPT, ARTICLE