Say i have the following MySql 'orders' table:
id|car|car_qty|speakers|speakers_qty
1 | 2 | 15 | 5 | 16
2 | 2 | 19 | 5 | 40
What is the query to get the following:
product|qty
2 | 15
2 | 19
5 | 16
5 | 40
WITHOUT using UNION?
Thank you!
Try this:
SELECT car product,car_qty qty FROM orders
UNION
SELECT speakers product,speakers_qty qty FROM orders
Do a union ALL from the same table... Just need to make sure the column names and data types are the same...
select
car as product,
car_qty as qty
from
orders
union all
select
speakers as product,
speakers_qty as qty
from
orders;
Related
I have two seperate MySQL queries which return two set of results that look like this
Query 1 result:
country | buyers | payment | num_of_sales
UK | 5 | 106.45 | 4
Thailand | 6 | 250.10 | 3
and:
Query 2 result:
country | buyers | payment | num_of_sales
UK | 2 | 150.00 | 1
Norway | 9 | 310.80 | 2
All I need is to merge / union them so the final result will look like this:
Expected result
country | buyers | payment | num_of_sales
UK | 7 | 256.45 | 5
Thailand | 6 | 250.10 | 3
Norway | 9 | 310.80 | 2
Please help, and if possible, with a bit of explaination. Thank you!
Use UNION ALL to merge the two queries results' sets into one result set, then use GROUP BY country with SUM to get the totals you are looking for, something like this:
SELECT
country,
SUM(buyers) AS buyers,
SUM(payment) AS payment,
SUM(num_of_sales) AS num_of_sales
FROM
(
SELECT country, buyers, payment, num_of_sales
FROM -- your query1 results
UNION ALL
SELECT country, buyers, payment, num_of_sales
FROM -- your query2 result2
) AS t
GROUP BY country
I have numeric data and I want to show all plus average value. How should I do to make it in MySQL?
Example : (2.5 is average value)
Data
------
1
2
3
4
2.5
You can do it without union
SELECT
AVG(value)
FROM a
GROUP BY id with rollup
Output
| VALUE |
|-------|
| 1 |
| 2 |
| 3 |
| 4 |
| 2.5 |
Fiddle Demo
Try this:
SELECT Data FROM table
UNION ALL
SELECT AVG(Data) AS Data FROM table
I have 2 tables:
stock
StockID | ItemName
1 | hat
2 | hammer
3 | banana
4 | elephant
5 | book
and Basket
BasketID | StockID | Quantity
1 | 3 | 5
2 | 2 | 20
3 | 1 | 7
4 | 2 | 60
5 | 5 | 23
6 | 1 | 17
7 | 3 | 3
8 | 4 | 6
9 | 3 | 1
10 | 2 | 1
11 | 2 | 13
I'm trying to make an SQL query which out puts the StockID, ItemName, Total Quantity Sold, and the Number of Orders that Item had.
I have this:
SELECT stock.StockID, stock.ItemName, SUM( basket.Quantity ) AS QuantitySold
FROM stock
JOIN basket ON stock.StockID = basket.StockID
GROUP BY stock.Itemname
ORDER BY stock.StockID
LIMIT 0 , 30
Which works fine, but when I try adding:
COUNT (DISTINCT basket.BasketID)
I just get a message saying I have a Syntax Error.
I am fairly new to all this, so sorry if my logic is wrong, but shouldn't that just count the distinct values tied to stockID, as it does pretty much that with the SUM of quantity sold, where it locates all the basket.Quantity values tied to the stockID in the basket table.
All help much appreciated -Tom
Not sure if this is the full answer to your question, but I don't think that in MySQL you can have a space between the function name and the leading parenthesis like you do with COUNT.
I have a report made in report builder 3 that will find order lines based off of an order number. I am trying to have the row repeat based on the quantity value of that row like this:
Part Qty
001 5
002 2
I am trying to get it to repeat the 001 row 5 times and the 002 row 2 times.
(This is for printing off labels)
I cannot create or update tables in the database.
Thanks
This can be addressed in the underlying SQL statement. See this answer.
The basic idea is to create a view with a list of numbers (N). Then join to that view with N <= Qty:
CREATE VIEW [dbo].[vwNumbers]
AS
SELECT N=1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5
UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9 UNION ALL SELECT 10
UNION ALL SELECT 11 UNION ALL SELECT 12 UNION ALL SELECT 13 UNION ALL SELECT 14 UNION ALL SELECT 15
UNION ALL SELECT 26 UNION ALL SELECT 27 UNION ALL SELECT 28 UNION ALL SELECT 29 UNION ALL SELECT 20
UNION ALL SELECT 21 UNION ALL SELECT 22 UNION ALL SELECT 23 UNION ALL SELECT 24 UNION ALL SELECT 25
UNION ALL SELECT 26 UNION ALL SELECT 27 UNION ALL SELECT 28 UNION ALL SELECT 29 UNION ALL SELECT 30
GO
SELECT Part
FROM PartList
INNER JOIN vwNumbers ON N <= Qty
I have the same problem and after searching a lot I could do it this way, I have to repeat and display a product label based on a number, for example in my table label that I have:
ItemId | Barcode | CustName | LabelQty
001 | 123abc | Jhon | 3
So in this case I need to repeat the same label 3 times.
I achieved this by grouping the table by a value that count the number of times to be repeated.
ItemId | Barcode | CustName | LabelQty | Counter
001 | 123abc | Jhon | 3 | 1
001 | 123abc | Jhon | 3 | 2
001 | 123abc | Jhon | 3 | 3
In my case the 3 labels are the same so I fill only the first row with label data in order to reduce the data to process, like this:
ItemId | Barcode | CustName | LabelQty | Counter
001 | 123abc | Jhon | 3 | 1
null | null | null | null | 2
null | null | null | null | 3
in the field expresion I use =first(field.value, 'datasource') to find the row with data.
if you have to repeat different values I suppose that you have to fill all rows.
hope this helps.
sorry for bad english.
I need to calculate the sum of stock from some rows, the same product exist for different companies, so I need to calculate the SUM/Total of all products, per product name, this is the result that I want to achieve:
ID | Name | Company | Stock | Total Stock
1 | Product1 | Company1| 10 | 30
2 | Product1 | Company2| 10 | 30
3 | Product1 | Company2| 10 | 30
4 | Product2 | Company1| 10 | 15
5 | Product2 | Company2| 5 | 15
6 | Product3 | Company2| 5 | 5
7 | Product4 | Company1| 6 | 10
8 | Product4 | Company2| 4 | 10
Etc...
The Total Stock is the sum of Select SUM(Stock) From Product Where Name="Product1"
How can I save this result ? Should I use a Trigger Or a Procedure? I have basic knowledge of SQL
This needs to run every 30 minutes or every time a row is updated.
The product table has around 40.000 Products.
SELECT a.*, b.totalStock
FROM tableName a
INNER JOIN
(
SELECT Name, SUM(Stock) totalStock
FROM tableName
GROUP BY Name
) b on a.name = b.name
-- WHERE a.Name = 'Product1'
SQLFiddle Demo
PS: you need to use ID instead of Name.