Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
bvanyangu
Starting Member
20 Posts |
Posted - 2014-06-24 : 06:48:49
|
My query below executes Zero Rows. I need to get these columns;DemoGroupOrder,DemoGroup,DemographicGroup & DemoGrouplabe. How should I write this query, so I get the rows I want.Thanks for your helpDeclare @DemoGroupOrder int,@ReportDate date,@DemoCategory varchar (200)SELECT DISTINCT 'Age' DemographicGroup,CASE WHEN DATEDIFF(YY, [Date of Birth], @ReportDate) < 25 THEN '<=25' WHEN DATEDIFF(YY,[Date of Birth],@ReportDate) BETWEEN 25 AND 35 THEN '26-35'WHEN DATEDIFF(YY,[Date of Birth],@ReportDate) BETWEEN 36 AND 45 THEN '36-45' WHEN DATEDIFF(YY,[Date of Birth],@ReportDate) > 45 THEN '46+' END AS DemoGroup,caseWHEN DATEDIFF(YY,[Date of Birth],@ReportDate) < 25 THEN 1 WHEN DATEDIFF(YY,[Date of Birth] ,@ReportDate) BETWEEN 25 AND 35 THEN 2 WHEN DATEDIFF(YY,[Date of Birth],@ReportDate) BETWEEN 36 AND 45 THEN 3 WHEN DATEDIFF(YY,[Date of Birth],@ReportDate) > 45 THEN 4 END AS DemoGroupOrderINTO #DemographicFROM BorrowerDim_Staging UNIONSELECT DISTINCT 'Marital Status',[Marital Status], 1FROM BorrowerDim_StagingUNIONSELECT DISTINCT 'Race', Race, 1FROM BorrowerDim_Staging UNIONSELECT DISTINCT 'Sex', Sex, 1FROM BorrowerDim_Staging SELECT *, DemographicGroup + ' - ' + DemoGroup DemoGrouplabelFROM #DemographicWHERE DemographicGroup IN (@DemoCategory)ORDER BY DemographicGroup, DemoGroupOrder |
|
gbritton
Master Smack Fu Yak Hacker
2780 Posts |
Posted - 2014-06-24 : 08:00:04
|
Need to see:1. CREATE TABLE statements for all the tables in your query2. INSERT INTO statements to populate the tables with sample data3. expected results from your query using the tables and sample data in items 1 and 2Also, I cannot see where you are setting the variable @DemoCategory. If your query runs with that variable left at its default value (null) it will indeed return no rows. |
 |
|
sz1
Aged Yak Warrior
555 Posts |
Posted - 2014-06-24 : 08:04:10
|
For a start you have declared variables but you haven't assigned anything?Declare @DemoGroupOrder int,@ReportDate@DemoCategory varchar (200)WHERE DemographicGroup IN (@DemoCategory)In what? CASE WHEN DATEDIFF(YY, [Date of Birth], @ReportDate) < 25 THEN '<=25' @ReportDate date = GetDATE(), ?? or another assigned date...you need to assign the variables, if @ReportDate is in the table and is the field then it should read:@ReportDate date = YourFieldDateWe are the creators of our own reality! |
 |
|
|
|
|
|
|