mysql query does not return expected result - mysql

I have two mysql tables.
table-1: table-item:
id | itemid | itemname | catid
---------------------------------
1 | 1 | Pen | 1
2 | 2 | Pencil | 1
3 | 3 | Sharpner | 1
4 | 4 | Book | 2
5 | 5 | Khata | 2
6 | 6 | Bag | 3
7 | 7 | File | 3
---------------------------------
table-2: yearly-item:
id | itemid |catid| year
-----------------------------
1 | 1 | 1 | 2015
2 | 3 | 1 | 2015
3 | 4 | 2 | 2015
4 | 6 | 3 | 2015
5 | 1 | 2 | 2016
6 | 1 | 1 | 2016
------------------------------
I want to get a list of items of catid-1 from table-item for the year-2016 which is not present in yearly-item table.
id | itemid | itemname | catid
---------------------------------
2 | 2 | Pencil | 1
3 | 3 | Sharpner | 1
---------------------------------
for that purpose, while executing this query:
SELECT * FROM table_item t LEFT JOIN yearly_item y ON t.itemid=y.itemid AND t.catid=y.catid WHERE t.catid=1 AND y.year='2016' AND y.itemid IS NULL
it is giving this result:
MySQL returned an empty result set (i.e. zero rows).

Try
select b.itemid, b.itemname, b.catid, a.year
from yearly_item a, item b
where year = 2016
and b.itemid != a.itemid
and b.catid = 1
and a.catid = 1
http://sqlfiddle.com/#!9/cfd66/14/0

Related

GROUP sum BY two tables by joining

I'm trying to write a SQL query that will correctly group sales items sold_qyt and sub-total-price together as per product's category so I can show this on the printable invoice that product from Jelly Sheet = 4 at a rate of 62 subtotal for this category product is 248(4 * 62 = 248). but when I try to run the below-mentioned query it shows out-put as 12 but I want subtotal and sold_qyt segregated base on category.
I have tried to run different queries just one query gives the output which is mentioned below and this is for just the sum of all sold_qyt. DB example is also shown below
DB Example: (For better understanding)
Table # 1:
Category
ID | code | name
1 | 1 | jelly sheet
2 | 2 | 9D Glass
3 | 3 | Polished Glass
Table # 2:
Product:
ID | code | name | cost | category_id | price
1 | 1 | IP11JS | 50 | 1 | 62
2 | 2 | IP12JS | 50 | 1 | 62
3 | 3 | IP119D | 40 | 2 | 55
4 | 4 | IP129D | 40 | 2 | 55
5 | 5 | IP11PG | 18 | 3 | 25
6 | 6 | IP12PG | 18 | 3 | 25
Table # 3:
sale_items:
ID | sale_id | product_id | product_code | product_name | unit_price | sold_qyt | subtotal |
1 | 1 | 1 | 1 | IP11JS | 62 | 2 | 124 |
2 | 1 | 2 | 2 | IP12JS | 62 | 2 | 124 |
3 | 1 | 3 | 3 | IP119D | 55 | 2 | 110 |
4 | 1 | 4 | 4 | IP129D | 55 | 2 | 110 |
5 | 1 | 5 | 5 | IP11PG | 25 | 2 | 50 |
6 | 1 | 6 | 6 | IP12PG | 25 | 2 | 50 |
7 | 2 | 7 | 1 | IP11JS | 62 | 2 | 124 |
8 | 2 | 8 | 2 | IP12JS | 62 | 2 | 124 |
9 | 2 | 9 | 3 | IP119D | 55 | 2 | 110 |
10 | 2 | 10 | 4 | IP129D | 55 | 2 | 110 |
11 | 2 | 11 | 5 | IP11PG | 25 | 2 | 50 |
12 | 2 | 12 | 6 | IP12PG | 25 | 2 | 50 |
SQL Query which is run by me:
SELECT sale_id,
SUM(sold_qyt) AS sold_qyt
FROM sale_items
GROUP BY sale_id
kindly help me with this difficulty thanks in advance
Update: 1-21-2021
i execute new query
SELECT (sma_sale_items.sale_id, sma_categories.code AS sma_products.category_id, sma_products.code AS sma_sale_items.product_code,)
SUM(sold_qyt) AS sold_qyt
SUM(subtotal) AS subtotal
FROM sma_sale_items
LEFT JOIN sma_products ON sma_products.id=sma_sale_items.product_id
LEFT JOIN sma_categories ON sma_categories.code=sma_products.category_id
GROUP BY sma_sale_items.sale_id
ORDER BY sma_categories
but no luck :(
I want the output like this:
Expected OUT PUT:
ID | sale_id | category_name | sold_qyt | subtotal |
1 | 1 | Jelly Sheet | 4 | 248 |
2 | 1 | 9D Glass | 4 | 220 |
3 | 1 | Polished Glass | 4 | 100 |
4 | 2 | Jelly Sheet | 4 | 248 |
5 | 2 | 9D Glass | 4 | 220 |
6 | 2 | Polished Glass | 4 | 100 |
The ID column in your expected result set is very misleading - it appears to be just new ID value for the output result set rather than any of the ID values from the source tables.
If it is important for you then you can use this query:
SELECT ROW_NUMBER() OVER (ORDER BY sale_id, category_id),
sale_id,
category_name,
sold_qty,
subtotal
FROM (
SELECT c.ID as category_id,
si.sale_id,
c.[name] as category_name,
SUM(si.sold_qty) as sold_qty,
SUM(si.subtotal) as subtotal
FROM sale_items si
JOIN product p ON p.ID = si.product_code
JOIN category c ON c.ID = p.category_id
GROUP BY c.ID,
si.sale_id,
c.[name]
) r
If it is not relevant and you only want the sale_id, category_name and the totals then simplify it to:
SELECT si.sale_id,
c.[name] as category_name,
SUM(si.sold_qty) as sold_qty,
SUM(si.subtotal) as subtotal
FROM sale_items si
JOIN product p ON p.ID = si.product_code
JOIN category c ON c.ID = p.category_id
GROUP BY si.sale_id,
c.[name]
ORDER BY sale_id, category_name

Update records based on date on MySQL using joins

I have all those tables above.
car_model_tbl
-----------------------------
id | car_model_name|status |
-----------------------------
1 | seria_1 | 1 |
-----------------------------
2 | golf_4 | 1 |
-----------------------------
3 | C_Class | 1 |
-----------------------------
4 | golf_5 | 1 |
-----------------------------
5 | seria_2 | 0 |
-----------------------------
car_manufacturer_tbl
-------------------------
id |car_manufactu_name |
-------------------------
1 | bmw |
-------------------------
2 | volkswagen |
-------------------------
3 | mercedes |
-------------------------
car_service_tbl
---------------------------------
id | model_id| service_date |
---------------------------------
1 | 1 | 2018-03-10 |
---------------------------------
2 | 2 | 2018-02-10 |
---------------------------------
3 | 1 | 2018-01-10 |
---------------------------------
4 | 1 | 2017-12-10 |
---------------------------------
5 | 2 | 2017-12-10 |
---------------------------------
6 | 3 | 2018-02-10 |
---------------------------------
7 | 2 | 2018-01-10 |
---------------------------------
9 | 4 | 2018-03-10 |
---------------------------------
10 | 4 | 2018-02-10 |
---------------------------------
11 | 5 | 2018-02-10 |
---------------------------------
car_model_manufacturer_relation
-------------------------------------------------
id | model_id | manufactu_id| service_status |
-------------------------------------------------
1 | 1 | 1 | 1 |
-------------------------------------------------
2 | 5 | 1 | 1 |
-------------------------------------------------
3 | 2 | 2 | 1 |
-------------------------------------------------
4 | 4 | 1 | 1 |
-------------------------------------------------
5 | 2 | 2 | 1 |
-------------------------------------------------
6 | 3 | 3 | 1 |
-------------------------------------------------
I need to update car_model_manufacturer_relation.service_status = '0'
where car_service_tbl.service_date < "2018-03-01".
In this case car_model_manufacturer_relation.service_status of models 2, 3 and 5 should be set to '0' because every car_service_tbl.service_date for these models is smaller than "2018-03-01".
However, for models 1 and 4 car_model_manufacturer_relation.service_status should stay '1' because even that they have records smaller than "2018-03-01" they also have bigger dates ex. "2018-03-10".
I am trying to create a query for this but until now without success.
You'll need to nest a grouped query, to get the MAX date per model, and update from that.
update car_model_manufacturer_relation as cmmr,
(select model_id, max(service_date) as check_date
from car_service_tbl
group by model_id) as cst
set cmmr.service_status = '0'
where cmmr.model_id = cst.model_id
and cst.check_date < "2018-03-01"
Where you're using more than one table and the table names include underscores, I try and alias the tables to make the code a little shorter and easier on the eye, hence the use of cmmr and cst as table aliases.
The MAX date has also been renamed for clarity as check_date. You can of course name this anything you wish.
With sub query:
UPDATE car_model_manufacturer_relation c
LEFT join (SELECT model_id, service_date FROM car_service_tbl ORDER BY service_date DESC LIMIT 1) as s ON s.model_id = c.model_id
SET service_status=0
WHERE c.service_date < "2018-03-01"
#tyro - be careful with your solution, as a LEFT JOIN would update the service status to 0 when there wasn't a service date within the car_service_tbl. You would need to use a full join, rather than just the LEFT JOIN as you suggested in order to update the records correctly I feel.

Deleting the duplicate records, by comparing it with a max value in another column - mysql

id | rem_id |max_val
-- | ------ |------
1 | 1 | 7
2 | 2 | 6
3 | 3 | 1
4 | 1 | 1
5 | 2 | 1
6 | 3 | 1
In the above table I need to remove the duplicates from the rem_id column with min val in the max_val column
id | rem_id |max_val
-- | ------ |------
1 | 1 | 7
2 | 2 | 6
3 | 3 | 1
This will delete all but the highest valued max_val for each rem_id, or multiples of the same max_val by deleting those with a higher id:
delete t
from t
left join t as i
on i.rem_id = t.rem_id
and (i.max_val > t.max_val
or (i.max_val = t.max_val and i.id < t.id)
)
where i.id is not null;
rextester demo: http://rextester.com/QKIK17666
returns:
+----+--------+---------+
| id | rem_id | max_val |
+----+--------+---------+
| 1 | 1 | 7 |
| 2 | 2 | 6 |
| 3 | 3 | 1 |
+----+--------+---------+

GROUP BY twice and SUM

I have table with data like this
code | month | sales | value
1 | 1 | 1 | 4
1 | 1 | 2 | 2
1 | 2 | 1 | 2
2 | 1 | 1 | 4
and I want to group the data like this
code | month | sales | value
1 | 1 | 1 | 6
1 | 1 | 2 | 2
2 | 1 | 1 | 4
so far I have this mysql query
SELECT code,month,sales,sum(value) as value FROM data GROUP BY code
but it only gives this result
code | month | sales | value
1 | 1 | 1 | 8
2 | 1 | 1 | 4
Any suggestion please?
These will give u expected result:
SELECT code,month,sales,sum(value) as value FROM data GROUP BY code, sales

How to get all list of data from a table, where it does not appear on another?

I have 5 tables as per below. How is it possible for me to get a list of all training companies that does not have training class data (which means no female or male students), all within a mysql query statement?
select training_companies.*
from training_companies
left join training_centers on training_centers.training_company_id = training_companies.id
left join training_center_programmes on training_centers.id = training_center_programmes.training_center_id
left join training_class_data on training_class_data.training_center_programme_id = training_center_programmes.id
where training_companies.id IS NULL
So far I could only get to here, but I supposed this is wrong. Kindly advise. Thanks.
training_class_data
id | student_category_id | training_centre_programme_id | female | male
1 | 1 | 1 | 10 | 10
2 | 1 | 2 | 10 | 10
3 | 2 | 1 | 10 | 10
4 | 3 | 1 | 10 | 10
training_programmes
id | name
1 | Yoga
2 | Pilates
training_center_programmes
id | training_center_id | status | training_programme_id
1 | 1 | 1 | 1
2 | 2 | 1 | 1
3 | 3 | 1 | 1
4 | 4 | 1 | 2
5 | 5 | 1 | 2
training_centers
id | name | address | postal code | training_company_id
1 | TF Center 1 | abc | 1234 | 1
2 | TF Center 2 | abc | 1234 | 1
3 | TF Center 3 | abc | 1234 | 1
4 | SFT Center 1 | xyz | 2345 | 2
5 | SFT Center 2 | xyz | 2345 | 2
6 | KFT Center 1 | cbd | 4234 | 3
training_companies
id | name | address | postal code
1 | Trim Fitness | abc | 1234
2 | Stay Fit Training | xyz | 2345
3 | Keep Fit Trainers | cbd | 4234
How is it possible for me to get a list of all training companies that
does not have training class data
You just need to use condition d.training_centre_programme_id IS NULL instead of training_companies.id IS NULL:
select DISTINCT c.*
FROM training_companies AS c
LEFT JOIN training_centers AS tc ON tc.training_company_id = c.id
LEFT JOIN training_center_programmes AS p ON p.training_center_id = tc.id
LEFT JOIN training_class_data AS d ON d.training_centre_programme_id = p.id
WHERE d.training_centre_programme_id IS NULL;
SQL Fiddle Demo