sql query group/sum and turn rows into combined columns - sql

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

Related

tsql - pivot sum of employee

I have here a sample table data which i want to use pivot and make my DedCode data into column.
the AMount example is already computed to its total but how can i count the number of employee if it is same with branch,deptcode and emptype.
my sample table data.
expected output:
You can use conditional aggregation:
select branch, deptcode, emptype, sum(empcount) as empcount,
sum(case when dedcode = 'PHIC' then amount else 0 end) as phic,
sum(case when dedcode = 'SLOAN' then amount else 0 end) as sloan,
sum(case when dedcode = 'VLOAN' then amount else 0 end) as vloan
from t
group by branch, deptcode, emptype;

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.

SQL select grouping and subtract

i have table named source table with data like this :
And i want to do query that subtract row with status plus and minus to be like this group by product name :
How to do that in SQL query? thanks!
Group by the product and then use a conditional SUM()
select product,
sum(case when status = 'plus' then total else 0 end) -
sum(case when status = 'minus' then total else 0 end) as total,
sum(case when status = 'plus' then amount else 0 end) -
sum(case when status = 'minus' then amount else 0 end) as amount
from your_table
group by product
There is another method using join, which works for the particular data you have provided (which has one "plus" and one "minus" row per product):
select tplus.product, (tplus.total - tminus.total) as total,
(tplus.amount - tminus.amount) as amount
from t tplus join
t tminus
on tplus.product = tminus.product and
tplus.status = 'plus' and
tplus.status = 'minus';
Both this and the aggregation query work well for the data you have provided. In other words, there are multiple ways to solve this problem (each has its strengths).
you can query as below:
select product , sum (case when [status] = 'minus' then -Total else Total end) as Total
, sum (case when [status] = 'minus' then -Amount else Amount end) as SumAmount
from yourproduct
group by product

Oracle SQL Expression to Sum Payments for Six Months based on Selected Month

Basically what I'm trying to do is calculate the average percentage of CreditPayments and the average percentage of CashPayments over a projected period (Lookback 3 months then total all paymnents of the correct type in the previous six months)
So if Average (During Correct Lookback Period) was = 75% Credit & 25% Cash then for my outstanding A/R for that month I could anticapte what % will be Credit & what % will be cash. I'd like to run it for a 12 month period with the lookback changing for each MOS.
Right now my result is always 0, which is obviously wrong. I know the initial inner query is correct; however, the problem happens when the outer query runs. I think the problem lies with the T1. MOS, which I think is limiting the results. Any help would be greatly appreciated.
SELECT
T1.Facility Facility
, T1.CustType CustType
, T1.MOS MOS
, SUM(CASE WHEN (T1.MOS BETWEEN ADD_MONTHS(T1.MOS,-9) AND (ADD_MONTHS(T1.MOS,-3)-1) THEN T1.CredPmts ELSE 0 END)/SUM(CASE WHEN (T1.MOS BETWEEN ADD_MONTHS(T1.MOS,-9) AND (ADD_MONTHS(T1.MOS,-3)-1) THEN T1.Charges ELSE 0 END)
, SUM(CASE WHEN (T1.MOS BETWEEN ADD_MONTHS(T1.MOS,-9) AND (ADD_MONTHS(T1.MOS,-3)-1) THEN T1.CashPmts ELSE 0 END)/SUM(CASE WHEN (T1.MOS BETWEEN ADD_MONTHS(T1.MOS,-9) AND (ADD_MONTHS(T1.MOS,-3)-1) THEN T1.Charges ELSE 0 END)
FROM
(
SELECT
T.FACILITY Facility
, T.CUSTTYPE CustType
, trunc(to_date(T.SERVICEDATE,'j'),'MONTH') MOS
, SUM(CASE WHEN T.TYPE = 'C' THEN T.AMOUNT ELSE 0 END) Charges
, SUM(CASE WHEN (T.TYPE IN ('P1','62','12','75','P6','23') THEN T.AMOUNT ELSE 0 END CredPmts
, SUM(CASE WHEN (T.TYPE IN ('92','57','P3','P9','26','39') THEN T.AMOUNT ELSE 0 END CashPmts
FROM TRANSTABLE T
WHERE
T.FACILITY = '123'
AND T.SERVICEDATE BETWEEN to_char(ADD_MONTHS(to_date('20120101', 'yyyymmdd'),-9), 'j') AND to_char(to_date('20121231', 'yyyymmdd'), 'j')
GROUP BY
T.FACILITY
, T.CUSTTYPE
, trunc(to_date(T.SERVICEDATE,'j'),'MONTH')
)T1
GROUP BY
T1.Facility
, T1.CustType
, T1.MOS
There are mismatched parenthesis in the CASE WHEN clause of the outer query:
WHEN (T1.MOS BETWEEN ADD_MONTHS(T1.MOS,-9) AND (ADD_MONTHS(T1.MOS,-3)-1))
all four places. Can you check it?

How come I am getting the same total values of 2 different columns

I need someone to correct the statement below please. Thank you in advance.
SELECT CATEGORY
--WHAT PERIOD?
,'P3' AS PERIOD
,'2013' AS FISCALYEAR
,COUNT(CASE SecurityLayer WHEN 'dblayer' THEN SecurityLayer ELSE '' END) DB_SEC_COUNT
,COUNT(CASE SecurityLayer WHEN 'Applayer' THEN SecurityLayer ELSE '' END) APP_SEC_COUNT
FROM [db_eCAM].[dbo].[tbl_SecChecks]
GROUP BY CATEGORY
Are you trying to return the # of times each of those things matches? Then use
SELECT CATEGORY
--WHAT PERIOD?
,'P3' AS PERIOD
,'2013' AS FISCALYEAR
,SUM(CASE SecurityLayer WHEN 'dblayer' then 1 else 0 end) AS DB_SEC_COUNT
,SUM(CASE SecurityLayer WHEN 'Applayer' then 1 else 0 end) AS APP_SEC_COUNT
FROM [db_eCAM].[dbo].[tbl_SecChecks]
GROUP BY CATEGORY
Try that.
Instead of counting the columns try summing it (otherwise each row will still be counted regardless of its value):
SELECT CATEGORY
--WHAT PERIOD?
,'P3' AS PERIOD
,'2013' AS FISCALYEAR
,SUM(CASE SecurityLayer WHEN 'dblayer' THEN 1 ELSE 0 END) DB_SEC_COUNT
,SUM(CASE SecurityLayer WHEN 'Applayer' THEN 1 ELSE 0 END) APP_SEC_COUNT
FROM [db_eCAM].[dbo].[tbl_SecChecks]
GROUP BY CATEGORY