Group By and Having Concept in SQL
TABLE
select * from
XXSD_FOOD_TL
FRUIT_NAME
|
UNIT_PRICE
|
QUANTITY
|
PURCHASE_DATE
|
Apple
|
100
|
60
|
01-Jan-20
|
Orange
|
110
|
40
|
01-Jan-20
|
Apple
|
105
|
45
|
02-Jan-20
|
kiwi
|
190
|
22
|
02-Jan-20
|
Banana
|
70
|
20
|
03-Jan-20
|
Pumpkin
|
14
|
120
|
03-Jan-20
|
Banana
|
60
|
23
|
04-Jan-20
|
Tomato
|
13
|
15
|
04-Jan-20
|
Pumpkin
|
11
|
12
|
05-Jan-20
|
Tomato
|
22
|
100
|
05-Jan-20
|
Apple
|
101
|
11
|
06-Jan-20
|
kiwi
|
200
|
19
|
06-Jan-20
|
Apple
|
115
|
12
|
07-Jan-20
|
Tomato
|
11
|
21
|
07-Jan-20
|
Banana
|
65
|
25
|
08-Jan-20
|
Pumpkin
|
16
|
33
|
08-Jan-20
|
Apple
|
105
|
12
|
09-Jan-20
|
Tomato
|
12
|
29
|
09-Jan-20
|
Orange
|
135
|
21
|
10-Jan-20
|
kiwi
|
195
|
26
|
10-Jan-20
|
Group
By Concept
Row Labels
|
Sum of QUANTITY
|
Apple
|
140
|
Banana
|
68
|
kiwi
|
67
|
Orange
|
61
|
Pumpkin
|
165
|
Tomato
|
165
|
SELECT
FRUIT_NAME, COUNT(*)
QUANTITY
FROM
XXSD_FOOD_TL
GROUP BY
FRUIT_NAME
SELECT
FRUIT_NAME, SUM(QUANTITY)
QUANTITY
FROM
XXSD_FOOD_TL
WHERE
FRUIT_NAME in ('Apple','Pumpkin')
GROUP BY
FRUIT_NAME
Having
Concept
Row Labels
|
Sum of QUANTITY
|
Apple
|
140
|
Tomato
|
165
|
Pumpkin
|
165
|
SELECT
FRUIT_NAME, COUNT(*)
QUANTITY
FROM
XXSD_FOOD_TL
GROUP BY
FRUIT_NAME
SELECT
FRUIT_NAME, SUM(QUANTITY)
QUANTITY
FROM
XXSD_FOOD_TL
GROUP BY
FRUIT_NAME
HAVING SUM(QUANTITY) > 100
select
FRUIT_NAME, SUM(QUANTITY)
QUANTITY
from XXSD_FOOD_TL
where
QUANTITY >100
GROUP BY
FRUIT_NAME
Having
and Where
SELECT
FRUIT_NAME, SUM(QUANTITY)
QUANTITY
FROM
XXSD_FOOD_TL
WHERE
FRUIT_NAME in ('Apple','Pumpkin')
GROUP BY
FRUIT_NAME
HAVING SUM(QUANTITY) > 100
Comments
Post a Comment