need to group values

  • my table stucture like

    date HourType hours HourValue status

    day1 m 1 1 y

    day1 m 2 1 y

    day1 m 3 1 y

    day1 a 4 0 n

    day1 a 5 0 n

    day1 a 6 0 n

    day2 m 1 1 y

    day2 m 2 1 y

    day2 m 3 1 y

    day2 a 4 0 y

    day2 a 5 1 y

    day2 a 6 1 y

    my day1 afternoon all hours 0, and day2 4th hour only 0,

    i need to status column that must be 'N' when all afternoon hours or morning hours is 0, if only one hour 0 means that must 'Y',

    how do set a status column like above.... can anyone help...

  • Can you write out the desired results, based on your sample data?

    If you could provide your input data in consumable (ie, runnable in SSMS) format, you will be more likely to get a working solution sooner. See the link in my signature for details of how to do that.

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • Perhaps this is what you mean.

    WITH SampleData (date, HourType, hours, HourValue, status) AS(

    SELECT 'day1','m',1,1,'y'

    UNION ALL SELECT 'day1','m',2,1,'y'

    UNION ALL SELECT 'day1','m',3,1,'y'

    UNION ALL SELECT 'day1','a',4,0,'n'

    UNION ALL SELECT 'day1','a',5,0,'n'

    UNION ALL SELECT 'day1','a',6,0,'n'

    UNION ALL SELECT 'day2','m',1,1,'y'

    UNION ALL SELECT 'day2','m',2,1,'y'

    UNION ALL SELECT 'day2','m',3,1,'y'

    UNION ALL SELECT 'day2','a',4,0,'y'

    UNION ALL SELECT 'day2','a',5,1,'y'

    UNION ALL SELECT 'day2','a',6,1,'y'

    )

    SELECT *

    ,CASE (

    SELECT MAX(HourValue)

    FROM SampleData b

    WHERE a.date = b.date AND a.HourType = b.HourType

    ) WHEN 1 THEN 'y' ELSE 'n' END

    FROM SampleData a;


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply