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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Query Help

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 help

Declare @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,

case
WHEN 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 DemoGroupOrder
INTO #Demographic
FROM BorrowerDim_Staging
UNION
SELECT DISTINCT 'Marital Status',[Marital Status], 1
FROM BorrowerDim_Staging
UNION
SELECT DISTINCT 'Race', Race, 1
FROM BorrowerDim_Staging
UNION
SELECT DISTINCT 'Sex', Sex, 1
FROM BorrowerDim_Staging
SELECT *, DemographicGroup + ' - ' + DemoGroup DemoGrouplabel
FROM #Demographic
WHERE 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 query
2. INSERT INTO statements to populate the tables with sample data
3. expected results from your query using the tables and sample data in items 1 and 2

Also, 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.
Go to Top of Page

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 = YourFieldDate

We are the creators of our own reality!
Go to Top of Page
   

- Advertisement -