[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

query syntax

Posted on 2015-02-23
3
Medium Priority
?
56 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 52

Expert Comment

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

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

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.
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…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

656 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