Solved

query syntax

Posted on 2015-02-23
3
33 Views
Last Modified: 2015-03-04
So now a follow up to a previous question!

I have the following query which i have grouped the measurements in to years and ranges.

PersonId is the identity of the patient.  Undersokningsdatum is a date, and U1MaximalIRAPDiameter is a measurement.

 What i really need now is to make sure i get all the empty rows.  That is for all the years i am interested in and for all the ranges.  How can i do that?

 with cte as(
 select
       PersonId , Undersokningsdatum , U1MaximalIRAPDiameter
 from (
       select
             PersonId , Undersokningsdatum , U1MaximalIRAPDiameter
           , ROW_NUMBER() OVER (PARTITION BY PersonId ORDER BY Undersokningsdatum DESC) AS RN
       from (
             select PersonId , Undersokningsdatum , U1MaximalIRAPDiameter
             from SweAAABas where Approved = 1 and U1Data = 1 and U1MaximalIRAPDiameter is not null
             union ALL
             select PersonId , Undersokningsdatum , U1MaximalIRAPDiameter
             from SweAAAFollowUp where Approved = 1 and U1Data = 1 and U1MaximalIRAPDiameter is not null
             ) AS SQA
       ) AS SQB
 where rn = 1
 )


 select 
   Year(t.Undersokningsdatum) as År,
   t.Aneurys as [Aneurys],
   count(t.Aneurys) as [Antal]
 from 
 (
   select Undersokningsdatum,
     case 
     when U1MaximalIRAPDiameter < 25 then '<25'
     when U1MaximalIRAPDiameter < 30 then '<30' 
     when U1MaximalIRAPDiameter < 35 then '<35' 
     when U1MaximalIRAPDiameter < 40 then '<40' 
     when U1MaximalIRAPDiameter < 45 then '<45' 
     when U1MaximalIRAPDiameter < 50 then '<50' 
     when U1MaximalIRAPDiameter < 55 then '<55' 
       else '>=55' end as Aneurys 
   from cte) t 
   
 group by  t.Aneurys, Year(Undersokningsdatum)
 order by  Year(Undersokningsdatum), t.Aneurys

Open in new window

0
Comment
Question by:soozh
  • 2
3 Comments
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40626934
Is there a table with ALL [PersonId] values, but only one row for each?
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 40627227
To produce a result that has every year for every person requires a cross join (Cartesian product).

let's assume
1. the range of years is 2013, 2014, 2015
2. the table [Persons] exists with a single row per person

then the overall query would look like this:

SELECT ... (list the columns)
FROM [Persons] AS P
CROSS JOIN  ( select 2013 as yr union all select 2014 union all select 2015 ) AS YRS
LEFT JOIN (
                      the query in the question here
                )  AS Q1
                   ON P.ID = Q1.personid AND yrs.yr = Q1.År

----------
If you do not have the equivalent of the assumed [Persons] table you could use a CTE of derived table instead e.g.

select distinct personid from [some-table-or-cte-here]

-----
If the range of years needed isn't known in advance that could also be made more flexible (e.g. by a recursive CTE).
If flexibility is not required just using constants and union all is fine.
0
 
LVL 45

Expert Comment

by:Vitor Montalvão
ID: 40639273
soozh, you still have the issue or it's already solved?
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
TSQL mapping detailed records to group records 9 30
SQl query 19 14
t-sql splitting string column 5 28
SQL Maintenance Plan 3 17
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now