sql - Ranking query records in specific order - Stack Overflow
Assume we have Ambulance with Patient reception, and each Patient need to do one or more (min. 1, max. 4) blood analysis. Each Patient gets OrdNumber
(by arrival) FOR THAT DAY, but later they are prioritized in a way that patients with more required analysis are before patients with fewer required analysis (ALSO ON PER DAY BASIS).
Next day OrdNumbers start from 1 again.
Let's say:
- Day1 we have 2 patients:
Patient_Id=1
with 3 analysis,Patient_Id=2
with 4 analysis. - Day2 we have 2 patients:
Patient_Id=3
with 4 analysis,Patient_Id=4
with 2 analysis.
Table Reception
will look like this:
ReceptionId | DayOfWeek | PatientId | Patient_OrdNumber_ForThatDay | NumberOfRequiredAnalysis | Patient_OrdNumber_ByNumberOfRequiredAnalysis |
---|---|---|---|---|---|
1 | 1 | 1 | 1 | 3 | 2 |
2 | 1 | 2 | 2 | 4 | 1 |
3 | 2 | 3 | 1 | 4 | 1 |
4 | 2 | 4 | 2 | 2 | 2 |
Assume we have Ambulance with Patient reception, and each Patient need to do one or more (min. 1, max. 4) blood analysis. Each Patient gets OrdNumber
(by arrival) FOR THAT DAY, but later they are prioritized in a way that patients with more required analysis are before patients with fewer required analysis (ALSO ON PER DAY BASIS).
Next day OrdNumbers start from 1 again.
Let's say:
- Day1 we have 2 patients:
Patient_Id=1
with 3 analysis,Patient_Id=2
with 4 analysis. - Day2 we have 2 patients:
Patient_Id=3
with 4 analysis,Patient_Id=4
with 2 analysis.
Table Reception
will look like this:
ReceptionId | DayOfWeek | PatientId | Patient_OrdNumber_ForThatDay | NumberOfRequiredAnalysis | Patient_OrdNumber_ByNumberOfRequiredAnalysis |
---|---|---|---|---|---|
1 | 1 | 1 | 1 | 3 | 2 |
2 | 1 | 2 | 2 | 4 | 1 |
3 | 2 | 3 | 1 | 4 | 1 |
4 | 2 | 4 | 2 | 2 | 2 |
Records in Analysis
table are ordered by NumberOfRequiredAnalysis
(same as Patient_OrdNumber_ByNumberOfRequiredAnalysis
) FOR THAT DAY, so we will have:
AnalysisId | ReceptionId |
---|---|
1 | 2 |
2 | 2 |
3 | 2 |
4 | 2 |
5 | 1 |
6 | 1 |
7 | 1 |
8 | 3 |
9 | 3 |
10 | 3 |
11 | 3 |
12 | 4 |
13 | 4 |
So, we have all analysis grouped by ReceptionId
, but not ordered by ReceptionId
.
What I need to get is ordinal number (for coloring each odd group rows on report) of each ReceptionId
group from the Analysis
table, like this:
AnalysisId | OrdinalNumerOfReceptionIdGroup |
---|---|
1 | 1 |
2 | 1 |
3 | 1 |
4 | 1 |
5 | 2 |
6 | 2 |
7 | 2 |
8 | 3 |
9 | 3 |
10 | 3 |
11 | 3 |
12 | 4 |
13 | 4 |
I tried to achieve this with DENSE_RANK()
:
DENSE_RANK() OVER
(PARTITION BY Reception.DayOfWeek, Reception.Patient_OrdNumber_ByNumberOfRequiredAnalysys
ORDER BY Analysis.AnalysisId) AS OrdinalNumerOfReceptionIdGroup
but that does not produce the desired output.
Share Improve this question edited 19 hours ago Dale K 27k15 gold badges54 silver badges82 bronze badges asked 21 hours ago CondottieroCondottiero 11 silver badge 2 |2 Answers
Reset to default 1Given your Analysis
table exists as shown, you can assign the OrdinalNumerOfReceptionIdGroup
value as follows:
- Get each group transition using
lag
to compare the currentReceptionId
to the previousReceptionId
. Lets calls thisChangeInGroup
. - Sum the
ChangeInGroup
group value for the current row and all preceding rows.
with cte as (
select *
, case when ReceptionId <> lag (ReceptionId, 1, ReceptionId) over (order by AnalysisId) then 1 else 0 end ChangeInGroup
from Analysis
)
select AnalysisId, ReceptionId
, sum(ChangeInGroup) over
(order by AnalysisId rows between unbounded preceding and current row) + 1
from cte
order by AnalysisId;
Which gives as requested:
AnalysisId | ReceptionId | OrdinalNumerOfReceptionIdGroup |
---|---|---|
1 | 2 | 1 |
2 | 2 | 1 |
3 | 2 | 1 |
4 | 2 | 1 |
5 | 1 | 2 |
6 | 1 | 2 |
7 | 1 | 2 |
8 | 3 | 3 |
9 | 3 | 3 |
10 | 3 | 3 |
11 | 3 | 3 |
12 | 4 | 4 |
13 | 4 | 4 |
db<>fiddle
You can use ROW_NUMBER()
to get an ordinal number based on DayOfWeek
and Patient_OrdNumber_ByNumberOfRequiredAnalysis
, this is done in CTE ordered_receptions
, then it is joined to the analysis table to map the ordinal number to each analysis.
Fiddle
WITH ordered_receptions AS (
SELECT
ReceptionId,
ROW_NUMBER() OVER (ORDER BY DayOfWeek, Patient_OrdNumber_ByNumberOfRequiredAnalysis) AS ordinalnumber
FROM
reception
)
, mapped_analysis AS (
SELECT
a.AnalysisId,
r.ordinalnumber
FROM
analysis a
JOIN
ordered_receptions r ON a.receptionid = r.receptionid
)
SELECT
analysisid,
ordinalnumber AS ordinalnumer_receptionid_group
FROM
mapped_analysis
ORDER BY
analysisid;
Output
analysisid | ordinalnumer_receptionid_group |
---|---|
1 | 1 |
2 | 1 |
3 | 1 |
4 | 1 |
5 | 2 |
6 | 2 |
7 | 2 |
8 | 3 |
9 | 3 |
10 | 3 |
11 | 3 |
12 | 4 |
13 | 4 |
- 京东1.7亿美元投资金蝶原因:布局企业ERP市场
- docker - how to fix "communications link failure the last packet sent successfully to the server was 0 milliseconds ago
- TypeScript Error (ts2345) when trying to collect property names in array - Stack Overflow
- reactjs - Why getting error 'Objects are not valid as a React child' in turborepo ui tests? - Stack Overflow
- csv - How to Handle Clob Data Type in Excel When Exporting Data From DB2 Database - Stack Overflow
- swift - How to Add .mlmodel File to Xcode App Playgrounds (.swiftpm) Project? - Stack Overflow
- typescript - Command 'pod install' failed, Cause: binbash -c - Stack Overflow
- java - How to write custom Spring Security PreAuthorize annotation - Stack Overflow
- Django is not updating on MacOS - Stack Overflow
- python - aiohttp showing 403 error but requests.get giving 200 response - Stack Overflow
- typescript - Declare object and internally refer to other values - Stack Overflow
- c - sorry, unimplemented: Thumb-1 ‘hard-float’ VFP ABI - arm-linux-gnueabihf-gcc - targeting armv6 - Stack Overflow
- How to configure PhpStorm to work with Node.js and ESLint inside a running Docker container? - Stack Overflow
- reactjs - How to keep the modal open after navigating and returning from a certain screen in react native? - Stack Overflow
- datagrid - Mudblazor MudDatagrid SelectedItemChanged problem with ondblclick version 8.0.0-preview - Stack Overflow
- typescript - angular signal is not recomputing - Stack Overflow
- sockets - PICO WMicropython websocket client can't send to PHP websocket properly - Stack Overflow
Analysis
table. Is something missing there? Or you handle that when creating theAnalysis
rows? – Dale K Commented 19 hours ago