0

I have a table DemographicDetails showing population information. The fields are

  • ID - Unique id field (autoincrement)
  • Population - integer
  • SurveyDate - datetime field
  • City - varchar field indicating city
  • State - varchar
  • Region- South/North
  • Country

I need to have report to display the population details in below format

India              Current Yr Figure        PreviousYr Figure   
State_1            X,XX,XX,XXX              XX,XX,XXX 
State_2            X,XX,XX,XXX              XX,XX,XXX 
State_3            X,XX,XX,XXX              XX,XX,XXX 

I have tried with the below query:

WITH cte AS (
    SELECT TOP (99.99) PERCENT 
            Population, City, State, SurveyDate, Region, Country, 
            CASE WHEN (DATEPART(yyyy, surveydate) = DATEPART(yyyy, GETDATE()))THEN 'CurrentYear' ELSE NULL END AS CurrentYear, 
            CASE WHEN (DATEPART(yyyy, surveydate) <> DATEPART(yyyy, GETDATE())) THEN 'LastYear' ELSE NULL END AS LastYear
    FROM DemographicDetails
    ORDER BY City)  
SELECT Population, City, State, Region, Country, SurveyDate, 
       CurrentYear, LastYear, COALESCE (CurrentYear, LastYear) AS DateStatus
FROM cte AS cte_1

And in the report textbox I have given the expression as:

iif(Fields!DateStatus.Value="CurrentYear",Fields!Population.Value,0) 

In first textbox and in second box the expression is:

iif(Fields!DateStatus.Value="LastYear",Fields!Population.Value,0)

However, my result is similar to this:

India       Current Yr Figure     PreviousYr Figure          
State_1     value1                0
State_1     0                     value2
State_2     value3                0 
State_3     value4                0

But I need it as follows:

India               Current Yr Figure         PreviousYr Figure          
State_1             value1                    value2
State_2             value3                    0 
State_3             value4                    0

i.e for state_1 value1 and value2 in same row.

4

0 回答 0