I am learning SQL and I have table that looks like this:
Id Name Payd Note
1 John 5.00 R:8days;U:5$
2 Adam 5.00 R:8days;
3 John 10.00 R:8days;
4 John 10.00 R:8days;
5 Adam 15.00 R:30days;
I want to make something like this:
Id Name Usage 5.00 10.00 15.00 Sum
1 John 5 5.00 20.00 0 25.00
2 Adam 5.00 0 15.00 20.00
I want to check that note column if there is 'U:5$' in it and then add a 5 to that customer that has 'U:5$' in note column, if it doesnt it doesnt add anything.
My code looks like this:
;with cte as (
select Customer, PaydAmount, PaydAmount as Payd, Note as Usage
from t1
)
select
Customer, Usage
,[4.00] = ISNULL([4.00],0)
,[5.00] = ISNULL([5.00],0)
,[9.00] = ISNULL([9.00],0)
,[10.00] = ISNULL([10.00],0)
,[15.00] = ISNULL([15.00],0)
,[18.00] = ISNULL([18.00],0)
,[20.00] = ISNULL([20.00],0)
,[25.00] = ISNULL([25.00],0)
,[50.00] = ISNULL([50.00],0)
,[Payd] =ISNULL([4.00],0) + ISNULL([5.00],0) + ISNULL([9.00],0) + ISNULL([10.00],0) + ISNULL([15.00],0) + ISNULL([18.00],0) + ISNULL([20.00],0) + ISNULL([25.00],0) + ISNULL([50.00],0)
from cte
pivot (
sum(PaydAmount) for Payd in ([4.00],[5.00],[9.00], [10.00], [15.00],[18.00], [20.00], [25.00], [50.00]))pvt
order by Customer;
I am not sure what your full output represents. But the first three columns are easy to get using conditional aggregation:
select row_number() over (order by (select null)) as id,
name,
max(case when note like '%U:5$' then 5 end) as usage,
sum(case when payd = 5.00 then payd else 0 end) as [5.00],
sum(case when payd = 10.00 then payd else 0 end) as [10.00],
sum(case when payd = 15.00 then payd else 0 end) as [15.00],
sum(payd) as total
from cte
group by name;
Note that the columns for 5, 10, and 15 sort of assume that the value in payd is a decimal/numeric type. Equality comparisons on floats are not recommended.
Related
I'm trying to query a customers table to get the total number of accounts per rep grouped by whether they were created this year or before.
CUSTOMER NAME
ACCOUNT REP
DATE CREATED
The query I'm trying to return would look like.
REP | NEW_ACCOUNTS | OLD_ACCOUNTS | TOTAL
-----------------------------------------
Tom | 100 | 12 | 112
Ted | 15 | 1 | 16
The query I've written looks as follows.
SELECT REP, CASE WHEN YEAR(GETDATE()) > YEAR(DATE_CREATED) THEN 1 ELSE 0 END AS ThisYear
FROM CUSTOMERS
GROUP BY REP, DATE_CREATED
Unfortunately, this is giving me
REP | ThisYear
-----------------------------------------
Tom | 1
Ted | 0
Tom | 0
Ted | 1
Ted | 1
I think you want conditional aggregation:
SELECT REP,
SUM(CASE WHEN YEAR(GETDATE()) = YEAR(DATE_CREATED) THEN 1 ELSE 0 END) AS NEW_ACCOUNTS,
SUM(CASE WHEN YEAR(GETDATE()) > YEAR(DATE_CREATED) THEN 1 ELSE 0 END) AS OLD_ACCOUNTS,
COUNT(*) as TOTAL
FROM CUSTOMERS
GROUP BY REP;
This assumes that creation dates are not in the future -- a reasonable assumption.
If you want one row per REP, then the only column in the GROUP BY should be REP.
You can want conditional aggregation :
SELECT REP,
SUM(CASE WHEN YEAR(GETDATE()) = YEAR(DATE_CREATED) THEN 1 ELSE 0 END) AS NEW_ACCOUNTS,
SUM(CASE WHEN YEAR(GETDATE()) > YEAR(DATE_CREATED) THEN 1 ELSE 0 END) AS OLD_ACCOUNTS, COUNT(*) AS TOTAL
FROM CUSTOMERS
GROUP BY REP;
i have query like this and i wanted to have sum of sales per day
SELECT DATEPART(day,deduction_timestamp) as [day],
[fare_deduction]
FROM [dbfastsprocess].[dbo].[vClosingTransitLog]
WHERE bus_id in ('JEAST', 'MKV004', 'NWTN01')
and YEAR(deduction_timestamp) = ISNULL(2016, YEAR(deduction_timestamp))
and MONTH(deduction_timestamp) = ISNULL(10, MONTH(deduction_timestamp))
GROUP BY DATEPART(day,deduction_timestamp), fare_deduction
with result :
day fare_deduction
--------------------------------
1 10.00
3 15.00
3 2.00
4 10.00
10 20.00
31 12.00
and i wanted the result to be like this..
day fare_deduction
--------------------------------
1 10.00
3 17.00
4 10.00
10 20.00
31 12.00
and take note that not all day have values and it only display the affected
day only. Can help me on these? Thanks!
SELECT DATEPART(day,deduction_timestamp) as [day],
sum(fare_deduction) as fare_deduction
FROM [dbfastsprocess].[dbo].[vClosingTransitLog]
WHERE bus_id in ('JEAST', 'MKV004', 'NWTN01')
and YEAR(deduction_timestamp) = ISNULL(2016, YEAR(deduction_timestamp))
and MONTH(deduction_timestamp) = ISNULL(10, MONTH(deduction_timestamp))
GROUP BY DATEPART(day,deduction_timestamp)
Simply use sum, it will get your required result
SELECT DATEPART(day,deduction_timestamp) as [day],
SUM([fare_deduction]) [fare_deduction]
FROM [dbfastsprocess].[dbo].[vClosingTransitLog]
WHERE bus_id in ('JEAST', 'MKV004', 'NWTN01')
and YEAR(deduction_timestamp) = ISNULL(2016, YEAR(deduction_timestamp))
and MONTH(deduction_timestamp) = ISNULL(10, MONTH(deduction_timestamp))
GROUP BY DATEPART(day,deduction_timestamp)
I don't have much experience with pivot/unpivot and could use some help. I have a SQL query with data as :
Category Account Name Value
001 1234 BALANCE_01 800
001 1234 BALANCE_02 1000
001 1234 BALANCE_03 1500
001 4567 BALANCE_01 900
001 4567 BALANCE_02 1200
001 4567 BALANCE_03 800
I need it to appear as:
Category Account BALANCE_01 BALANCE_02 BALANCE_03
001 1234 800 1000 1500
001 4567 900 1200 800
How do I do this?
Thanks,
Marcie
One way is to do this is by using conditional aggregation:
SELECT Category,
Account,
MAX(CASE WHEN Name = 'BALANCE_01' THEN Value ELSE NULL END) AS BALANCE_01,
MAX(CASE WHEN Name = 'BALANCE_02' THEN Value ELSE NULL END) AS BALANCE_02,
MAX(CASE WHEN Name = 'BALANCE_03' THEN Value ELSE NULL END) AS BALANCE_03
FROM Table
GROUP BY Category, Account
I would just just a group by
SELECT Category, Account,
SUM(CASE WHEN NAME='BALANCE_1' THEN Value ELSE 0 END) AS BALANCE_1,
SUM(CASE WHEN NAME='BALANCE_2' THEN Value ELSE 0 END) AS BALANCE_2,
SUM(CASE WHEN NAME='BALANCE_3' THEN Value ELSE 0 END) AS BALANCE_3
FROM Your_Table_You_Did_Not_Name
GROUP BY Category, Account
Note, if you have more than one row with the same Category, Account and Name this will fail -- but you don't tell us how to handle that.
I have a query output as below
Customer policytype plan amount
Sam ulip P1 250
Sam ulife u1 435
Sam Ulip P2 370
Hazar Ulip P1 679
Hazar Ulife u1 567
And so on ....
I need to transpose above output as follows
Customer ulip ulife
Sam 250 435
Sam 370 Null
Hazar 679 567
Can someone help me to achieve above result in db2
Use conditional Aggregate
SELECT customer,
Max(CASE WHEN policytype = 'ulip' THEN amount END) AS ulip,
Max(CASE WHEN policytype = 'ulife' THEN amount END) AS ulife
FROM Youratable
GROUP BY customer,
CASE WHEN plan IN ( 'p1', 'u1' ) THEN 1 ELSE 0 END
I have a table like this:
Item Qty Price A Price B
abc 5 36.00 0
qwe 8 0 48.00
zxc 6 12.00 0
poi 4 10.00 0
lkj 9 12.00 0
mnb 3 0 14.00
vfr 7 0 6.00
How can I sum the value using SQL ie. if Price A is zero, it will pick Price B. The expected results will be as follows :-
Item Value
abc 180.00
qwe 384.00
zxc 72.00
poi 40.00
lkj 36.00
mnb 42.00
vfr 42.00
SELECT
ITEM
, (Qty * [Price A]) + (Qty * [Price B]) AS Value
FROM
TableName
Use the following if A and B could be non-zero, and you only want to use price A when this occurs:
SELECT Item, CASE WHEN `Price A` != 0 THEN `Price A` * Qty
ELSE `Price B` * Qty
END AS Value
FROM table;
Why not do something like
SELECT SUM(Price A) FROM table; SELECT SUM(Price B) FROM table WHERE Price A = 0;
And then add the results together...
from the example above you could really just select priceA + priceB, since one or the other is always zero, otherwise... I am not quite sure this can be done in pure SQL...
Assuming both can be not null:
SELECT item, QTY * IF(PriceA!=0, PriceA, PriceB) AS VALUE from tableName;