Solved

query syntax

Posted on 2015-02-23
3
42 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 49

Expert Comment

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

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

733 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