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
Related
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.
I have several items which belongs to different group, some are unknown group.
I need the sum of each group by date.
I just try below SQL but still not exactly what I want:
with A as (
SELECT PG, EMANAGER_DATE,
CASE WHEN (EMANAGER_DATE = '2021-09') THEN SUM(NET_VALUE_USD) ELSE 0 END AS "2021-09",
CASE WHEN (EMANAGER_DATE = '2021-10') THEN SUM(NET_VALUE_USD) ELSE 0 END AS "2021-10",
CASE WHEN (EMANAGER_DATE = '2021-11') THEN SUM(NET_VALUE_USD) ELSE 0 END AS "2021-11",
CASE WHEN (EMANAGER_DATE = '2021-12') THEN SUM(NET_VALUE_USD) ELSE 0 END AS "2021-12"
FROM table1
GROUP BY PG, EMANAGER_DATE
)
select PG, max("2021-09") AS "2021-09", max("2021-10") AS "2021-10", max("2021-11") AS "2021-11", max("2021-12") AS "2021-12" from A GROUP BY PG
DB: SAP HANA
Data:
Item
PG
NET_VALUE_USD
EMANAGER_DATE
1
1
100
2021-09
2
1
200
2021-10
3
2
300
2021-09
4
2
400
2021-09
5
3
500
2021-09
6
3
200
2021-11
7
3
300
2021-11
8
900
2021-09
Expected result:
PG
2021-09
2021-10
2021-11
?
900
0
0
1
100
200
0
2
700
0
0
3
500
0
500
total
2200
200
500
You can adjust your query and use GROUPING SETS:
SELECT (CASE WHEN GROUPING(PG) = 1 THEN 'Total' ELSE PG END) as PG,
SUM(CASE WHEN EMANAGER_DATE = '2021-09' THEN NET_VALUE_USD ELSE 0 END) AS "2021-09",
SUM(CASE WHEN EMANAGER_DATE = '2021-10' THEN NET_VALUE_USD ELSE 0 END) AS "2021-10",
SUM(CASE WHEN EMANAGER_DATE = '2021-11' THEN NET_VALUE_USD ELSE 0 END) AS "2021-11",
SUM(CASE WHEN EMANAGER_DATE = '2021-12' THEN NET_VALUE_USD ELSE 0 END) AS "2021-12"
FROM _SYS_BIC."APP_SCM/ZCV_APP_SCM_CTOS_PARENT_UNCONFIRMED_BACKLOG_P"
GROUP BY GROUPING SETS ( (PG), () );
I'm using the following SQL request to Informix DB:
select fromQ, toQ, count(callid) as cont_num, type
from some_table
group by fromQ, toQ, type
order by fromQ, toQ;
It produces the result:
fromq toq cont_num type
-----------------------------------
Sales 12 1
English 1 1
MB 59 1
Reception 3 2
Reception 53 1
Service 1 1
MB Sales 1 1
MB English 1 1
This is OK, as expected. Please note there are 2 rows for toq=Reception.
Field WRTYPE can have values only from 1 to 3.
So idea is to make an output like this:
fromq toq cont_num type1 type2 type3
------------------------------------------------
Sales 12 12 0 0
English 1 1 0 0
MB 59 59 0 0
Reception 56 53 3 0
Service 1 1 0 0
MB Sales 1 1 0 0
MB English 1 1 0 0
Is there a simple way to do this?
Use conditional aggregation:
select fromQ, toQ, count(callid) as cont_num,
sum(case when type = 1 then 1 else 0 end) as type_1,
sum(case when type = 2 then 1 else 0 end) as type_2,
sum(case when type = 3 then 1 else 0 end) as type_3
from some_table
group by fromQ, toQ
order by fromQ, toQ;
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
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