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.
Related
I am trying to derive a MySQL query that turns this:
product | sold
milk | 6
milk | 4
bread | 3
bread | 2
bread | 2
to this;
product | sold | total order | Total sold
milk | 2 | 2 | 6
milk | 4 | 2 | 6
bread | 3 | 3 | 7
bread | 2 | 3 | 7
bread | 2 | 3 | 7
I've been able to get the queries for sums and counts no problem, but I cannot seem to get it to join as a new column matching the product. Is this even possible? I've tried WITH ROLLUP but it just creates another row, not exactly what I want.
You could join a simple query on this table with an aggregate query on:
SELECT a.product, a.sold, b.total_order, b.total_sold
FROM mytable a
JOIN (SELECT product, COUNT(*) AS total_order, SUM(sold) AS total_sold
FROM mytable
GROUP BY product) b ON a.product = b.product
I have a MySQL table like this:
acco_id | room_id | arrival | amount | persons | available
1 | 1 | 2015-19-12 | 3 | 4 | 1
1 | 2 | 2015-19-12 | 1 | 10 | 1
1 | 1 | 2015-26-12 | 4 | 4 | 1
1 | 2 | 2015-26-12 | 2 | 10 | 1
2 | 3 | 2015-19-12 | 2 | 6 | 0
2 | 4 | 2015-19-12 | 1 | 4 | 1
What im trying to achieve is a single query with a result like:
acco_id | max_persons_available
1 | 22
2 | 4
I tried using a GROUP BY accommodation_id using a query like:
SELECT
accommodation_id,
SUM(amount * persons) as max_persons_available
FROM
availabilities
WHERE
available = 1
GROUP BY
accommodation_id
Only now the result of acco_id uses all arrival dates. When I add arrival to the query no more unique acco_id's.
Does anyone know a good Single SQL which can use the table indexes?
If I'm understanding the question correct (the last part is a bit confusing). You want to have the accomodation id and numbers as you have now but limited to specific arrival dates.
If so the following statement should do exactly that as it is not necessary to put arrival into the select if you "just" use it in the where statement. As else you would need to put it into the group by and thus have non unique accomodation id's.
SELECT
accommodation_id,
SUM(amount * persons) as max_persons_available
FROM
availabilities
WHERE
available = 1 and arrival >= '2015-12-19' and arrival < '2015-10-26'
GROUP BY
accommodation_id
I guess (reading your question) what you are looking for is this but im not sure as your question is a bit unclear:
SELECT
accommodation_id,
arrival,
SUM(amount * persons) as max_persons_available
FROM
availabilities
WHERE
available = 1
GROUP BY
accommodation_id, arrival
I have a web app in .Net with Reportviewer and I use as sourcedata of reportviewer MySql query, I need to make a global sales report I have this Table sales:
|IdSale|Number|Date|Price| etc...
(The important part of my problem is the next, i need to calculate the sum of price for each row with the same Number. Why? Well the Number datafield is the receipt or bill, so I can have one sale for the receipt / bill number 6 and i selled 2 items:
|IdSale|Number| Date |Price|
| 1 | 6 |20/08 | 50€|
| 2 | 6 |20/08 | 10€|
----------------------------
So i need to get the sum of that but, with the others Numbers i mean i can do that with this query:
Select distinct sales.Number, sales.Date, SUM(sales.Price) as 'Importe',
from sales where number = 6;*
and return
|Number|Date|Importe(price)|
| 6 |20/8| 100€ |
That's ok but when i have this
|IdSale|Number| Date |Price|
| 1 | 6 |20/08 | 50€|
| 2 | 6 |20/08 | 10€|
| 3 | 7 |20/08 | 30€|
| 4 | 8 |20/08 | 20€|
----------------------------
I neet to get this output
|Number|Date|Importe(price)|
| 6 |20/8| 100€ |
| 7 |20/8| 30€ |
| 8 |20/8| 20€ |
But the only thing i made is using this query
Select distinct sales.Number, sales.Date, SUM(sales.Price) as 'Importe',
from sales where number >= 0;
|Number|Date|Importe(price|
| 2 |20/8| 150€ |
So im getting the sum of all but i just need the sum of each row with the same Number can i do this?
Are you just looking for group by?
Select s.Number, s.Date, SUM(s.Price) as Importe,
from sales s
group by s.Number, s.Date;
I have a table INVENTORY which consists of inventory items. I have the following table structure:
INSTALLATION_ID
COMPONENT_ID
HISTORY_ID
ON_STOCK
LAST_CHANGE
I need to obtain the row with the max HISTORY ID for records for which the spcified LAST_CHANGE month doesn't exist.
Each COMPONENT_ID and INSTALLATION_ID can occur multiple times, they are distinguished by their respective HISTORY_ID
Example:
I have the following records
COMPONENT_ID | INSTALLATION_ID | HISTORY_ID | LAST_CHANGE
1 | 100 | 1 | 2013-01-02
1 | 100 | 2 | 2013-02-01
1 | 100 | 3 | 2013-04-09
2 | 100 | 1 | 2013-02-22
2 | 100 | 2 | 2013-03-12
2 | 100 | 3 | 2013-07-07
2 | 100 | 4 | 2013-08-11
2 | 100 | 5 | 2013-09-15
2 | 100 | 6 | 2013-09-29
3 | 100 | 1 | 2013-02-14
3 | 100 | 2 | 2013-09-23
4 | 100 | 1 | 2013-04-17
I am now trying to retrieve the rows with the max HISTORY ID for each component but ONLY for COMPONENT_IDs in which the specifiec month does not exists
I have tried the following:
SELECT
INVENTORY.COMPONENT_ID,
INVENTORY.HISTORY_ID
FROM INVENTORY
WHERE INVENTORY.HISTORY_ID = (SELECT
MAX(t2.HISTORY_ID)
FROM INVENTORY t2
WHERE NOT EXISTS
(
SELECT *
FROM INVENTORY t3
WHERE MONTH(t3.LAST_CHANGE) = 9
AND YEAR(t3.LAST_CHANGE)= 2013
AND t3.HISTORY_ID = t2.HISTORY_ID
)
)
AND INVENTORY.INSTALLATION_ID = 200
AND YEAR(INVENTORY.LAST_CHANGE) = 2013
The query seems to have correct syntax but it times out.
In this particular case, i would like to retrieve the maximum HISTORY_ID for all components except for those that have records in September.
Because I need to completely exclude rows by their month, i cannot use NOT IN, since they will just suppress the records for september but the same component could show up with another month.
Could anybody give some pointers? Thanks a lot.
If I understand correctly what you want you can do it like this
SELECT component_id, MAX(history_id) history_id
FROM inventory
WHERE last_change BETWEEN '2013-01-01' AND '2013-12-31'
AND installation_id = 100
GROUP BY component_id
HAVING MAX(MONTH(last_change) = 9) = 0
Output:
| COMPONENT_ID | HISTORY_ID |
|--------------|------------|
| 1 | 3 |
| 4 | 1 |
If you always filter by installation_id and a year of last_change make sure that you have a compound index on (installation_id, last_change)
ALTER TABLE inventory ADD INDEX (installation_id, last_change);
Here is SQLFiddle demo
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.