Two different columns need criteria with SUM condition [closed] - mysql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a table with two columns in a mysql database.
For example: Item_name, qty in table1
I need to retrieve all items by Item_name where sum(qty) <> 0
I tried the following but it didn't work
select item_name from table1 where SUM(QTY) <> 0 group by item_name
Thank you.

When you want to add a condition on an aggregate, HAVING is used rather than WHERE:
SELECT item_name
FROM table1
GROUP BY item_name
HAVING SUM(QTY) <> 0

Related

SELECT row COUNT and another row value MYSQL [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I need to get city and model + order by common model value.
Without city mysql query is preparing successfully but with city is not working.
How can i get two columns info in one query?
SELECT model
FROM cars
GROUP BY model
ORDER BY COUNT(model) DESC
LIMIT 6
not working: SELECT cars,model FROM cars GROUP BY model ORDER BY COUNT(model) DESC LIMIT 6
If you want the model/city combinations with the most rows, then include both in the select and group by:
SELECT model, city
FROM cars
GROUP BY model, city
ORDER BY COUNT(*) DESC
LIMIT 6

Updating a column with a Count of data from another column in the same table? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
As the title suggests, trying to take the count or possibly distinct count of a column, we call it year, to count the number of a years for an individual or populated ID and place it another column in the same table. Here is an idea of what I have so far and we want to update the table. Thank you.
Join group table to updated table directly:
UPDATE outputtable O JOIN
(SELECT personID, COUNT(DISTINCT year) AS countYear
FROM outputtable GROUP BY personID) temp ON O.personID=temp.personID
SET O.N=temp.countYear

Joining on BETWEEN values [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
How do I do lookup or joining two tables based on the values of date?
In the first table I have Item_ID and Entry_Date as columns.
On the second one I have Shift_ID, Personnel, Begin_Date, and End_Date as columns.
I want to create a result that displays Item_ID and Personnel, where Personnel and Shift is determined by whether or not an Item's Entry_Date is between a shift's Begin_Date and End_Date.
I'm sorry for utilizing image, I meant to write the table within this post itself but I don't know how yet.
Try a basic join:
SELECT
i.Item_ID,
COALESCE(s.Personnel, 'NA') AS Personnel
FROM Item i
LEFT JOIN Shift s
ON i.Entry_Date BETWEEN s.Begin_Date AND s.End_Date;

How to group duplicate values into separate groups? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Data
Needed Result
Now I have to query how to get count (id) and group by the single field. This means that when I get the result, it will be as shown in image 2. When I use this query, it will only correct the first when line, and the next when it will return the missing value due to the above condition. Hope you can help. Thanks.
select count(id)
from table
GROUP BY (CASE
WHEN `fields` like '%-31-%' THEN '31'
WHEN `fields` like '%-35-%' THEN '35'
WHEN `fields` like '%-49-%' THEN '49'
ELSE '52' END)
select patterns.groupnum, count(table.id)
from table, ( SELECT '%-31-%' pattern, 31 groupnum UNION ALL
SELECT '%-35-%' , 35 UNION ALL
SELECT '%-49-%' , 49 UNION ALL
SELECT '%-52-%' , 52 ) patterns
WHERE table.`field` LIKE patterns.pattern
GROUP BY patterns.groupnum;
But I'd recommend you to normalize your data.

How to calculating SUM of (quantity*price) from two different tables? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have this two following tables:
Order Table
Product Table
I'm trying to calculate the subtotal price for each product (quantity*price) then SUM the TOTAL value for the entire order.
Thanks, Any help would be very appreciated.
Check if table names are correct and try this:
SELECT o.order_number, o.item_quantity, p.price,(o.item_quantity * p.price) AS subtotal, SUM( o.item_quantity * p.price) AS total
FROM order AS o
LEFT JOIN product AS p ON product_number = idproduct_detail;
With this I am connecting each order with the relative product then selecting item_quantity and price and SUM the product of them.