my record:
SF_ID CardID Status Received_Date Meetup_Date
12 1 Yes 2015-01-12 2015-12-03
13 1 No 2015-12-01 NULL
14 1 No 2015-12-01 NULL
15 2 No 2015-12-02 NULL
16 2 No 2015-12-02 NULL
17 3 No 2015-12-01 NULL
18 4 No 2015-12-06 NULL
19 5 Yes 2015-11-30 2015-12-01
20 5 No 2015-11-30 NULL
22 5 No 2015-11-30 NULL
23 7 yes 2015-12-06 2015-12-07
Requirement #1:
all cardID where Received_Date is Minimum and Status
is No and Top 01 SF_ID. I've tried it in the following way without success:
SELECT CardSFID,
CardID,
CardSFShortDate
FROM CC_Shortfall AS [data]
WHERE ( CardSFShortDate = (SELECT TOP 1 PERCENT CardSFShortDate
FROM CC_Shortfall
WHERE CardID = [data].CardID) )
AND CardSFYesNo = 'No'
Requirement #2:
all cardID Where MeetUp_Date is Maximum and Status
is Yes and there are not any No Status under CardID
i've tried to do it in the following way without succcess:
SELECT CardSFID,
CardID,
CardSfShortRcvDate
FROM CC_Shortfall AS [data]
WHERE ( CardSfShortRcvDate = (SELECT Max(CardSfShortRcvDate)
FROM CC_Shortfall
WHERE CardID = [data].CardID) )
AND CardSFYesNo = 'Yes'
Related
In MariaDb 10.3, how to find the latest(based on timestamp) row for each window(or partition, I am not entirely clear on the terminology here)?
Consider the following table with data
ItemID
Itemname
Value
Timestamp
1
A
22
2021-12-22 20:01:00
1
A
2
2021-12-22 15:09:44
1
A
3
2021-12-22 14:39:49
2
B
54
2021-12-22 12:46:37
2
B
23
2021-12-22 12:17:52
2
B
43
2021-12-22 11:19:11
1
A
23
2021-12-22 04:00:58
1
A
53
2021-12-22 03:00:58
3
C
21
2021-12-21 04:00:58
2
B
74
2021-12-21 04:06:58
2
B
36
2021-12-21 04:06:09
1
A
34
2021-12-21 03:08:09
Desired output
ItemID
ItemName
Value
Timestamp
1
A
22
2021-12-22 20:01:00
2
B
54
2021-12-22 12:46:37
1
A
23
2021-12-22 04:00:58
3
C
21
2021-12-21 04:00:58
2
B
74
2021-12-21 04:06:58
1
A
34
2021-12-21 03:08:09
Following query generates expected result
WITH ordered AS (
SELECT
*,
LAG(`ItemID`) OVER (ORDER BY `Timestamp` DESC) AS LastItem
FROM dataset
)
SELECT `ItemID`, `ItemName`, `Value`, `Timestamp`
FROM ordered
WHERE `ItemID` <> `LastItem` OR `LastItem` IS NULL
ORDER BY `Timestamp` DESC
demo
There is my issue :
I have two tables that i want to left join, and i want to display X rows depending on how many records there are in the second table.
Example :
TABLE_A TABLE_B
--------------------- -----------------------------------
| idA | yearA | | idB | idRefA | integ | lab |
|-------------------| |-------------------------------- |
| 1 | 2010 | | 20 | 2 | 54 | x |
| 2 | 2011 | | 20 | 2 | 50 | y |
| 3 | 2012 | | 20 | 2 | 28 | z |
| 4 | 2013 | | 21 | 3 | 18 | x |
| 5 | 2014 | | 21 | 3 | 22 | y |
--------------------- | 21 | 3 | 32 | z |
------------------------------------
There are my two tables.
This is my SQL query to join them :
SELECT * FROM TABLE_A
LEFT JOIN TABLE_B ON TABLE_A.ida = TABLE_B.idrefa
But i haven't the result that i expect, here is the result :
idA
yearA
idB
idRefA
integ
lab
1
2010
null
null
null
null
2
2011
20
2
28
z
2
2011
20
2
50
y
2
2011
20
2
54
x
3
2012
21
3
32
z
3
2012
21
3
22
y
3
2012
21
3
18
x
4
2013
null
null
null
null
5
2014
null
null
null
null
I am trying to have X rows on my LEFT JOIN (here X = 3). To complete the missing rows. Something like :
1 2010 19 1 0 x
1 2010 19 1 0 y
1 2010 19 1 0 z
...
Is it possible to do with a SELECT and LEFT JOIN ?
Here is my desired result :
ida
yearA
idb
idrefa
integ
lab
1
2010
null
null
null
null
1
2010
null
null
null
null
1
2010
null
null
null
null
2
2011
20
2
28
z
2
2011
20
2
50
y
2
2011
20
2
54
x
3
2012
21
3
32
z
3
2012
21
3
22
y
3
2012
21
3
18
x
Thanks for answer
Schemar and insert statements:
create table TABLE_A(idA int, yearA int);
insert into TABLE_A values( 1 , 2010 );
insert into TABLE_A values( 2 , 2011 );
insert into TABLE_A values( 3 , 2012 );
insert into TABLE_A values( 4 , 2013 );
insert into TABLE_A values( 5 , 2014 );
create table TABLE_B( idB int, idRefA int, integ int, lab varchar(10) );
insert into TABLE_B values( 20 , 2 , 54 , 'x' );
insert into TABLE_B values( 20 , 2 , 50 , 'y' );
insert into TABLE_B values( 20 , 2 , 28 , 'z' );
insert into TABLE_B values( 21 , 3 , 18 , 'x' );
insert into TABLE_B values( 21 , 3 , 22 , 'y' );
insert into TABLE_B values( 21 , 3 , 32 , 'z' );
Query#1 (for MySQL version older than 8.0):
SELECT min(ida)over() ida,min(yeara)over()yearA,idb,idrefa,integ,lab FROM TABLE_A
LEFT JOIN TABLE_B ON TABLE_A.ida = TABLE_B.idrefa
where TABLE_B.idrefa is null
union all
SELECT * FROM TABLE_A
LEFT JOIN TABLE_B ON TABLE_A.ida = TABLE_B.idrefa
where TABLE_B.idrefa is not null
Output:
ida
yearA
idb
idrefa
integ
lab
1
2010
null
null
null
null
1
2010
null
null
null
null
1
2010
null
null
null
null
2
2011
20
2
54
x
2
2011
20
2
50
y
2
2011
20
2
28
z
3
2012
21
3
18
x
3
2012
21
3
22
y
3
2012
21
3
32
z
Query#2 (for MySQL version 8.0 and above):
SELECT (case when idrefa is null then min(ida)over(partition by idrefa) else ida end)ida,(case when idrefa is null then min(yeara)over(partition by idrefa) else yearA end)yearA,idb,idrefa,integ,lab FROM TABLE_A
LEFT JOIN TABLE_B ON TABLE_A.ida = TABLE_B.idrefa
order by yearA
Output:
ida
yearA
idb
idrefa
integ
lab
1
2010
null
null
null
null
1
2010
null
null
null
null
1
2010
null
null
null
null
2
2011
20
2
28
z
2
2011
20
2
50
y
2
2011
20
2
54
x
3
2012
21
3
32
z
3
2012
21
3
22
y
3
2012
21
3
18
x
db<fiddle here
A problem with ordering a search result by 2 columns.
My table: transit time is stored in seconds, appointment is time
id transit_time appointment
----------------------------
2 3845 09:00:00
11 22053 13:00:00
10 4852 08:00:00
11 5985 NULL
13 7221 12:45:00
14 3812 NULL
17 4256 NULL
18 5663 NULL
19 4725 NULL
I want to make a select that order the records by:
1. appointment IS NULL at the end
2. by appointment time ASC
3. if transit time is greater than appointment then that record should be on top of sorting at point 2
For example the right order should be:
id transit_time appointment
----------------------------
11 22053 13:00:00
13 10221 12:45:00
10 3852 08:00:00
2 4245 09:00:00
11 5985 NULL
18 5663 NULL
19 4725 NULL
17 4256 NULL
14 3812 NULL
I've tried many sorts with CASE but no luck to get that order by. Any help will be appreciated.
SELECT * FROM table WHERE ...
ORDER BY (CASE WHEN appointment IS NULL THEN 1 ELSE 0 END) asc,
transit_time desc
Convert to seconds to make the comparison
ORDER BY CASE WHEN time_appointment IS NULL THEN 1 ELSE 0 END ,
transit_time DESC,
CASE WHEN transit_time > TIME_TO_SEC(time_appointment) THEN transit_time ELSE NULL END
im trying to write a database query to populate two coloumns of a custom table of myn by using three other tables in the same db, but currently those coloumns return nothing, i will provide tables and my query below!!! can any body solve this problem
table_17
trackID MyID Rid panelID timestamp useragent_browser useragent_OS
1 17 123 25 2014-04-29 10:02:01 Default Browser unknown NULL
2 17 123 25 2014-04-30 05:11:01 Default Browser unknown NULL
3 17 123 25 2014-05-15 06:44:02 Default Browser unknown NULL
4 17 59595 25 2014-10-06 07:50:30 Default Browser unknown 0
5 17 59595 25 2014-10-06 07:51:29 NULL NULL 0
6 17 59595 25 2014-10-06 07:54:51 NULL NULL 0
7 17 789 25 2014-10-07 10:20:29 Default Browser unknown 0
8 17 789 25 2014-10-07 10:23:48 Default Browser unknown 0
9 17 1881 25 2014-10-27 05:16:11 NULL NULL 0
10 17 188 25 2014-10-27 05:18:29 NULL NULL 0
projects
MyID projectname accountID creationdate status launchdate closedate total_impr
1 Virgo mobile 1 2010-07-07 09:09:09 3 2010-08-18 09:23:51 2011-11-18 19:38:41 1999
2 sys test 1 2010-06-05 09:12:35 3 2010-06-16 09:10:33 2010-06-30 11:23:55 2
16 test2 1 2010-08-16 14:12:50 1 NULL NULL 0
7 testproject 1 2010-08-24 14:44:40 2 2014-03-21 22:55:46 NULL 27
18 system2 1 2010-09-21 13:57:46 2 2010-09-21 12:03:23 NULL 51529
19 Holidayland 1 2010-09-30 08:36:55 2 2010-10-03 09:23:55 NULL 6246491
20 thor_test 1 2010-11-17 17:41:11 2 2014-07-17 10:43:53 NULL 0
21 cima 1 2011-02-14 19:13:32 2 2014-07-17 05:07:40 NULL 0
my query
mysql_query("
INSERT INTO custom_table (`useragent_browser`, `useragent_OS`,`MyID`,`timestamp`,`projectname`)
SELECT `useragent_browser`, `useragent_OS`, 'b.timestamp', 'b.MyID',`projectname`
FROM $table a, projects b
WHERE a.timestamp > DATE_ADD(NOW(), INTERVAL -1 MONTH) AND a.MyID = b.MyID")
or die(mysql_error());
The result i get from the query
trackID projectname MyID Rid panelID timestamp useragent_browser useragent_Os
1 testproject 0 NULL 25 0000-00-00 00:00:00 NULL NULL NULL
2 testproject 0 NULL 25 0000-00-00 00:00:00 NULL NULL NULL
4 Test168-150 0 NULL 25 0000-00-00 00:00:00 Chrome Win7 NULL
5 Test168-150 0 NULL 25 0000-00-00 00:00:00 Chrome Win7 NULL
6 Test168-150 0 NULL 25 0000-00-00 00:00:00 Default Browser unknown NULL
7 Test-155 0 NULL 25 0000-00-00 00:00:00 Default Browser unknown NULL
8 Test-155 0 NULL 25 0000-00-00 00:00:00 Default Browser unknown NULL
9 Test-155 0 NULL 25 0000-00-00 00:00:00 Default Browser unknown NULL
10 Test-155 0 NULL 25 0000-00-00 00:00:00 Default Browser unknown NULL
14 test155 0 NULL 25 0000-00-00 00:00:00 Default Browser unknown NULL
The columns which i need to populate Myid and timestamp is zero.. can anybody help me to populate those two correctly
Edit: organized query for readability above
You are accessing them incorrectly (as strings) and you have them reversed.
In the INSERT you have:
`MyID`,`timestamp`
In the SELECT you have:
'b.timestamp', 'b.MyID'
It should be:
`b.MyID`, `b.timestamp`
Therefore you are not using quotes, so they are not evaluated as strings and they are in the correct order.
I have a table that holds tasks like the following example:
id user_id project_id done_at duration type
1 13 15 2014-08-11 03:00:00 1
2 13 15 2014-08-11 04:00:00 1
10 13 15 2014-08-12 04:00:00 1
3 13 15 2014-08-12 08:00:00 2
5 13 13 2014-08-13 08:00:00 1
7 13 15 2014-08-14 04:00:00 1
8 13 15 2014-08-18 08:00:00 1
9 13 15 2014-08-19 08:00:00 2
How can i get start/end sequences of consecutive done_at column by user, project and type like this:
user_id project_id type start_done_at end_done_at duration
13 15 1 2014-08-11 2014-08-12 11:00:00
13 15 2 2014-08-12 2014-08-12 08:00:00
13 13 1 2014-08-13 2014-08-14 12:00:00
13 15 1 2014-08-18 2014-08-18 08:00:00
13 15 2 2014-08-19 2014-08-19 08:00:00
I already tried following this example: http://www.artfulsoftware.com/infotree/qrytip.php?id=76 however i didnt make to get the duration correctly.
My current query is this:
SELECT task.user_id as user_id, task.done_at as start_done_at, MIN(task2.done_at) as end_done_at, task.project_id as project_id, task.timeoff_type_id as timeoff_type, SEC_TO_TIME(SUM(TIME_TO_SEC(task.duration))) as duration
FROM tasks task
LEFT JOIN tasks task1 ON task.done_at = task1.done_at + 1
AND task.user_id = task1.user_id
AND task.project_id = task1.project_id
AND task.timeoff_type_id = task1.timeoff_type_id
LEFT JOIN tasks task2 ON task.done_at <= task2.done_at
AND task.user_id = task2.user_id
AND task.project_id = task2.project_id
AND task.timeoff_type_id = task2.timeoff_type_id
LEFT JOIN tasks task3 ON task2.done_at = task3.done_at -1
AND task2.user_id = task3.user_id
AND task2.project_id = task3.project_id
AND task2.timeoff_type_id = task3.timeoff_type_id
WHERE task1.done_at IS NULL
AND task2.done_at IS NOT NULL
AND task3.done_at IS NULL
GROUP BY task.user_id, task.project_id, task.timeoff_type_id, task.done_at
select
user_id, project_id,
min(done_at) as start_done_at,
max(done_at) as end_done_at,
sec_to_time(sum(time_to_sec(duration))) as duration
from (
select
t.*,
#gn := if(timestampdiff(day, #prev_date, done_at) between 0 and 1 and #prev_type = type and #prev_project = project_id, #gn, #gn + 1) as group_number,
#prev_date := done_at,
#prev_type := type,
#prev_project := project_id
from
t
, (select #prev_project := null, #prev_date := null, #prev_type := null, #gn := 0) var_init
order by project_id, done_at, duration
) sq
group by group_number
see it working live in an sqlfiddle