I have the following example data:
ID | DATE | LOCATION | DATA
----|-------|----------|------
1 | MAR-1 | 1 | 100
2 | MAR-2 | 1 | 120
3 | MAR-3 | 1 | 160
4 | MAR-3 | 2 | 80
5 | MAR-4 | 1 | 170
6 | MAR-4 | 2 | 100
7 | MAR-5 | 1 | 10
8 | MAR-6 | 1 | 50
I need to subtract the previous day's data from the current day's and put the result into a "delta" column, where the result is also unique per location. In this example, the data is from a power meter, which is reset to zero periodically. So, I need to test if the "delta" result is negative, and if so, copy only the raw "data" value in the "delta" column. Based on the above example data, I would like this output:
ID | DATE | LOCATION | DATA | DELTA | COMMENT
----|-------|----------|------|-------|-------------------------------
1 | MAR-1 | 1 | 100 | NULL | NO PREVIOUS DATA EXISTS
2 | MAR-2 | 1 | 120 | 20 |
3 | MAR-3 | 1 | 160 | 40 |
4 | MAR-3 | 2 | 80 | NULL | NO PREVIOUS DATA EXISTS
5 | MAR-4 | 1 | 170 | 10 |
6 | MAR-4 | 2 | 100 | 20 |
7 | MAR-5 | 1 | 10 | 10 | DELTA WOULD BE - SO TAKE DATA
8 | MAR-6 | 1 | 50 | 40 |
I'm using MySQL v5.7.
You can use subquery to find the output like following.
SELECT id,date,location,data,
CASE
WHEN nv < 0 THEN data
ELSE nv
END AS Delta
FROM (SELECT *,
data - (SELECT data
FROM mytable t1
WHERE t1.id < t2.id
AND t1.location = t2.location
ORDER BY id DESC
limit 1)nv
FROM mytable t2) t
Online Demo
Output
| id | date | location | data | Delta |
| --- | ----- | -------- | ---- | ----- |
| 1 | MAR-1 | 1 | 100 | |
| 2 | MAR-2 | 1 | 120 | 20 |
| 3 | MAR-3 | 1 | 160 | 40 |
| 4 | MAR-3 | 2 | 80 | |
| 5 | MAR-4 | 1 | 170 | 10 |
| 6 | MAR-4 | 2 | 100 | 20 |
| 7 | MAR-5 | 1 | 10 | 10 |
| 8 | MAR-6 | 1 | 50 | 40 |
Related
I have a table where I want to group by both categories and days, however I want to organize the days into separate buckets and apparently I do not find a way to do it.
Table:
| Days | Category | Values |
| 2 | A | 20 |
| 4 | B | 50 |
| 6 | A | 100 |
| 2 | A | 70 |
| 1 | B | 220 |
| 9 | A | 130 |
| 7 | A | 45 |
| 1 | A | 90 |
| 5 | B | 280 |
| 5 | B | 10 |
| 8 | A | 70 |
| 9 | B | 50 |
| 0 | A | 120 |
| 3 | B | 115 |
| 0 | B | 25 |
| 3 | B | 10 |
| 6 | A | 55 |
The result I would like to get:
| Days | Category | Values |
| 0-4 | A | 300 |
| 0-4 | B | 420 |
| 5-9 | A | 400 |
| 5-9 | B | 340 |
Based on my current knowledge this is how far I can get:
SELECT
Days, Category, Value
FROM
Table
GROUP BY
Days,
Category
But of course I cannot create the day buckets. Can you please help me out with it?
Use the DIV operator (DIV = Integer division. Discards from the division result any fractional part to the right of the decimal point):
SELECT CONCAT(
MIN(Days DIV 5) * 5,
' – ',
(MIN(Days DIV 5) + 1) * 5 - 1
) AS Days,
Category,
SUM(`Values`) AS `Values`
FROM `Table`
GROUP BY Days DIV 5,
Category
sqlfiddle
I have a table like this and i want to output without duplication of the same user. If i use group by it shows only one record on the same column. iam also using left join for location and user name. A little help
+------+---------+----------+---------+
| user | work id | location | time |
+------+---------+----------+---------+
| 1 | 42 | 1 | 2hr |
| 1 | 42 | 1 | 10min |
| 1 | 42 | 1 | 30min |
| 2 | 42 | 1 | 4hr |
| 2 | 42 | 1 | 2.30min |
| 1 | 50 | 2 | 4min |
| 1 | 50 | 2 | 5min |
| 2 | 20 | 3 | 3hr |
| 1 | 20 | 3 | 6hr |
+------+---------+----------+---------+
Iam looking for this
+------+---------+----------+
| user | work id | location |
+------+---------+----------+
| 1 | 42 | 1 |
| 1 | 50 | 2 |
| 1 | 20 | 3 |
| 2 | 42 | 1 |
| 2 | 20 | 3 |
+------+---------+----------+
You simply need a distinct clause here -
SELECT DISTINCT user
,workid
,location
FROM YOUR_TABLE
ORDER BY user
,location
This seems like such a simple problem, but I can't find a good solution. I'm trying to select information from a slightly misformatted table. Basically, wherever sequence=0, the person_id should actually be a company_id. This company_id then applies to all the rows which have the same group_id.
Someone thought it was a good idea to format things this way instead of simply having a company_id column, but it makes trying to select by company very difficult. It would make my programming much easier to simply add this extra column, and fix the formatting.
I want to turn something like this:
+----------+------------+-----------+----------+
| group_id | date | person_id | sequence |
+----------+------------+-----------+----------+
| 1 | 2012-08-31 | 10 | 0 |
| 1 | 2012-08-31 | 11 | 1 |
| 1 | 2012-08-31 | 12 | 2 |
| 2 | 1999-04-16 | 10 | 0 |
| 2 | 1999-04-16 | 21 | 1 |
| 2 | 1999-04-16 | 22 | 2 |
| 2 | 1999-04-16 | 23 | 3 |
| 2 | 1999-04-16 | 24 | 4 |
| 3 | 2001-01-09 | 30 | 0 |
| 3 | 2001-01-09 | 31 | 1 |
| 3 | 2001-01-09 | 11 | 2 |
| 3 | 2001-01-09 | 12 | 3 |
+----------+------------+-----------+----------+
Into this:
+------------+----------+------------+-----------+----------+
| company_id | group_id | date | person_id | sequence |
+------------+----------+------------+-----------+----------+
| 10 | 1 | 2012-08-31 | 11 | 1 |
| 10 | 1 | 2012-08-31 | 12 | 2 |
| 10 | 2 | 1999-04-16 | 21 | 1 |
| 10 | 2 | 1999-04-16 | 22 | 2 |
| 10 | 2 | 1999-04-16 | 23 | 3 |
| 10 | 2 | 1999-04-16 | 24 | 4 |
| 30 | 3 | 2001-01-09 | 31 | 1 |
| 30 | 3 | 2001-01-09 | 11 | 2 |
| 30 | 3 | 2001-01-09 | 12 | 3 |
+------------+----------+------------+-----------+----------+
The only way I can think of how to achieve this is with nested SELECT statements, which are very inefficient considering I have about 100M rows. It's a one time fix though, so I don't mind letting it run overnight.
If you permanently want to change your table to include a company_id column then do this:
First alter the table and add the new column:
alter table your_table add company_id int;
Then update all rows to set the company to the person_id = 0 for the group:
UPDATE your_table a
JOIN your_table b ON a.group_id = b.group_id
SET a.company_id = b.person_id
WHERE b.sequence = 0;
And finally remove the rows with sequence = 0:
DELETE FROM your_table WHERE sequence = 0;
Sample SQL Fiddle
The end result will be:
| group_id | date | person_id | sequence | company_id |
|----------|------------|-----------|----------|------------|
| 1 | 2012-08-31 | 11 | 1 | 10 |
| 1 | 2012-08-31 | 12 | 2 | 10 |
| 2 | 1999-04-16 | 21 | 1 | 10 |
| 2 | 1999-04-16 | 22 | 2 | 10 |
| 2 | 1999-04-16 | 23 | 3 | 10 |
| 2 | 1999-04-16 | 24 | 4 | 10 |
| 3 | 2001-01-09 | 31 | 1 | 30 |
| 3 | 2001-01-09 | 11 | 2 | 30 |
| 3 | 2001-01-09 | 12 | 3 | 30 |
+--------+---------+-------+--------+
| billID | orderId | price | date |
+--------+---------+-------+--------+
| 1 | 1 | 100 | 1.3.12 |
| 2 | 1 | 230 | 1.4.12 |
| 3 | 1 | 300 | 1.5.12 |
| 4 | 2 | 1000 | 1.3.12 |
| 5 | 2 | 160 | 1.4.12 |
| 6 | 3 | 400 | 1.3.12 |
+--------+---------+-------+--------+
I want to create view that have column that sum all price have same orderID but with date earlier than rows date. Like this:
+--------+---------+-------+--------+--------------+
| billID | orderId | price | date | add-on price |
+--------+---------+-------+--------+--------------+
| | | | | |
| 1 | 1 | 100 | 1.3.12 | 100 |
| 2 | 1 | 230 | 1.4.12 | 330 |
| 3 | 1 | 300 | 1.5.12 | 630 |
| 4 | 2 | 1000 | 1.3.12 | 1000 |
| 5 | 2 | 160 | 1.4.12 | 1160 |
| 6 | 3 | 400 | 1.3.12 | 400 |
+--------+---------+-------+--------+--------------+
You can user a correlated subquery for this:
select t.*,
(select sum(t2.price)
from table t2
where t2.orderId = t.orderId and t2.date <= t.date
) as CumulativePrice
from table t;
I have a database table that records which site_id is being used for each user_id. I am getting duplicates everytime I save the settings. I want it so I can query the table for user_id = 99 and see which site_id is associated with that user_id.
SQL:
$sql = "INSERT INTO user_sites (rooftop_id, site_id, created_at, active)
VALUES ('$rooftop_id','$key', now(), '$value')
ON DUPLICATE KEY
UPDATE updated_at = IF(active=VALUES(active),updated_at,NOW()),
active = VALUES(active)
";
Current Output:
+----+---------+---------+--------+
| id | user_id | site_id | active |
+----+---------+---------+--------+
| 1 | 99 | 30 | 1 |
| 2 | 99 | 20 | 1 |
| 3 | 99 | 40 | 1 |
| 4 | 32 | 30 | 1 |
| 5 | 32 | 20 | 1 |
| 6 | 32 | 40 | 1 |
| 7 | 32 | 30 | 0 |
| 8 | 32 | 20 | 0 |
| 9 | 32 | 40 | 0 |
+----+---------+---------+--------+
Desired Output:
+----+---------+---------+--------+
| id | user_id | site_id | active |
+----+---------+---------+--------+
| 1 | 99 | 30 | 1 |
| 2 | 99 | 20 | 1 |
| 3 | 99 | 40 | 1 |
| 4 | 32 | 30 | 0 |
| 5 | 32 | 20 | 0 |
| 6 | 32 | 40 | 0 |
+----+---------+---------+--------+
Have you defined a unique key on the table? For what you want, I think it is:
create unique index admin_sites_userid_siteid on admin_sites(user_id, site_id);
This is then used to define the when an insert matches an existing row.