SL is the primary key.
Table name -> impo
I want to sum all the QTY which have same Model number.(Here add 40+35 because of INDO666)
I tried this:
SELECT i.Model_num, i.QTY
FROM impo AS i
JOIN (SELECT Model_num, SUM(QTY) AS t_qty
FROM impo
GROUP BY Model_num )
AS s ON i.Model_num = s.Model_num
I don't understand why you have an extra join. The subquery seems to do what you want:
SELECT Model_num, SUM(QTY) AS t_qty
FROM impo
GROUP BY Model_num ;
Related
How to count value from two tables and return the result in a single value.?
I had two tables named order and affiliate, like below:
order:
order_ID(primary key), order_name, Order_status, affiliate_ID (foreign key)
affiliate:
affiliate_ID (primary key),affiliate_name
Now, I want to count the orders based on affiliate name by comparing the affiliate_ID on order table and affiliate table.
I have tried it like this:
Query
SELECT COUNT(*)
FROM order o
AND (SELECT COUNT (affiliate_name) FROM affiliate a
WHERE a.affiliate_id = O.affilate_id) AS total
It returns an error.
I feel this is what you need
SELECT a.affiliate_ID, a.affiliate_name, COUNT(o.order_ID) as total
FROM order o
LEFT JOIN affiliate a ON o.affiliate_ID = a.affiliate_ID
GROUP BY o.affiliate_ID;
to add WHERE clause, see sample
SELECT a.affiliate_ID, a.affiliate_name, COUNT(o.order_ID) as total
FROM order o
LEFT JOIN affiliate a ON o.affiliate_ID = a.affiliate_ID
WHERE o.affiliate_ID = 3
GROUP BY o.affiliate_ID;
Not sure I understand your question correctly, but is this what you're looking for ?
SELECT *, COUNT(*) as total
FROM order o
JOIN affiliate a ON a.affiliate_id = o.affiliate_id
GROUP BY o.affiliate_id
Otherwise, provive sample & desired result example please.
Sum of a multiplication of two columns grouped by another column in an inner join of three tables returns wrong value.
Below are my three tables:
Table1:
Table2:
Table3:
My Query is as below:
SELECT c.price, c.quantity, SUM( c.quantity * c.price ) AS price,
group_concat( a.rate
SEPARATOR '<br>' ) AS rates, c.hsn AS hsn
FROM tax_wa a
INNER JOIN tax_rate_class b ON a.tax_rate_id = b.tax_rate_id
INNER JOIN inv_item c ON b.tax_class_id = c.tax_class_id
WHERE c.invoice_id = '17'
GROUP BY c.hsn
And the result is:
But above one is not correct... To expain it, if you run the below query on the inv_item table (alone, with no joins) you get correct results:
SELECT price, quantity, sum( quantity * price )
FROM `inv_item`
WHERE invoice_id = '17'
GROUP BY hsn
Result is good:
Above result the wrong value calculated
if you add all
Presumably, you want the sum():
SELECT SUM(c.price), SUM(c.quantity), SUM( c.quantity * c.price ) AS price,
group_concat( a.rate SEPARATOR '<br>' ) AS rates, c.hsn AS hsn
FROM tax_wa a INNER JOIN
tax_rate_class b
ON a.tax_rate_id = b.tax_rate_id INNER JOIN
inv_item c
ON b.tax_class_id = c.tax_class_id
WHERE c.invoice_id = 17
GROUP BY c.hsn;
Okay so I have three(3) tables that i want to join together
tableA is the main details and primary key is row_id autoincremented
tableB is the exteded details and primary/foreign key is row_id coming from tableA
tableC stores unordered ratings and comments for a particular row_id
I want to join all these tables so that I can see all details plus the number of instances in tableC for a row_id and the avg rating.
SELECT *
FROM `tableA` A
LEFT JOIN `tableB` B
ON A.`row_id` = B.`row_id`
LEFT JOIN (
SELECT COUNT( 1 ) AS 'count', Avg(`row_rating`) AS 'avg'
FROM `tableC`
GROUP BY tableC.`row_id`
)C
ON C.`row_id` = A.`row_id`
ORDER BY C.`avg` ASC
The result of this query combines all properly but the same count and avg is displayed in all rows.
Looks like you want to group the records by row_id in inner query. In which case, you need to SELECT row_id instead of COUNT(1), try this:
SELECT *
FROM `tableA` A
LEFT JOIN `tableB` B
ON A.`row_id` = B.`row_id`
LEFT JOIN (
SELECT row_id, Avg(`row_rating`) AS 'avg'
FROM `tableC`
GROUP BY tableC.`row_id`
)C
ON C.`row_id` = A.`row_id`
ORDER BY C.`avg` ASC
I need sum quantity in a different field with alias.
Image
I don't what your two table names are but this should work. You're trying to aggregate on idMaterial after joining on idMaterial.
SELECT A.idMaterial, C.nombre_material, A.sum_qty_entrada, B.sum_qty_salida, (A.sum_qty_entrada - B.sum_qty_salida) total
FROM (
SELECT idMaterial, SUM(qty) sum_qty_entrada FROM mesa_entrada
GROUP BY idMaterial
)
AS A
JOIN (
SELECT idMaterial, SUM(qty) sum_qty_salida FROM mesa_salida
GROUP BY idMaterial
) AS B ON
A.idMaterial = B.idMaterial
JOIN mesa_con_nombre_material AS C ON
A.idMaterial = C.idMaterial
i think that you need this
SELECT SUM(hr) FROM
(
Select sum(qty) as hr FROM table1
UNION ALL
Select sum(qty) as hr FROM table2
)a
I have a table:
PRICE_UPDATE
id (int 5, auto-increment, primary, unique)
part_number (varchar 10, non-null)
price (float(10,2), non-null)
Some of the part_numbers are duplicated (1 or more duplicate records). Sometimes with the same price, sometimes with different prices.
How can I delete all of the duplicate rows based on part_number, leaving either the highest price or just 1 record if the prices were all the same?
Is this even doable in straight MySQL?
DELETE t1
FROM YourTable t1, YourTable t2
WHERE t1.part_number = t2.part_number
AND (t1.price, t1.id) < (t2.price, t2.id)
From inside, to outside:
Selects the ids with the max price per part_number
Selects the max id with the max price per part_number
Deletes the ids not present in 2.
delete tablename where id not in (
(select max(id) from tablename a
inner join
( select id, max(price)
from tablename
group by part_number ) b on a.id = b.id and a.price = b.price
group by part_number))