?
Solved

query syntax

Posted on 2015-02-23
3
Medium Priority
?
52 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 49

Expert Comment

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

Accepted Solution

by:
PortletPaul earned 2000 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 51

Expert Comment

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

Featured Post

How To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

Question has a verified solution.

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

In this article I will describe the Detach & Attach 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.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

762 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