I have 2 Tables with following data:
TABLE 1 (dummy_daily)
Entry storenum busidate daily_budget
1 1 2017-07-01 4000
2 1 2017-07-02 3500
3 1 2017-07-03 2000
4 1 2017-07-04 6000
5 1 2017-07-05 1500
TABLE 2 (site_labour)
Lab_id storenum busidate lab_hour
1123 1 2017-07-01 128
1124 1 2017-07-02 103
1125 1 2017-07-03 114
1126 1 2017-07-04 108
1127 1 2017-07-05 118
This is my current query to combine the 2 tables that have the same date to give result of daily_budget and lab_hour
QUERY:
SELECT
a.daily_budget as Ideal, c.lab_hour as Actual,
b.store_name, b.storenum,a.busidate
FROM dummy_daily a JOIN site_store b ON b.storenum=a.storenum JOIN
site_labour c ON b.storenum=c.storenum
WHERE b.storenum='1' AND
(CASE WHEN c.busidate BETWEEN '2017-07-01' AND '2017-07-05' THEN c.lab_hour ELSE 0 END)
AND (CASE WHEN a.busidate
BETWEEN '2017-07-01' AND '2017-07-05' THEN a.daily_budget ELSE 0 END)
But my current query give me a wrong result :
Wrong Result of Current Query
Ideal Actual storenum busidate
4000 128 1 2017-07-01
3500 128 1 2017-07-02
2000 128 1 2017-07-03
6000 128 1 2017-07-04
1500 103 1 2017-07-05
4000 103 1 2017-07-01
3500 103 1 2017-07-02
2000 103 1 2017-07-03
6000 103 1 2017-07-04
1500 103 1 2017-07-05
This data will continue until end of Actual 118
Expected Result
Ideal Actual storenum busidate
4000 128 1 2017-07-01
3500 103 1 2017-07-02
2000 114 1 2017-07-03
6000 108 1 2017-07-04
1500 118 1 2017-07-05
You have missed one more table so its make confusion to create logic,
As per my understanding, I have created a SELECT statement. Please try this:-
SELECT
dummy_daily.daily_budget as Ideal, site_labour.lab_hour as Actual,
site_store.store_name, site_store.storenum, dummy_daily.busidate
FROM dummy_daily
JOIN site_store
ON dummy_daily.storenum = site_store.storenum
JOIN site_labour
ON dummy_daily.storenum = site_labour.storenum
WHERE (dummy_daily.storenum = 1)
AND (dummy_daily.busidate BETWEEN '2017-07-01' AND '2017-07-05')
AND (site_labour.busidate BETWEEN '2017-07-01' AND '2017-07-05')
AND (dummy_daily.busidate = site_labour.busidate);
Let's try the SQL below:
SELECT
a.daily_budget as Ideal, c.lab_hour as Actual,
b.store_name, b.storenum,a.busidate
FROM dummy_daily a JOIN site_store b ON b.storenum=a.storenum AND b.busidate=a.busidate JOIN
site_labour c ON b.storenum=c.storenum AND b.busidate=c.busidate
WHERE b.storenum='1' AND
(CASE WHEN c.busidate BETWEEN '2017-07-01' AND '2017-07-05' THEN c.lab_hour ELSE 0 END)
AND (CASE WHEN a.busidate
BETWEEN '2017-07-01' AND '2017-07-05' THEN a.daily_budget ELSE 0 END)
Related
What I'm trying to do is to select a specific amount of tickets (max 2) and every person that has the sum of the number of tickets less of 3 and the valid field has to be != 'e'
I have this table:
ID
id_person
nr_tickets
valid
1
220
1
s
2
220
1
s
3
330
2
s
4
330
1
e
5
331
1
s
6
220
2
s
7
441
1
s
8
442
2
s
9
443
1
s
10
444
1
s
11
445
2
s
Here is what I did:
SELECT m.id, m.id_person, m.nr_tickets, m.valid
FROM table m
JOIN table m1 ON m1.id <= m.id
WHERE m.nr_tickets > 0
GROUP BY m.id
HAVING SUM(case when m.valid != 'e' then m1.nr_tickets end) <= 10
This query gives me
ID
id_person
nr_tickets
valid
1
220
1
s
2
220
1
s
3
330
2
s
5
331
1
s
6
220
2
s
7
441
1
s
8
442
2
s
As you can see the query it's almost right, the thing is that the person 220 in the results has the sum of the tickets is greater than 2.
What I'm trying to achieve is to bypass the ID 6, and to have instead the ID 9
select `id`,`id_person`, sum(`nr_tickets`) as `nr_tickets`, `valid`
from `test`
group by `id_person`
having sum(`nr_tickets`) < 3 and `valid`!="e"
Output:
id id_person nr_tickets valid
5 331 1 s
7 441 1 s
8 442 2 s
9 443 1 s
10 444 1 s
11 445 2 s
I have some values which is based on date for different groupings
For each grouping id, what is the max of value till a particular date.
Example
Date Grouping Id Value
2017-06-01 1 100
2017-06-03 1 101
2017-06-06 1 103
2017-06-02 2 200
2017-06-04 2 210
2017-06-08 2 220
2017-06-01 2 1000
2017-06-05 2 1020
2017-06-09 2 1050
I need the output like below
Date Grouping Id Value
2017-06-01 1 100
2017-06-02 1 100
2017-06-03 1 101
2017-06-04 1 101
2017-06-05 1 101
2017-06-06 1 103
2017-06-07 1 103
2017-06-08 1 103
2017-06-09 1 103
2017-06-10 1 103
2017-06-01 2 NULL
2017-06-02 2 200
2017-06-03 2 200
2017-06-04 2 210
2017-06-05 2 210
2017-06-06 2 210
2017-06-07 2 210
2017-06-08 2 220
2017-06-09 2 220
2017-06-10 2 220
2017-06-01 3 1000
2017-06-02 3 1000
2017-06-03 3 1000
2017-06-04 3 1000
2017-06-05 3 1020
2017-06-06 3 1020
2017-06-07 3 1020
2017-06-08 3 1020
2017-06-09 3 1050
2017-06-10 3 1050
So assuming you have the TEMPLATE_TABLE that has all days x all grouping IDs data, you can try below two queries:
CREATE TABLE TABLE_1 AS
SELECT A.DATE, A.GROUPING_ID, B.VALUE
FROM
TEMPLATE_TABLE A
LEFT JOIN
GIVEN_TABLE B
ON A.DATE = B.DATE AND A.GROUPING_ID = B.GROUPING_ID
ORDER BY A.GROUPING_ID, A.DATE;
Above table TABLE_1 will pad values for matches and NULL for non-matches.
You can use this table to find maximum values till a given date for all dates through a self join given in the below query:
SELECT B.DATE, B.GROUPING_ID, MAX(A.VALUE) AS VALUE
FROM
TABLE_1 A
INNER JOIN
TABLE_1 B
ON A.GROUPING_ID = B.GROUPING_ID AND A.DATE<=B.DATE
GROUP BY B.DATE, B.GROUPING_ID;
Hope this works. Let me know if this doesn't.
I have a MySQL table with the following table structure and desired output
historical_id grd_id register_type timestamp address value historical_type insertion_time
5358 2 11 2016-05-07 12:45:00 1 18.1 1 2016-05-07 13:44:58
5359 2 11 2016-05-07 12:45:00 2 51.4 1 2016-05-07 13:44:58
5360 2 11 2016-05-07 12:45:00 3 476 1 2016-05-07 13:44:58
5364 2 11 2016-05-07 13:00:00 1 18.79 1 2016-05-07 13:59:58
5365 2 11 2016-05-07 13:00:00 2 51.2 1 2016-05-07 13:59:58
5366 2 11 2016-05-07 13:00:00 3 718 1 2016-05-07 13:59:58
Desired output from query
kWh_date temp rh co2
2016-05-0712:45:00 18.1 51.4 476
2016-05-0713:00:00 18.79 51.2 718
Can anybody help as to how I get the desired output. I have tried to 'GROUP' by timestamp (not my decision to use this name and cannot change it) but it just shows me 1 set of data (ie 'address' 1). I have used 3 'SELECT' statements but that shows the timestamp repeated. Any help is much appreciated.
I have tried
SELECT `register_type`
, `timestamp`
, `address`
, `value`
FROM grdxf.historical
WHERE `register_type` = 11
GROUP
BY `timestamp`;
I have tried different queries but they end up failing also. I'm really at a lose and new to queries.
E.g.:
SELECT FROM_UNIXTIME(900 * FLOOR(UNIX_TIMESTAMP(timestamp)/900)) timestamp
, MAX(CASE WHEN address = 1 THEN value END) temp
, MAX(CASE WHEN address = 2 THEN value END) rh
, MAX(CASE WHEN address = 3 THEN value END) co2
FROM historical
WHERE register_type = 11
GROUP
BY timestamp;
I have the following table in mysql:
Key DI CI FD FA NM Valid_from Valid_to
0 1224468 123 2012-06-30 3 6 2013-01-23 9999-12-31
1 1234567 123 2013-12-31 3 10 2014-02-27 2014-03-10
2 1234567 123 2013-12-31 2 12 2014-03-10 9999-12-31
3 1234579 123 2013-12-31 3 12 2014-05-15 9999-12-31
4 1234595 123 2013-12-31 1 12 2014-06-30 9999-12-31
5 122469 123 2015-11-11 1 6 2015-11-11 9999-12-31
6 1224470 123 2015-11-11 2 12 2015-11-11 9999-12-31
7 1224471 123 2015-11-11 3 15 2015-11-11 9999-12-31
8 1224472 123 2015-11-10 2 13 2015-11-10 9999-12-31
9 1224473 123 2015-11-10 3 12 2015-11-10 9999-12-31
If there are records which has the same "FD", I need to get the ones which 's "FA" is "1", if exists.
Basically, I want this output.
Key DI CI FD FA NM Valid_from Valid_to
0 1224468 123 2012-06-30 3 6 2013-01-23 9999-12-31
4 1234595 123 2013-12-31 1 12 2014-06-30 9999-12-31
5 122469 123 2015-11-11 1 6 2015-11-11 9999-12-31
8 1224472 123 2015-11-10 2 13 2015-11-10 9999-12-31
9 1224473 123 2015-11-10 3 12 2015-11-10 9999-12-31
I have tried the following code, but it gives a weird output:
Code:
SELECT T1.*
FROM findoc T1 LEFT JOIN
findoc T2
ON DATE(T1.`Financial_date`) = DATE(T2.`Financial_date`) AND T2.`Fig_audit` <> 1
WHERE T2.`Fig_audit` IS NULL OR T1.`Fig_audit` = 1
Output:
Key DI CI FD FA NM Valid_from Valid_to
4 1234595 123 2013-12-31 1 12 2014-06-30 9999-12-31
4 1234595 123 2013-12-31 1 12 2014-06-30 9999-12-31
4 1234595 123 2013-12-31 1 12 2014-06-30 9999-12-31
5 122469 123 2015-11-11 1 6 2015-11-11 9999-12-31
5 122469 123 2015-11-11 1 6 2015-11-11 9999-12-31
It looks a complicated query, and I couldn't manage to do it.
How can I do it?
Thanks.
you can do case based aggregation to find out if there exists row with same date and with atleast one row with column value for FA as 1
select F.* from finddoc F
inner join
(
select fd , sum( case when fa = 1 then 1 else 0 end) as faOneCount
from finddoc
group by fd
) T
on (F.FD = T.FD and T.faOneCount = 1 AND F.FA =1)
or ( F.FD = T.FD and T.faOneCount =0 )
If I understand your question correctly (and I'm not sure I do), you could try something like this:
SELECT T1.*
FROM findoc T1
where T1.NM in (select distinct NM from findoc where FA = 1);
I'm assuming that NM is the common field for which you want to return all results if the FA for any of those NM entries is 1. But that could be a wrong assumption.
First off, for categorization purposes, could someone figure out a better name to index this as?
Secondly, I have a table and I want to grab the username from another table to two columns on the same table.
SELECT ignite_board.*,username AS 'name'
FROM ignite_board,user_info
WHERE bnumber = 3
AND ignite_board.uid = user_info.id
ORDER BY POSITION
Gets me this:
ID bnumber position UID qual time following name
1176 3 1 442 2 1382045726 440 bb1
1177 3 2 445 2 1382045936 442 bb4
1181 3 3 450 2 1382050220 449 bb6
1178 3 4 446 2 1382050371 439 aa2
1209 3 5 466 1 1382050130 450 bb8
1212 3 6 469 2 1382050502 467 bb10
1210 3 7 467 1 1382050172 466 bb9
1232 3 8 475 1 1382050415 446 aa7
1180 3 9 444 0 1382045690 445 bb3
1214 3 10 471 0 1382050220 466 bb11
1233 3 11 476 0 1382050415 475 aa8
1179 3 12 441 0 1382045562 475 aa1
1182 3 13 452 0 1382046032 448 aa6
1216 3 14 473 0 1382050272 469 bb12
1234 3 15 477 0 1382050502 469 bb16
1271 3 16 454 0 1382306814 442 bb7
But what would I need to get the username and the Following username (sorta like this)
SELECT ignite_board.*,username AS 'name',username AS 'following'
FROM ignite_board,user_info
WHERE bnumber = 3
AND ignite_board.uid = user_info.id
AND ignite_board.following = user_info.id
ORDER BY POSITION
SELECT b.*
, u.username name
, f.username following
FROM ignite_board b
JOIN user_info u
ON u.id = b.uid
JOIN user_info f
ON f.id = b.following
WHERE bnumber = 3
ORDER
BY position;
You have to join the user_info table twice, and give a different alias to each instance:
SELECT ignite_board.*,u.username AS 'name',f.username AS 'following'
FROM ignite_board,user_info u, user_info f
WHERE bnumber = 3
AND ignite_board.uid = u.id
AND ignite_board.following = f.id
ORDER BY POSITION