how to sum with case with oracle sql - sql

I m having data in columns as:
item_id
month_in
amount
1
1
1500
1
1
1000
2
1
2500
3
1
2600
3
1
1000
4
1
2700
4
1
1000
1
2
1500
1
2
2000
2
2
1000
3
3
2500
3
3
2500
4
3
1000
4
3
2500
I want to have like this result
item_id
januari
februari
maret
1
2500
3500
0
2
2500
1000
0
3
3600
0
0
4
3700
0
3500
in oracle sql query how to solve this.
please help me
I have try this
select
item_id,
(case month_in=1 then sum(amout) end) AS januari
from table
group by item_id, month_in
order by item_id asc
but not working as I expected

We can try a pivot query here:
SELECT
item_id,
SUM(CASE WHEN month_in = 1 THEN amount ELSE 0 END) AS januari,
SUM(CASE WHEN month_in = 2 THEN amount ELSE 0 END) AS februari,
SUM(CASE WHEN month_in = 3 THEN amount ELSE 0 END) AS maret
FROM yourTable
GROUP BY
item_id
ORDER BY
item_id;

You almost got it. This is the simplest solution, without extra hassle:
SELECT
item_id,
SUM(CASE WHEN month_in = 1 THEN amount ELSE 0 END) AS januari,
SUM(CASE WHEN month_in = 2 THEN amount ELSE 0 END) AS februari,
SUM(CASE WHEN month_in = 3 THEN amount ELSE 0 END) AS maret
from table
group by item_id
order by item_id asc
You don't need to define the months as a separate rowset, because the months are defined as values 1, 2, 3, ... as columns. Similarly, the items are defined by the group by function.

Related

Pivot Table from Query involving Months in TeraData

I have a table generated from a query in TeraData that looks like this:
User_ID Transaction_Month Number_of_Visits
123 1 1
123 2 2
221 4 1
123 5 2
221 3 5
I am trying to create a Pivot table of sorts that will look like:
User_ID Month_1 Month_2 Month_3 Month_4 ..... Month_12
123
That will store the number of visits in each month column.
Any help would be greatly appreciated.
Thanks.
You can do conditional aggregation:
select user_id,
sum(case when transaction_month = 1 then number_of_visits else 0 end) month_1,
sum(case when transaction_month = 2 then number_of_visits else 0 end) month_2,
...
sum(case when transaction_month = 12 then number_of_visits else 0 end) month_12
from mytable
group by user_id

Group by to include case statement inside SQL statement

I have a table which stores purchase info from sellers and table contains rating to every purchase out of 5 stars. I want to have output Group By sellers and Each sellers good(Above 3) and bad(Below 4) ratings count
PurchaseId SellerId PurchaseRating
1 ABC 2
2 ABC 5
3 DEF 1
4 XYZ 2
5 DEF 4
7 ABC 3
OUTPUT
SellerId TotalOrders AvgRating Above3*(4&5) Below4*(1 to 3)
ABC 3 3.3 1 2
DEF 2 2.5 1 1
XYZ 1 2 0 1
For first 3 columns I am getting result using this
Select SellerId, Count(P.Id) TotalOrders, Avg(PurchaseRating) AvgRating,
CASE WHEN P.PurchaseRating >= 4 THEN 1 ELSE 0 END AS Above3*(4&5)
from
[dbo].[AmazonAbeOrdersPurchaseInfo] P
Where PurchaseRating is not null
Group by P.SellerId
order by TotalOrders desc
Unable to identify how to include case in group by clause.
You are trying to do conditional aggreation. The important thing is that you want the conditional expression inside the aggregate function:
select
sellerId,
count(*) totalOrders,
avg(purchaseRating) avgRating,
sum(case when purchaseRating > 3 then 1 else 0 end) above3,
sum(case when purchaseRating < 4 then 1 else 0 end) below4
from [dbo].[AmazonAbeOrdersPurchaseInfo]
where purchaseRating is not null
group by sellerId
order by totalOrders desc

Firebird 3 SQL windows function over()

I need help..
I have table orders like this
id,order_date,price,customer_id
1 01.01.2001 100 1
2 01.02.2001 0 1
3 20.02.2001 0 1
4 04.04.2001 200 1
I need select result like this
id,order_date,price,customer_id,somefield
1 01.01.2001 100 1 100
2 01.02.2001 0 1 100
3 20.02.2001 0 1 100
3 04.04.2001 200 1 200
Try sql like this
select a.id,order_date,
coalesce(a.price,0) price,
customer_id
sum(coalesce(a.price,0)) OVER (order by a.order_date) somefield,
from tb_orders a
where a.customer_id=4583 and a.orderstate = 1
order by a.order_date
but result gives this
id,order_date,price,customer_id,somefield
1 01.01.2001 100 1 100
2 01.02.2001 0 1 100
3 20.02.2001 0 1 100
3 04.04.2001 200 1 300
You could create subgroup:
SELECT *, MAX(price) OVER(PARTITION BY grp) AS somefield
FROM (
select a.id,order_date,
coalesce(a.price,0) price,
customer_id,
sum(CASE WHEN price = 0 THEN 0 ELSE 1 END) OVER (order by a.order_date) grp
from tb_orders a
where a.customer_id=4583
and a.orderstate = 1
) sub
order by order_date;
db<>fiddle demo

SQL Calculations based by field type and group by the type

Database includes FamID, TicketType and Amt
I want to get a calculation for total amount for each tickettype for each family and sort by family high to low based on total for all tickettypes.
Database values are:
FamID TicketType Amt
1 1 10
1 1 10
1 2 20
1 3 30
2 2 20
2 1 10
2 1 10
2 1 10
2 3 30
3 3 30
3 3 30
3 3 30
Would like results to be
Family Type 1 Type 2 Type 3 Total
3 0 0 90 90
2 30 20 30 80
1 20 20 30 70
Am I trying to do too much?
You never specified your RDBMS, but the following pivot query should work across most major ones with little modification:
SELECT t.`Type 1`, t.`Type 2`, t.`Type 3`,
(t.`Type 1` + t.`Type 2` + 2*t.`Type 3`) AS Total
FROM
(
SELECT FamID AS Family,
SUM(CASE WHEN TicketType = 1 THEN Amt ELSE 0 END) AS `Type 1`,
SUM(CASE WHEN TicketType = 2 THEN Amt ELSE 0 END) AS `Type 2`,
SUM(CASE WHEN TicketType = 3 THEN Amt ELSE 0 END) AS `Type 3`,
FROM Tickets
GROUP BY FamID
) t
ORDER BY t.Total DESC

Query to get data with minimum query cost

I have a table, structure like this:
BID_FK AccountID Amount
1 1-1-1-1-1-1-1 4050
1 1-1-1-1-1-1-1 4050
1 1-1-1-1-1-1-1 4050
1 1-1-1-1-1-1-1 4050
1 1-1-1-1-1-1-2 500
1 1-1-1-1-1-1-2 500
1 1-1-1-1-1-1-2 500
1 1-1-1-1-1-1-2 500
2 1-1-1-1-1-1-1 6580
2 1-1-1-1-1-1-1 6580
2 1-1-1-1-1-1-1 6580
2 1-1-1-1-1-1-1 6580
2 1-1-1-1-2-1-1 1000
2 1-1-1-1-2-1-1 1000
2 1-1-1-1-2-1-1 1000
2 1-1-1-1-2-1-1 1000
I want to query for getting out put like this :
AccountID Amount(BID_FK = 1) Amount (BID_FK = 2)
1-1-1-1-1-1-1 16200 26320
1-1-1-1-1-1-2 2000 0
1-1-1-1-2-1-1 0 4000
With minimum query cost because I have large number of data.
Any help would be highly appreciated!
Maybe something like this could help?
SELECT
AccountID,
SUM(CASE WHEN BID_FK=1 THEN Amount ELSE 0 END) AMOUNT1,
SUM(CASE WHEN BID_FK=2 THEN Amount ELSE 0 END) AMOUNT2
FROM yourtable
GROUP BY AccountId
SELECT AccountID,
SUM( CASE WHEN BID_FK=1 THEN Amount ELSE 0 END) AS `Amount(BID_FK = 1)`
SUM( CASE WHEN BID_FK=2 THEN Amount ELSE 0 END) AS `Amount(BID_FK = 2)`
FROM yourTable
GROUP BY AccountID