MYSQL Select using Left Join, Count, Group by - mysql

I have this query:
SELECT c.`id`, w.`qty`, COUNT(c.`id`) AS pieces, c.`location`
FROM `control` AS c
LEFT JOIN `warehouse` AS w ON w.`id` = c.`id`
WHERE c.`code` = '40'
GROUP BY c.`id`
I have these tables:
`control` c `warehouse` w
+----+--------+------+----------+ +------+-------+
| id | pieces | code | location | | id | qty |
+----+--------+------+----------+ +------+-------+
| 112| 112-1 | 40 | london | | 112 | 3 |
| 112| 112-2 | 40 | london | | 113 | 3 |
| 112| 112-3 | 40 | london | | 114 | 1 |
| 113| 113-1 | 40 | italy | | 115 | 1 |
| 113| 113-2 | 40 | italy | +--------------+
| 113| 113-3 | 40 | italy |
| 114| 114-1 | 41 | france |
| 115| 115-1 | 41 | france |
| 112| 112-1 | 40 | germany |
| 112| 112-2 | 40 | germany |
| 112| 112-3 | 40 | germany |
| 113| 112-1 | 40 | russia |
| 113| 112-2 | 40 | russia |
| 113| 112-3 | 40 | russia |
| 112| 112-1 | 40 | poland |
| 112| 112-2 | 40 | poland |
| 112| 112-3 | 40 | poland |
+-------------------------------+
Im getting this:
actual output
+-----+-----+--------+----------+
| id | qty | pieces | location |
+-----+-----+--------+----------+
| 112 | 3 | 9 | poland |
| 113 | 3 | 6 | russia |
+-------------------------------+
I'm trying to get this result:
desired output
+-----+-----+--------+----------+
| id | qty | pieces | location |
+-----+-----+--------+----------+
| 112 | 3 | 3 | london |
| 113 | 3 | 3 | italy |
| 112 | 3 | 3 | germany |
| 113 | 3 | 3 | russia |
| 112 | 3 | 3 | poland |
+-------------------------------+
Is possible this result? maybe tweaking my query?
I tried without GROUP BY but in that case i just get 1 row totalizing pieces.

If you want to separate the different locations to different rows, you need to add that column to the group by clause:
SELECT c.`id`, w.`qty`, COUNT(c.`id`) AS pieces, c.`location`
FROM `control` AS c
LEFT JOIN `warehouse` AS w ON w.`id` = c.`id`
WHERE c.`code` = '40'
GROUP BY c.`id`, c.`location`
-- Here ---------^

I suspect that you just need to add qty and location to the group by clause:
SELECT c.`id`, w.`qty`, COUNT(*) AS pieces, c.`location`
FROM `control` AS c
LEFT JOIN `warehouse` AS w ON w.`id` = c.`id`
WHERE c.`code` = '40'
GROUP BY c.`id`, w.`qty`, c.`location`
Starting MySQL 5.7, it is mandatory to list all-non aggregated columns in the group by clause (unless you change default sql option ONLY_FULL_GROUP_BY); most other databases also implement this constraint. I would recommend getting used to it...
Side notes:
COUNT(c.id) is better written COUNT(*), since id seems like a not nullable column
generally spearking, you shoud avoid using backticks around table and column names unless when absolutly necessary.

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

Not getting desired output in mysql

I am firing a query in mysql but not getting desired output.
this is the code:
select team_name,
sum(semis.points+final.points) as final_points
from semis
inner join final on semis.sid=final.sid
inner join teams on teams.tid=semis.tid
group by semis.tid
union
select team_name,
semis.Points
from semis
inner join teams on semis.tid=teams.tid
left join final on semis.sid=final.sid
where final.sid is null;
OUTPUT:
+-----------------------+--------------+
| team_name | final_points |
+-----------------------+--------------+
| BioTech & BioChem | 7 |
| Chemistry | 7 |
| Botany & Zoology | 7 |
| Physics & Electronics | 17 |
| BCA | 19 |
| BCOM | 11 |
| Gujarati | 10 |
| English | 10 |
| Economics | 20 |
| BCOM | 3 |
| Chemistry | 3 |
| English | 3 |
+-----------------------+--------------+
and the result i want to fetch
+-----------------------+--------------+
| team_name | final_points |
+-----------------------+--------------+
| BioTech & BioChem | 7 |
| Chemistry | 10 |
| Botany & Zoology | 7 |
| Physics & Electronics | 17 |
| BCA | 19 |
| BCOM | 14 |
| Gujarati | 10 |
| English | 13 |
| Economics | 20 |
+-----------------------+--------------+
Adding last 3 values to english,bcom,chemistry increasing it by 3 and making a total of BCOM: 14, Chemistry:10 , English: 13
From the sample data you posted and the expected results it looks like you can do it without UNION, by left joining final and with coalesce() for final.points:
select team_name, sum(semis.points + coalesce(final.points, 0)) as final_points
from semis
inner join teams on teams.tid=semis.tid
left join final on semis.sid=final.sid
group by semis.tid

Replace < 1 values in an aggregate SUM T-SQL query

I am try to avoid using #temp to get an average where a field from table contains just a few less than 1 values. Convert to varchar, replace then convert back seems ugly. Suggestions very welcomed!!
Select
(select cmp_zip from company where cmp_id = ord_originpoint) as OriginZip,
ord_originpoint as OriginPoint,
replace((select cty_nmstct from company where cmp_id = ord_originpoint),'/','') as Origin_City_State,
(select cmp_zip from company where cmp_id = ord_destpoint) as DestZip,
ord_destpoint as DestPoint,
replace((select cty_nmstct from company where cmp_id = ord_destpoint),'/','') as Dest_City_State,
COUNT(ord_hdrnumber) as OrdCount,
SUM(ord_rate)/COUNT(ord_hdrnumber) as AvgRate,
SUM(ord_totalmiles)/COUNT(ord_hdrnumber) as AvgMiles,
(SUM(ord_rate) / COUNT(ord_hdrnumber)) / (SUM(ord_totalmiles) / COUNT(ord_hdrnumber)) as AvgRevperMiles
from
orderheader (NOLOCK)
where ord_billto <> 'CSXJAC01'
and ord_revtype1 = 'NE'
and ord_status = 'CMP'
and ord_bookdate > GETDATE() - 730
and ord_completiondate < GETDATE()
and ord_totalmiles > 0
group by
ord_originpoint,ord_destpoint
order by
OrdCount desc
If I exclude columns less than zero in the totalmiles column I get :
+-------+----------+---------------+-------+-------+----------+-----------------+-------+---+------+-----+
| 8850 | DSDMIL | MILLTOWN | NJMID | 7206 | NORELI | ELIZABETH | NJ | 1 | 243 | 25 |
| 7047 | CSXNOR | NORTH BERGEN | NJ | 11550 | NASHEM | HEMPSTEAD | NYNAS | 1 | 492 | 34 |
| 7047 | CSXNOR | NORTH BERGEN | NJ | 7022 | PERFAI | FAIRVIEW | NJBER | 1 | 190 | 1 |
| 17013 | PEPCAR01 | CARLISLE | PA | 21224 | CSXBAL | BALTIMORE | MD | 1 | 350 | 97 |
| 23944 | GARKEN | KENBRIDGE | VA | 21224 | CSXBAL | BALTIMORE | MD | 1 | 814 | 230 |
| 21224 | CSXBAL | BALTIMORE | MD | 18202 | HAZHAZ04 | HAZLETON | PA | 1 | 621 | 161 |
| 17055 | WOOMEC | MECHANICSBURG | PACUM | 21224 | CSXBAL | BALTIMORE | MD | 1 | 355 | 85 |
| 23139 | MORPOW01 | POWHATAN | VA | 21224 | CSXBAL | BALTIMORE | MD | 1 | 1376 | 186 |
| 17109 | PEPHAR | HARRISBURG | PA | 21224 | CSXBAL | BALTIMORE | MD | 1 | 350 | 78 |
| 21224 | CSXBAL | BALTIMORE | MD | 20066 | WASWAS05 | WASHINGTON | DC | 1 | 675 | 54 |
| 21224 | CSXBAL | BALTIMORE | MD | 20743 | GRACAP | CAPITOL HEIGHTS | MD | 1 | 300 | 45 |
| 7047 | CSXNOR | NORTH BERGEN | NJ | 7866 | ROCROC05 | ROCKAWAY | NJ | 1 | 243 | 34 |
| 21224 | CSXBAL | BALTIMORE | MD | 20772 | BOBUPP | UPPER MARLBORO | MD | 1 | 283 | 37 |
+-------+----------+---------------+-------+-------+----------+-----------------+-------+---+------+-----+
I'm still not entirely sure of the exact question that you're asking, but the way that you're doing subqueries within your SELECT statement is always going to be poor for performance, try using correct joins, something like this;
SELECT
c1.cmp_zip AS OriginZip
,oh.ord_originpoint AS OriginPoint
,replace(c1.cty_nmstct),'/','') AS Origin_City_State
,c2.cmp_zip AS DestZip
,oh.ord_destpoint AS DestPoint
,replace(c2.cty_nmstct,'/','') AS Dest_City_State
,COUNT(oh.ord_hdrnumber) AS OrdCount
,SUM(oh.ord_rate)/COUNT(oh.ord_hdrnumber) AS AvgRate
,SUM(oh.ord_totalmiles)/COUNT(oh.ord_hdrnumber) AS AvgMiles
,(SUM(oh.rd_rate)/COUNT(oh.ord_hdrnumber))/( SUM(oh.ord_totalmiles)/COUNT(oh.ord_hdrnumber)) AS AvgRevperMiles
FROM orderheader oh
LEFT JOIN company c1
ON oh.ord_originpoint = c1.cmp_id
LEFT JOIN company c2
ON oh.ord_destpoint = c2.cmp_id
WHERE oh.ord_billto <> 'CSXJAC01'
AND oh.ord_revtype1 = 'NE'
AND oh.ord_status = 'CMP'
AND oh.ord_bookdate > GETDATE() - 730
AND oh.ord_completiondate < GETDATE()
AND oh.ord_totalmiles > 0
GROUP BY
c1.cmp_zip
,oh.ord_originpoint
,replace(c1.cty_nmstct),'/','')
,c2.cmp_zip
,oh.ord_destpoint
,replace(c2.cty_nmstct,'/','')
ORDER BY OrdCount DESC
You may need to tweak the odd field (i'm not sure the table that contains the field rd_rate for example, althouth this may be a typo for ord_rate at a guess)

MySQL: SELECT value and a minimum value on certain conditions in the same row

(MySQL) In 'simple' terms I need to add a minimum price column.
That is, the minimum price for each unique combination of PA and DA records.
Example Raw Data
id | PA | DA | price |
---|-----|------------|--------|
1 | SW1 | PO19 | 100 |
1 | W6 | E16 | 5 |
2 | SW1 | PO19 | 90 |
2 | W6 | E16 | 8 |
3 | TW6 | SO14 | 2000 |
3 | W6 | E16 | 9 |
Output from Example
id | PA | DA | price | MIN price|
---|-----|------------|--------|--------- |
1 | SW1 | PO19 | 100 | 90 |
1 | W6 | E16 | 5 | 5 |
2 | SW1 | PO19 | 90 | 90 |
2 | W6 | E16 | 8 | 5 |
3 | TW6 | SO14 | 2000 | 2000 |
3 | W6 | E16 | 9 | 5 |
e.g. above: for PA=SW1, DA=PO19 the MIN price=90 (id=2).
Ideally I would also like to only SELECT a particular id, but it still returns the "global" minimum.
e.g. if I want to select id=2, it returns:
id | PA | DA | price | MIN price|
---|-----|------------|--------|--------- |
2 | SW1 | PO19 | 90 | 90 |
2 | W6 | E16 | 8 | 5 |
I would post some attempts I've made but they've been useless attempts.
Regards,
George
The sub-select with the minimum price can be joined to the original table to get your result.
SELECT p.id, p.pa, p.da, p.price, minp.price min_price
FROM prices p
JOIN (SELECT pa, da, min(price) price from prices group by pa, da) minp
ON minp.pa = p.pa and minp.da = p.da
WHERE p.id = 2
You can use the the subquery like this
check output here sqlFiddle
select p.id,p.pa,p.da,
(select min(s.price)
from sample s
group by s.pa,s.da
having s.pa=p.pa and s.da=p.da)
from sample p
mind the formatting

Minimum price selection for product when database stores the prices in differrent currencies

I geather prices for products in different online store which are in United States (us), United Kingdom (uk) and etc. and hold it in one mysql TABLE (com) in local currency.
For example, for United States price in US Dollar, for United Kingdom - in GB pound.
TABLE com
---------------------------------------------------------------------------------------
| AUTO_INC | COUNTER | ID | CONC | VOLUME | PRICE | SHOP | DATE_G | COUNTRY |
|----------|---------|------|-----------|--------|-------|-----------|--------|---------|
| 115124 | 76720 | 2399 | prod_name | 13 | 34.23 | store1.us | 3 | us |
| 115186 | 50952 | 2399 | prod_name | 13 | 36 | store2.us | 3 | us |
| 115187 | 45828 | 2399 | prod_name | 13 | 37.44 | store3.us | 3 | us |
| 116448 | 73419 | 2399 | prod_name | 11.6 | 48 | store4.us | 3 | us |
| 116449 | 73421 | 2399 | prod_name | 13 | 65.5 | store4.us | 3 | us |
| 133334 | 22154 | 2399 | prod_name | 13 | 36.95 | store5.us | 4 | us |
| 133386 | 31646 | 2399 | prod_name | 13 | 37.44 | store3.us | 4 | us |
| 134828 | 54667 | 2399 | prod_name | 11.6 | 48 | store4.us | 4 | us |
| 134929 | 54670 | 2399 | prod_name | 13 | 65.5 | store4.us | 4 | us |
| 133337 | 22155 | 2399 | prod_name | 13 | 26.95 | store1.uk | 4 | uk |
| 133387 | 31647 | 2399 | prod_name | 13 | 17.44 | store2.uk | 4 | uk |
| 134829 | 54668 | 2399 | prod_name | 11.6 | 30 | store3.uk | 4 | uk |
| 134830 | 54671 | 2399 | prod_name | 13 | 45.5 | store4.uk | 4 | uk |
---------------------------------------------------------------------------------------
To convert prices from local currencies to US dollar I have created TABLE my_currency
TABLE my_currency
--------------------------------------
| AUTO_INC | DOMAIN | EX_RATE | DATE_G |
|----------|--------|---------|--------|
| 235 | uk | 0.6066 | 4 |
| 236 | us | 1 | 4 |
| 237 | uk | 0.6066 | 3 |
| 238 | us | 1 | 3 |
--------------------------------------
If I want to select minimum prices for us online stores (where COUNTRY = 'us'), I use the following query:
SELECT t1.* FROM com as t1
INNER JOIN (
SELECT id, conc, volume, min(price) as usd_price, date_g
FROM com
WHERE id=2399 AND date_g=4 AND country='us'
GROUP BY conc, volume) as t2
ON t1.conc=t2.conc and t1.volume=t2.volume and t1.id=t2.id and t1.price=t2.usd_price and t1.date_g=t2.date_g
ORDER BY conc DESC, volume DESC
And I get CORRECT result:
| AUTO_INC | COUNTER | ID | CONC | VOLUME | PRICE | SHOP | DATE_G | COUNTRY |
|----------|---------|------|-----------|--------|-------|-----------|--------|---------|
| 133334 | 22154 | 2399 | prod_name | 13 | 36.95 | store5.us | 4 | us |
| 134828 | 54667 | 2399 | prod_name | 11.6 | 48 | store4.us | 4 | us |
But now my goal is to select minimum price from all stores (country in ('us', 'uk')) and take into account that database holds prices in local currencies:
1. convert prices from local currencies to us dollar
2. select minimum prices in us dollar
So I tried to use the following query:
SELECT t1.auto_inc, t1.id, t1.conc, t1.volume, (t1.price / my_currency.ex_rate) as sub_price, t1.date_g
FROM com as t1
inner join my_currency
ON t1.country=my_currency.domain AND t1.date_g=my_currency.date_g
inner join (
select com.id, com.conc, com.volume, min(com.price / my_currency.ex_rate) as usd_price, com.date_g
from com
inner join my_currency
ON com.country=my_currency.domain AND com.date_g=my_currency.date_g
WHERE com.id=2399 AND com.date_g=4 AND com.country in ('us', 'uk')
GROUP BY conc, volume) as t2
on
t1.id=t2.id and
t1.conc=t2.conc and
t1.volume=t2.volume and
(t1.price / my_currency.ex_rate)=t2.usd_price and
t1.date_g=t2.date_g
ORDER BY conc DESC, volume DESC
But I get INCORRECT result:
| AUTO_INC | ID | CONC | VOLUME | SUB_PRICE | DATE_G |
|----------|------|-----------|--------|-----------|--------|
| 134828 | 2399 | prod_name | 11.6 | 48 | 4 |
CORRECT result is:
| AUTO_INC | ID | CONC | VOLUME | SUB_PRICE | DATE_G |
|----------|------|-----------|--------|-----------|--------|
| 134828 | 2399 | prod_name | 11.6 | 48 | 4 |
|----------|------|-----------|--------|-----------|--------|
| 133387 | 2399 | prod_name | 13 | 28.750412 | 4 |
Does anybody have any ideas?
sqlfiddle.com
The problem is the join on the calculated decimal value. The following works:
SELECT t1.auto_inc, t1.id, t1.conc, t1.volume,
(t1.price / my_currency.ex_rate) as sub_price, t1.date_g
FROM com as t1 inner join
my_currency
ON t1.country = my_currency.domain AND t1.date_g = my_currency.date_g
inner join
(select com.id, com.conc, com.volume, min(com.price / my_currency.ex_rate) as usd_price, com.date_g
from com inner join
my_currency
ON com.country = my_currency.domain AND
com.date_g = my_currency.date_g
WHERE com.id=2399 AND com.date_g = 4 AND
com.country in ('us', 'uk')
GROUP BY com.id, com.conc, com.volume
) as t2
on t1.id = t2.id and
t1.conc = t2.conc and
t1.volume = t2.volume and
abs((t1.price / my_currency.ex_rate) - t2.usd_price) < 0.01 and
t1.date_g = t2.date_g
ORDER BY conc DESC, volume DESC;
However, if you change the join condition on price to:
t1.price / my_currency.ex_rate = t2.usd_price and
Then it doesn't work.
It does work if you cast both results back to decimal(10, 2):
cast(t1.price / my_currency.ex_rate as decimal(10, 2)) = cast(t2.usd_price as decimal(10, 2))
It might have something to do with this note in the documentation:
In division performed with /, the scale of the result when using two
exact-value operands is the scale of the first operand plus the value
of the div_precision_increment system variable (which is 4 by
default). For example, the result of the expression 5.05 / 0.014 has a
scale of six decimal places (360.714286)