Related
I want to run the same select query but change only the romecode field which is a string and limit each result by 30. Finally, I concatenate all the results into one usingUNION ALL.
This is the full list of codes which means that I need to repeat the same select + UNION ALL many times:
('G1603', 'E1205', 'D1101', 'N1202', 'M1501', 'G1402', 'I1401',
'M1607', 'J1102', 'C1201', 'M1801', 'I1203', 'I1604', 'M1705',
'H2102', 'M1203', 'K2503', 'E1103', 'N1103', 'M1805', 'H1204',
'M1602', 'D1106', 'M1707', 'C1501', 'M1701', 'G1101', 'J1302',
'C1103', 'E1401', 'J1201', 'H1301', 'C1301')
And how I am doing now:
(
SELECT
appellationlibelle,
romelibelle,
romecode,
descriptioncleaned,
description
FROM
`scrappers-293910.vigilant_memory_raw.indeed`
WHERE romecode = 'G1603' LIMIT 30)
UNION ALL
(
SELECT
appellationlibelle,
romelibelle,
romecode,
descriptioncleaned,
description
FROM
`scrappers-293910.vigilant_memory_raw.indeed`
WHERE romecode = 'E1205' LIMIT 30)
UNION ALL
(
SELECT
appellationlibelle,
romelibelle,
romecode,
descriptioncleaned,
description
FROM
`scrappers-293910.vigilant_memory_raw.indeed`
WHERE romecode = 'D1101' LIMIT 30)
I repeat this select 33 times.
I tried to find a similar solution but I couldn't find any. If it is a duplicated question just kindly drop the link please :D
Combine into a single query, use Row_number() to give each row a number resetting on each Romecode. Then bring back all rows where the row number is 30 or below.
You just need to clarify how you choose which 30 rows to bring back. Your original query doesn't specify so you'll need to figure that out to plug into the row_number order by.
select
*
from
(
SELECT
appellationlibelle,
romelibelle,
romecode,
descriptioncleaned,
description,
row_number() over (partition by romecode order by datecreation) as rn
FROM
scrappers-293910.vigilant_memory_raw.indeed
WHERE romecode in
('G1603', 'E1205', 'D1101', 'N1202', 'M1501', 'G1402', 'I1401',
'M1607', 'J1102', 'C1201', 'M1801', 'I1203', 'I1604', 'M1705',
'H2102', 'M1203', 'K2503', 'E1103', 'N1103', 'M1805', 'H1204',
'M1602', 'D1106', 'M1707', 'C1501', 'M1701', 'G1101', 'J1302',
'C1103', 'E1401', 'J1201', 'H1301', 'C1301')
) thedata where thedata.rn <= 30
Maybe I've misunderstood your question, but this seems easy to do with an IN (...) condition.
SELECT
appellationlibelle,
romelibelle,
romecode,
descriptioncleaned,
description
FROM
`scrappers-293910.vigilant_memory_raw.indeed`
WHERE romecode in ('G1603', 'E1205', 'D1101', 'N1202', 'M1501', 'G1402', 'I1401',
'M1607', 'J1102', 'C1201', 'M1801', 'I1203', 'I1604', 'M1705',
'H2102', 'M1203', 'K2503', 'E1103', 'N1103', 'M1805', 'H1204',
'M1602', 'D1106', 'M1707', 'C1501', 'M1701', 'G1101', 'J1302',
'C1103', 'E1401', 'J1201', 'H1301', 'C1301')
-- LIMIT 30 <-- don't know if you still want this?
I have this data:
"config_timeslice_id","config_id","created"
14326,1145,"2021-08-31 13:45:00"
14325,1145,"2021-08-22 13:34:51"
14321,1145,"2021-06-16 10:47:59"
2357,942,"2019-12-24 10:09:38"
When I run this query:
SELECT config_timeslice_id
FROM config_timeslice
WHERE config_id = 1145
AND created <= CURRENT_TIMESTAMP
ORDER BY created DESC
LIMIT 1
I get 14325, as I would expect, because today is 2021-08-23.
But when I run this query:
SELECT DISTINCT t.config_id,
(
SELECT config_timeslice_id
FROM config_timeslice
WHERE config_id = t.config_id
AND created <= CURRENT_TIMESTAMP
ORDER BY created DESC
LIMIT 1
) AS ts_id
FROM config_timeslice t
I get:
config_id,ts_id
942,2357
1145,14321
I can’t figure out why the second row doesn’t give 14325
MariaDB 10.4.18 must have a bug. When I upgraded to 10.4.21, it works.
I got a problem regarding grouping if a value is the same as in the row above.
Our statement looks like this:
SELECT pat_id,
treatData.treatmentdate AS Date,
treatMeth.name AS TreatDataTableInfo,
treatData.treatmentid AS TreatID
FROM dialysistreatmentdata treatData
LEFT JOIN hdtreatmentmethods treatMeth
ON treatMeth.id = treatData.hdtreatmentmethodid
WHERE treatData.hdtreatmentmethodid IS NOT NULL
AND Year(treatData.treatmentdate) >= 2013
AND ekeyid = 12
ORDER BY treatData.ekeyid,
treatmentdate DESC,
treatdatatableinfo;
The output looks like this:
The desired output should be grouped if the value is the same as in the row/rows before and ther should be a ToDate as you can see in the screenshot which is the date of the next row -1 day.
The desired output should look like this:
I hope someone has a solution regarding this matter!
Or maybe someone has an idea how to solve this problem within qlikview.
Looking forward for solutions
Michael
You want to collapse episodes of treatment into single rows. This is a "gaps-and-islands" problem. I like the difference of row numbers approach:
select patid, min(date) as fromdate, max(date) as todate, TreatDataTableInfo,
min(treatid)
from (select td.Pat_ID, td.TreatmentDate As Date, tm.Name As TreatDataTableInfo,
td.TreatmentID As TreatID,
row_number() over (partition by td.pat_id order by td.treatmentdate) as seqnum_p,
row_number() over (partition by td.pat_id, tm.name order by td.treatment_date) as seqnum_pn
from DialysisTreatmentData td Left join
HDTreatmentMethods tm
On tm.ID = td.HDTreatmentMethodID
where td.HDTreatmentMethodID Is Not Null And
td.TreatmentDate) >= '2013-01-01' and
EKeyID = 12
) t
group by patid, TreatDataTableInfo, (seqnum_p - seqnum_pn)
order by patid, TreatmentDate Desc, TreatDataTableInfo;
Note: This uses the ANSI standard window function row_number(), which is available in most databases.
Below is a possible Qlikview solution. I've put some comments in the script. If it's not clear just let me know. The result picture is below the script.
RawData:
Load * Inline [
Pat_ID,Date,TreatDataTableInfo,TreatId
PatNum_12,08.07.2016,HDF Pradilution,1
PatNum_12,07.07.2016,HDF Predilution,2
PatNum_12,23.03.2016,HD,3
PatNum_12,24.11.2015,HD,4
PatNum_12,22.11.2015,HD,5
PatNum_12,04.09.2015,HD,6
PatNum_12,01.09.2015,HD,7
PatNum_12,30.07.2015,HD,8
PatNum_12,12.01.2015,HD,9
PatNum_12,09.01.2015,HD,10
PatNum_12,26.08.2014,Hemodialysis,11
PatNum_12,08.07.2014,Hemodialysis,12
PatNum_12,23.05.2014,Hemodialysis,13
PatNum_12,19.03.2014,Hemodialysis,14
PatNum_12,29.01.2014,Hemodialysis,15
PatNum_12,14.12.2013,Hemodialysis,16
PatNum_12,26.10.2013,Hemodialysis,17
PatNum_12,05.10.2013,Hemodialysis,18
PatNum_12,03.10.2013,HD,19
PatNum_12,24.06.2013,Hemodialysis,20
PatNum_12,03.06.2013,Hemodialysis,21
PatNum_12,14.05.2013,Hemodialysis,22
PatNum_12,26.02.2013,HDF Postdilution,23
PatNum_12,23.02.2013,HDF Pradilution,24
PatNum_12,21.02.2013,HDF Postdilution,25
PatNum_12,07.02.2013,HD,26
PatNum_12,25.01.2013,HDF Pradilution,27
PatNum_12,18.01.2013,HDF Pradilution,28
];
GroupedData:
Load
*,
// assign new GroupId for all rows where the TreatDataTableInfo is equal
if( RowNo() = 1, 1,
if( TreatDataTableInfo <> peek('TreatDataTableInfo'),
peek('GroupId') + 1, peek('GroupId'))) as GroupId,
// assign new GroupSubId (incremental int) for all the records in each group
if( TreatDataTableInfo <> peek('TreatDataTableInfo'),
1, peek('GroupSubId') + 1) as GroupSubId,
// pick the first Date field value and spread it acccross the group
if( TreatDataTableInfo <> peek('TreatDataTableInfo'), TreatId, peek('TreatId_Temp')) as TreatId_Temp
Resident
RawData
;
Drop Table RawData;
right join (GroupedData)
// get the max GroupSubId for each group and right join it to
// the GroupedData table to remove the records we dont need
MaxByGroup:
Load
max(GroupSubId) as GroupSubId,
GroupId
Resident
GroupedData
Group By
GroupId
;
// these are not needed anymore
Drop Fields GroupId, GroupSubId, TreatId;
// replace the old TreatId with the new TreatId_Temp field
// which contains the first TreatId for each group
Rename Field TreatId_Temp to TreatId;
I have a table that a user placing bets on dUpdateTime.
DB Table :
My Query :
select * from tbet where iUserKey=53298
How do I create a query that would give me the latest dUpdateTime with iUserKey = 53298.
Using MAX aggregate function
SELECT MAX(dUpdateTime) FROM tbet WHERE iUserKey = 53298
Try this:
select * from tbet where iUserKey=53298 order by dUpdateTime desc limit 1
Use max aggregate function to get the latest
SELECT MAX(dUpdateTime) FROM tbet WHERE iUserKey=53298
I got a few problems when trying to fetch only specific data. First I don't know how to create a sql query (current sql query I can grab only one user) so I can grab the data like this.
Second I want to grab 1 year data until current date. Below is my sql query done so far (I need to do it manual one by one).
SELECT type, COUNT(*) FROM (
TABLE_DATE_RANGE([githubarchive:day.events_],
TIMESTAMP('2013-1-01'),
TIMESTAMP('2015-08-28')
)) AS events
WHERE type IN ("CommitCommentEvent","CreateEvent","DeleteEvent","DeploymentEvent","DeploymentStatusEvent","DownloadEvent","FollowEvent",
"ForkEvent","ForkApplyEvent","GistEvent","GollumEvent","IssueCommentEvent","IssuesEvent","MemberEvent","MembershipEvent","PageBuildEvent",
"PublicEvent","PullRequestEvent","PullRequestReviewCommentEvent","PushEvent","ReleaseEvent","RepositoryEvent","StatusEvent","TeamAddEvent",
"WatchEvent") AND actor.login = "datomnurdin"
GROUP BY type;
Reference:
https://www.githubarchive.org/
https://github.com/igrigorik/githubarchive.org
Here is how to properly pivot the data:
SELECT actor.login,
ifnull(sum(if(type='CommitCommentEvent',1,null)),0) as CommitCommentEvent,
ifnull(sum(if(type='CreateEvent',1,null)),0) as CreateEvent,
ifnull(sum(if(type='DeleteEvent',1,null)),0) as DeleteEvent,
ifnull(sum(if(type='DeploymentEvent',1,null)),0) as DeploymentEvent,
ifnull(sum(if(type='DeploymentStatusEvent',1,null)),0) as DeploymentStatusEvent,
ifnull(sum(if(type='DownloadEvent',1,null)),0) as DownloadEvent,
ifnull(sum(if(type='FollowEvent',1,null)),0) as FollowEvent,
ifnull(sum(if(type='ForkEvent',1,null)),0) as ForkEvent,
ifnull(sum(if(type='ForkApplyEvent',1,null)),0) as ForkApplyEvent,
ifnull(sum(if(type='GistEvent',1,null)),0) as GistEvent,
ifnull(sum(if(type='GollumEvent',1,null)),0) as GollumEvent,
ifnull(sum(if(type='IssueCommentEvent',1,null)),0) as IssueCommentEvent,
ifnull(sum(if(type='IssuesEvent',1,null)),0) as IssuesEvent,
ifnull(sum(if(type='MemberEvent',1,null)),0) as MemberEvent,
ifnull(sum(if(type='MembershipEvent',1,null)),0) as MembershipEvent,
ifnull(sum(if(type='PageBuildEvent',1,null)),0) as PageBuildEvent,
ifnull(sum(if(type='PublicEvent',1,null)),0) as PublicEvent,
ifnull(sum(if(type='PullRequestEvent',1,null)),0) as PullRequestEvent,
ifnull(sum(if(type='PullRequestReviewCommentEvent',1,null)),0) as PullRequestReviewCommentEvent,
ifnull(sum(if(type='PushEvent',1,null)),0) as PushEvent,
ifnull(sum(if(type='ReleaseEvent',1,null)),0) as ReleaseEvent,
ifnull(sum(if(type='RepositoryEvent',1,null)),0) as RepositoryEvent,
ifnull(sum(if(type='StatusEvent',1,null)),0) as StatusEvent,
ifnull(sum(if(type='TeamAddEvent',1,null)),0) as TeamAddEvent,
ifnull(sum(if(type='WatchEvent',1,null)),0) as WatchEvent,
FROM (
TABLE_DATE_RANGE([githubarchive:day.events_],
DATE_ADD(CURRENT_TIMESTAMP(), -1, "YEAR"),
CURRENT_TIMESTAMP()
)) AS events
WHERE type IN ("CommitCommentEvent","CreateEvent","DeleteEvent","DeploymentEvent","DeploymentStatusEvent","DownloadEvent","FollowEvent",
"ForkEvent","ForkApplyEvent","GistEvent","GollumEvent","IssueCommentEvent","IssuesEvent","MemberEvent","MembershipEvent","PageBuildEvent",
"PublicEvent","PullRequestEvent","PullRequestReviewCommentEvent","PushEvent","ReleaseEvent","RepositoryEvent","StatusEvent","TeamAddEvent",
"WatchEvent")
GROUP BY 1
limit 100