Solved

query syntax

Posted on 2015-02-23
3
47 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 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 50

Expert Comment

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

Featured Post

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

705 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