I have a MySQL table that tracks certain totals by both hour of the day and various locations. I am trying to create a query that will total not only each column, but also each row. The query I have so far totals each column, but I can't figure out how to get a total for each row as well.
This is my query:
SELECT * FROM
(SELECT IFNULL(hour,"Total") as hour, SUM(location1), SUM(location2), SUM(location3), SUM(location4), SUM(location), FROM counts WHERE ay = 'AY1617' GROUP BY hour WITH ROLLUP) as crossdata
ORDER BY FIELD (hour,'8:00am','9:00am','10:00am','11:00am','12:00pm','1:00pm','2:00pm','3:00pm','4:00pm','5:00pm','6:00pm','7:00pm','8:00pm','9:00pm','10:00pm','11:00pm')
This is ultimately what I want the output to look like:
hour location1 location2 location3 location4 totals
8am 4 3 2 1 10
9am 1 2 2 1 6
10am 2 3 2 3 10
totals 7 8 6 5 26
How can I achieve this?
For what it's worth, this is not a crosstab query. You aren't pivoting rows to columns.
I tried this query and got the result you want:
SELECT IFNULL(hour, 'Total') AS hour,
SUM(location1) AS location1,
SUM(location2) AS location2,
SUM(location3) AS location3,
SUM(location4) AS location4,
SUM(location1)+SUM(location2)+SUM(location3)+SUM(location4) AS totals
FROM counts
WHERE ay = 'AY1617'
GROUP BY hour WITH ROLLUP;
You should really use the TIME data type instead of strings for the hour. Then it just sorts correctly.
+----------+-----------+-----------+-----------+-----------+--------+
| hourt | location1 | location2 | location3 | location4 | totals |
+----------+-----------+-----------+-----------+-----------+--------+
| 08:00:00 | 4 | 3 | 2 | 1 | 10 |
| 09:00:00 | 1 | 2 | 2 | 1 | 6 |
| 10:00:00 | 2 | 3 | 2 | 3 | 10 |
| Total | 7 | 8 | 6 | 5 | 26 |
+----------+-----------+-----------+-----------+-----------+--------+
Related
I have a MYSQL table where I need to get to subtract values from 2 different rows.
This is my DB table:
Tablename: ext_partnertotals
| Partner_ID | Partnername | Month | Year | Total |
|------------|-------------|-------|------|-------|
| 1 | Partner 1 | 1 | 2018 | 10 |
| 1 | Partner 1 | 2 | 2018 | 12 |
| 2 | Partner 2 | 1 | 2018 | 18 |
| 2 | Partner 2 | 2 | 2018 | 12 |
It should get this with a query:
| Partner_ID | Partnername | up/down |
|------------|-------------|---------|
| 1 | Partner 1 | +2 |
| 2 | Partner 2 | -6 |
I need to get the Subtract value of 2 different months for each Partner.
Every Partner has a tablerow for each month and a value for that month.
Now I need to get If they went up or went down in value since the month before.
Can someone write me a query?
Since you're unable to improve your terrible schema, I recommend you use a (very ugly/hard to maintain and very slow) correlated subquery:
SELECT Partner_ID, Partnername, Year, Month, Total - (
SELECT Total
FROM ext_partnertotals AS prev
WHERE prev.Partner_ID = cur.Partner_ID AND CASE cur.Month
WHEN 1 THEN prev.Year = cur.Year - 1 AND prev.Month = 12
ELSE prev.Year = cur.Year AND prev.Month = cur.Month - 1
END
) AS `up/down` FROM ext_partnertotals AS cur
See it on sqlfiddle.
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 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.
So i have a table called Ronde:
ID | Teamid | Timestamp
----------------------------------------
1 | 1 | 2013-06-28 18:35:28
2 | 1 | 2013-06-28 18:36:28
3 | 2 | 2013-06-28 18:36:30
4 | 3 | 2013-06-28 18:37:28
5 | 2 | 2013-06-28 18:40:28
6 | 1 | 2013-06-28 18:42:28
7 | 2 | 2013-06-28 18:43:28
8 | 3 | 2013-06-28 18:48:28
Here's a sqlfiddle of same.
So what i need is a query who takes the 2 newest records grouped by Teamid and do a math function with timestamp.
So example:
newest1, newest2
result = ( 60 minutes / (newest2.timestamp - newest1.timestamp) ) * 6
The result is the avg speed between 2 timestamps.
`LIMIT` in subquery don't work
Someone have a solution for my problem ???
Desired output data :
Teamid | Speed
1 | 60
2 | 120
3 | 32,72
Maybe something like this....
http://www.sqlfiddle.com/#!2/2dc3f/7
?
How can I get the number of records over a date interval, including dates with no records?
For example I have the following table:
DATE | INSERTID
2011-12-10 | 1
2011-12-10 | 2
2011-12-12 | 3
2011-12-13 | 4
2011-12-15 | 5
2011-12-15 | 6
and the result to be:
DATE | COUNT(INSERTID)
2011-12-10 | 2
2011-12-11 | 0
2011-12-12 | 1
2011-12-13 | 1
2011-12-14 | 0
2011-12-15 | 2
I believe you're going to want to start by generating your list of days, then left join to your object table, grouping on date and doing a count() on the object table. There's already an answer that covers generating the dates, from the looks of it.