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 |
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2007-08-22 : 13:50:37
|
What's the easiest way to accomplish this.I have the following columns of a tableMyRecordID,Rate1,Rate2,Rate3,Rate4,Rate5I need to take the Maximum out of those 5 rates.SoSelect MyRecordID,HIGHESTVALUE(RATE1,RATE2,RATE3,RATE4,RATE5)from MytableObviously HIGHESTVALUE is a made up function, but is there a easy way to accomplish this with out case statments wrapped into case statements?Thanks |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-08-22 : 13:55:33
|
Is this the new exam question?SELECT MyRecordID, Date1, Date2, Date3, Date4, ( SELECT MAX(p) FROM(SELECT Rate1 AS p UNION ALL SELECT Rate2 UNION ALL SELECT Rate3 UNION ALL SELECT Rate4 UNION ALL SELECT Rate5) AS d)FROM MyTable E 12°55'05.25"N 56°04'39.16" |
 |
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2007-08-22 : 13:59:53
|
No, more like a boss get's mad if I don't have his query working perfect in next 30 minute question :)Thanks |
 |
|
X002548
Not Just a Number
15586 Posts |
|
X002548
Not Just a Number
15586 Posts |
Posted - 2007-08-22 : 14:53:59
|
[code]DECLARE @x table(Col1 int IDENTITY(1,1), Rate1 int, Rate2 int, Rate3 int, Rate4 int, Rate5 int)INSERT INTO @x(Rate1, Rate2, Rate3, Rate4, Rate5)SELECT 1,2,3,4,5 UNION ALLSELECT 5,4,3,2,1 UNION ALLSELECT 4,3,5,1,2SELECT a.Col1, MAX_Rate FROM @x a JOIN (SELECT Col1, MAX(Rate) AS MAX_Rate FROM ( SELECT Col1, Rate1 AS Rate FROM @x UNION ALL SELECT Col1, Rate2 AS Rate FROM @x UNION ALL SELECT Col1, Rate3 AS Rate FROM @x UNION ALL SELECT Col1, Rate4 AS Rate FROM @x UNION ALL SELECT Col1, Rate5 AS Rate FROM @x) AS XXX GROUP BY Col1) AS bON a.Col1 = b.Col1[/code]Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-08-22 : 15:15:27
|
No need to self-join.quote: Originally posted by X002548
SELECT Col1, MAX(Rate) AS MAX_Rate FROM ( SELECT Col1, Rate1 AS Rate FROM @x UNION ALL SELECT Col1, Rate2 AS Rate FROM @x UNION ALL SELECT Col1, Rate3 AS Rate FROM @x UNION ALL SELECT Col1, Rate4 AS Rate FROM @x UNION ALL SELECT Col1, Rate5 AS Rate FROM @x) AS XXX GROUP BY Col1
E 12°55'05.25"N 56°04'39.16" |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-08-22 : 15:21:49
|
Interesting Brett...Here is your query plan (89% of batch and 15 reads) |--Stream Aggregate(GROUP BY:([Union1020]) DEFINE:([Expr1022]=MAX([Union1021]))) |--Sort(ORDER BY:([Union1020] ASC)) |--Concatenation |--Table Scan(OBJECT:(@x)) |--Table Scan(OBJECT:(@x)) |--Table Scan(OBJECT:(@x)) |--Table Scan(OBJECT:(@x)) |--Table Scan(OBJECT:(@x)) And here is mine (11% of batch and 3 reads) |--Compute Scalar(DEFINE:([Expr1007]=[Expr1005])) |--Nested Loops(Inner Join, OUTER REFERENCES:([Rate1], [Rate2], [Rate3], [Rate4], [Rate5])) |--Table Scan(OBJECT:(@x)) |--Stream Aggregate(DEFINE:([Expr1005]=MAX([Union1004]))) |--Constant Scan(VALUES:(([Rate1]),([Rate2]),([Rate3]),([Rate4]),([Rate5]))) My querySELECT Col1, ( SELECT MAX(p) FROM(SELECT Rate1 AS p UNION ALL SELECT Rate2 UNION ALL SELECT Rate3 UNION ALL SELECT Rate4 UNION ALL SELECT Rate5) AS d)FROM @x E 12°55'05.25"N 56°04'39.16" |
 |
|
X002548
Not Just a Number
15586 Posts |
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2007-08-22 : 16:11:20
|
quote: Originally posted by X002548 ummmm....are you still with us?Tell said boss to buy a book on data Normalization
I'm not sure if I understand the response. Here's a little more background info:Rate1 - 5 represents a individual weekly rate which we are provided by a customer (Week 1 is Rate 1, week 2 is rate 2 etc...) in the form of a 20 page RTF report we get each month.I wrote some code to parse this file by litterally going through each line of the RTF and breaking it down creating the record info all into seperate columns in the table as I mentioned (This includes rate1-5).Peso's method works very well, so I'm not sure if there is anything to normalize here?Please let me know how you would suggest to do this.Are you saying to create seperate tables to contain the week/rate information. I can see how this may be benefitial with 1000's of records, but I am dealing with a max of 500 records each month. Is it worth re-dueing this methodology? |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-08-22 : 16:18:14
|
[code]-- The denormalized waydeclare @x table (col int identity(1, 1), rate1 int, rate2 int, rate3 int, rate4 int, rate5 int)insert @xselect 1, 2, 3, 4, 5 union allselect 5, 4, 3, 2, 1 union allselect 4, 3, 5, 1, 2SELECT Col, (SELECT MAX(p) FROM (SELECT Rate1 AS p UNION ALL SELECT Rate2 UNION ALL SELECT Rate3 UNION ALL SELECT Rate4 UNION ALL SELECT Rate5) AS d) AS MaxRateFROM @x-- The normalized wayDECLARE @NewDesign TABLE (MyID INT, MyWeek INT, MyRate INT)INSERT @NewDesignSELECT 1, 1, 1 UNION ALLSELECT 1, 2, 2 UNION ALLSELECT 1, 3, 3 UNION ALLSELECT 1, 4, 4 UNION ALLSELECT 1, 5, 5 UNION ALLSELECT 5, 1, 5 UNION ALLSELECT 5, 2, 4 UNION ALLSELECT 5, 3, 3 UNION ALLSELECT 5, 4, 2 UNION ALLSELECT 5, 5, 1 UNION ALLSELECT 4, 1, 4 UNION ALLSELECT 4, 2, 3 UNION ALLSELECT 4, 3, 5 UNION ALLSELECT 4, 4, 1 UNION ALLSELECT 4, 5, 2SELECT MyID, MAX(MyRate) AS MaxRateFROM @NewDesignGROUP BY MyIDORDER BY MyID[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-08-22 : 16:28:03
|
This is how you can normalize your present table-- The denormalized waydeclare @x table (col int identity(1, 1), rate1 int, rate2 int, rate3 int, rate4 int, rate5 int)insert @xselect 1, 2, 3, 4, 5 union allselect 5, 4, 3, 2, 1 union allselect 4, 3, 5, 1, 2select * from @x-- Normalize the dataSELECT Col, 1 AS MyWeek, Rate1 AS MyRate FROM @x UNION ALLSELECT Col, 2, Rate2 FROM @x UNION ALLSELECT Col, 3, Rate3 FROM @x UNION ALLSELECT Col, 4, Rate4 FROM @x UNION ALLSELECT Col, 5, Rate5 FROM @xORDER BY Col, MyWeek E 12°55'05.25"N 56°04'39.16" |
 |
|
pootle_flump
1064 Posts |
|
|
|
|
|
|