I have a requirement to display data according to month side by side.
Below are the records in the table:
|Student ID| Type | Date | Amount($)
|00000001 | Foods |01/01/2009 | 10
|00000001 | Foods |01/02/2009 | 20
|00000002 | Drinks |01/01/2009 | 10
|00000003 | Snacks |01/02/2009 | 10
|00000003 | Drinks |01/02/2009 | 10
The expected results are like below:
|Student ID| Type | Jan | Feb
|00000001 | Foods | 10 | 20
|00000002 | Drinks | 10 | 0
|00000003 | Snacks | 0 | 10
|00000003 | Drinks | 0 | 10
The amount of type Foods for Student ID is displayed according to the month.
I tried to achieve the expected results by using CASE statement like below but it can't achieve like what is expected.
SELECT STUDENT_ID
, TYPE
, CASE WHEN MONTH(DATE)=1 THEN AMOUNT ELSE 0 AS JAN
, CASE WHEN MONTH(DATE)=2 THEN AMOUNT ELSE 0 AS FEB
FROM CANTEEN_SPENT
But the results are like below
|Student ID| Type | Jan | Feb
|00000001 | Foods | 10 | 0
|00000001 | Foods | 0 | 20
|00000002 | Drinks | 10 | 0
|00000003 | Snacks | 0 | 10
|00000003 | Drinks | 0 | 10
The data for Student ID 00000001 should be merged together as shown in the expected example.
Try conditional aggregation:
SELECT STUDENT_ID
, TYPE
, SUM(CASE WHEN MONTH(DATE) = 1 THEN AMOUNT END) AS JAN
, SUM(CASE WHEN MONTH(DATE) = 2 THEN AMOUNT END) AS FEB
FROM CANTEEN_SPENT
GROUP BY STUDENT_ID, TYPE
ORDER BY STUDENT_ID
Related
I am trying to summerize sales date, by month, sales region and type. The problem is, the results change when I try to group by year.
My simplified query is as follows:
SELECT
DAB700.DATUM,DAB000.X_REGION,DAB700.BELEG_ART, // the date, sales region, order type
// calculate the number of orders per month
COUNT (DISTINCT CASE WHEN MONTH(DAB700.DATUM) = 1 THEN DAB700.BELEG_NR END) as jan,
COUNT (DISTINCT CASE WHEN MONTH(DAB700.DATUM) = 2 THEN DAB700.BELEG_NR END) as feb,
COUNT (DISTINCT CASE WHEN MONTH(DAB700.DATUM) = 3 THEN DAB700.BELEG_NR END) as mar
FROM "DAB700.ADT" DAB700
left join "DAB050.ADT" DAB050 on DAB700.BELEG_NR = DAB050.ANUMMER // join to table 050, to pull in order info
left join "DF030000.DBF" DAB000 on DAB050.KDNR = DAB000.KDNR // join table 000 to table 050, to pull in customer info
left join "DAB055.ADT" DAB055 on DAB050.ANUMMER = left (DAB055.APNUMMER,6)// join table 055 to table 050, to pull in product info
WHERE (DAB700.BELEG_ART = 10 OR DAB700.BELEG_ART = 20) AND (DAB700.DATUM>={d '2021-01-01'}) AND (DAB700.DATUM<={d '2021-01-11'}) AND DAB055.ARTNR <> '999999' AND DAB055.ARTNR <> '999996' AND DAB055.TERMIN <> 'KW.22.22' AND DAB055.TERMIN <> 'KW.99.99' AND DAB050.AUF_ART = 0
group by DAB700.DATUM,DAB000.X_REGION,DAB700.BELEG_ART
This returns the following data, which is correct (manually checked):
| DATUM | X_REGION | BELEG_ART | jan | feb | mar |
|------------|----------|-----------|-----|-----|-----|
| 04.01.2021 | 1 | 10 | 3 | 0 | 0 |
| 04.01.2021 | 3 | 10 | 2 | 0 | 0 |
| 04.01.2021 | 4 | 10 | 1 | 0 | 0 |
| 04.01.2021 | 4 | 20 | 1 | 0 | 0 |
| 04.01.2021 | 6 | 20 | 2 | 0 | 0 |
| 05.01.2021 | 1 | 10 | 1 | 0 | 0 |
and so on....
The total number of records for Jan is 117 (correct).
Now I now want to summerize the data in one row (for example, data grouped by region and type)..
so I change my code so that I have:
SELECT
YEAR(DAB700.DATUM),
and
group by YEAR(DAB700.DATUM)
the rest of the code stays the same.
Now my results are:
| EXPR | X_REGION | BELEG_ART | jan | feb | mar |
|------|----------|-----------|-----|-----|-----|
| 2021 | 1 | 10 | 16 | 0 | 0 |
| 2021 | 1 | 20 | 16 | 0 | 0 |
| 2021 | 2 | 10 | 19 | 0 | 0 |
| 2021 | 2 | 20 | 22 | 0 | 0 |
| 2021 | 3 | 10 | 12 | 0 | 0 |
| 2021 | 3 | 20 | 6 | 0 | 0 |
Visually it is correct. But, the total count for January is now 116. A difference of 1. What am I doing wrong?
How can I keep the results from the first code - but have it presented as per the 2nd set?
You count distinct BELEG_NR. This is what makes the difference. Let's look at an example. Let's say your table contains four rows:
DATUM
X_REGION
BELEG_ART
BELEG_NR
04.01.2021
1
10
100
04.01.2021
1
10
200
05.01.2021
1
10
100
05.01.2021
1
10
300
That gives you per day, region and belegart:
DATUM
X_REGION
BELEG_ART
DISTINCT COUNT BELEG_NR
04.01.2021
1
10
2
05.01.2021
1
10
2
and per year, region and belegart
YEAR
X_REGION
BELEG_ART
DISTINCT COUNT BELEG_NR
2021
1
10
3
The BELEG_NR 100 never appears more than once per day, so every instance gets counted. But it appears twice for the year, so it gets counted once instead of twice.
query which calculates the total amount in dollars of stolen goods for each month for restricted and neutral items.
I have 2 tables
first
| UPC | item | in_stock | price | ship_day | class |
1 | 101 | 'generator' | 16 | 5999 | '12-1-2065'| 'restricted'
2 | 102 | 'blank tape' | 30 | 3000 | '12-1-2065'| 'neutral'
second
| UPC | unit_stolen |
1 | 101 | 4 |
1 | 401 | 2 |
If I understand correctly, this is basically a join and group by:
select date_trunc('mon', f.ship_day) as yyyymm,
sum(f.price * s.unit_stolen) filter (where f.class = 'restricted'),
sum(f.price * s.unit_stolen) filter (where f.class = 'neutral')
from first f join
second s
on f.upc = s.upc
group by date_trunc('mon', f.ship_day)
Need a Query to Display daily count of each item bought by customers in columns from 1st day of month to last day
Sample data table "Item"
+--------+--------+----------+---------------+
| Purchase Date | Item Code| Item Name| Price|
|--------+--------+----------+--------------+
| 01-JAN-20 | 11 | Apple | 1 |
| 01-JAN-20 | 11 | Apple | 1 |
| 02-JAN-20 | 12 | Orange | 2 |
| 02-JAN-20 | 11 | Apple | 1 |
| 03-JAN-20 | 12 | Orange | 2 |
| 03-JAN-20 | 12 | Orange | 2 |
| 04-JAN-20 | 12 | Orange | 2 |
| 04-JAN-20 | 11 | Apple | 1 |
+--------+--------+----------+--------------+
SQL Query should Display Daily Count using Item code and Result to be displayed as below table .
Count daily with each day displayed in column base on the day e.g If today is 4th of Jan then count tomorrow will create new column with count result and continues until last day of month or something similar.
+--------+--------+----------+---------------+
| Items | Jan 01| Jan 02| Jan 03|Jan 04| etc
+--------+--------+----------+--------------+
| Apple | 2 | 1 | 2 | 1 |
| Orange | 0 | 1 | 0 | 1 |
+--------+--------+----------+--------------+
If you know what dates you want, you can use conditional aggregation:
select item,
sum(case when purchase_date = '2020-01-01' then 1 else 0 end) as jan_1,
sum(case when purchase_date = '2020-01-02' then 1 else 0 end) as jan_2,
sum(case when purchase_date = '2020-01-03' then 1 else 0 end) as jan_3,
sum(case when purchase_date = '2020-01-04' then 1 else 0 end) as jan_4,
. . .
from items
group by item;
Note that this assumes that purchase_date is really stored as an internal date format. So the comparison is a date constant -- however, that might differ among databases.
If you do not have a specific set of dates in mind, then you will need to use dynamic SQL.
I am working in SQL and have a destination table:
Event ID (key) | Road | total count | motorcycles | cars | trucks | bus
And I have a record like table:
[Event ID | mode of transport | count
1 | bus | 3
1 | cars | 20
1 | trucks | 2
1 | motorcycles | 5
2 | bus | 1
2 | cars | 12
2 | motorcycles | 1][1]
(combination of Event ID and mode of transport combination is unique)
How do I combine the data from the second table into the first easily as result:
Event ID (key) | Road | total count | motorcycles | cars | trucks | bus
1 | ... | ... | 5 | 20 | 2 | 3
2 | ... | ... | 1 | 12 | | 1
I am looking for a way that can incorporate the record data from the second table in one SQL structure / statement. Thank you!
You can use conditional aggregation:
select eventid, road,
sum(count) as total_count,
sum(case when mode = 'motorcycles' then count end) as cnt_motorcycles,
sum(case when mode = 'car' then count end) as cnt_car,
sum(case when mode = 'bus' then count end) as cnt_bus,
sum(case when mode = 'truck' then count end) as cnt_truck
from t
group by eventid, road;
given the following table
+-----------------------------+
| id | type | price | item_id |
|-----------------------------|
| 1 | 1 | 20 | 22 |
|-----------------------------|
| 2 | 1 | 22 | 22 |
|-----------------------------|
| 3 | 2 | 19 | 22 |
|-----------------------------|
| 4 | 2 | 11 | 22 |
|-----------------------------|
| 5 | 1 | 08 | 22 |
|-----------------------------|
| 6 | 2 | 25 | 22 |
+-----------------------------+
I am trying to select the data to create a view as follows in a single row
+-------------------------------------+
| type1_range | type2_range | item_id |
|-------------------------------------|
| 08 - 22 | 11 - 25 | 22 |
+-------------------------------------+
type1_range and type2_range are the minimum and maximum price for each types.
I can get the data in couple of rows using
SELECT type, MAX (price) , MIN (price)
FROM table
where item_id=22 GROUP BY type;
+----------------------------+
| type | max | min | item_id |
|----------------------------|
| 1 | 22 | 08 | 22 |
|----------------------------|
| 2 | 25 | 11 | 22 |
+----------------------------+
But I am trying to concat the rows like this:
+-------------------------------------+
| type1_range | type2_range | item_id |
|-------------------------------------|
| 08 - 22 | 11 - 25 | 22 |
+-------------------------------------+
What would be sql required for this?
Something like this:
SELECT
CONCAT(
MIN(CASE WHEN type = 1 THEN price END),
' - ',
MAX(CASE WHEN type = 1 THEN price END)
) as type1range,
CONCAT(
MIN(CASE WHEN type = 2 THEN price END),
' - ',
MAX(CASE WHEN type = 2 THEN price END)
) as type2range.
item_id
FROM table
WHERE item_id = 22
GROUP BY item_id
You've tagged two different database systems (please avoid doing this) but I believe they do both support CONCAT() for string concatenation
If you want to omit the item_id from the select list (you already know it's item 22) you can remove the GROUP BY. Alternatively if you remove the WHERE and leave the group by you'll get a row for each item_id
To get more of an idea as to how it works, remove the concat and the min/max - you'll see that the case when causes the price to show up only if the type is 1 (in the type 1 range column) otherwise it's null. It's the. Trivial for the min and max to work on just type 1 or just type 2 data for each column. It's actually a form of pivot query if you want to read up on them more
A straight forward approach would be having type1_range and type2_range as two sub-queries and join with the distinct id's like shown below,
SELECT t.item_id,type1_range,type2_range
FROM (Select distinct item_id from table) t
LEFT join
(SELECT item_id,type, concat(MIN(price),'-' ,MAX(price) ) as type1_range
FROM table
where type=1
GROUP BY item_id,type)type1 on type1.item_id=t.item_id
LEFT join
(SELECT item_id,type, concat(MIN(price),'-' ,MAX(price) ) as type2_range
FROM table
where type=2
GROUP BY item_id,type)type2 on type2.item_id=t.item_id