December 6, 2014 at 3:55 am
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...
December 6, 2014 at 5:29 am
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.
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
December 18, 2014 at 8:21 pm
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 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