Author |
Topic |
charles101
Starting Member
16 Posts |
Posted - 2007-02-20 : 02:31:09
|
Hello can someon please help with this problem - I have a table sampleno workcode result211 33 50211 33.01 51211 33.02 54211 33.03 32212 33 23212 33.01 34212 33.02 24212 33.03 11213 33.01 35214 33 36214 33.01 34214 33.02 45I would like to create a View returning the following output SampleNo 33_00 33_01 33_02 33_03211 50 51 54 32212 23 34 24 11213 35214 36 34 45I have tried the following Case statementSELECT [Sample Number], CASE WHEN 33 = [WORKCODE] THEN [RESULT] END AS [33_00],CASE WHEN 33.01 = [WORKCODE] THEN [RESULT] END AS [33_01], CASE WHEN 33.02 = [WORKCODE] THEN [RESULT] END AS [33_02], CASE WHEN 33.03 = [WORKCODE] THEN [RESULT] END AS [33_03], FROM dbo.[Other Sample Results]GROUP BY [Sample Number],CASE WHEN 33 = [WORKCODE] THEN [RESULT] END AS [33_00], CASE WHEN 33.01 = [WORKCODE] THEN [RESULT] END, CASE WHEN 33.02 = [WORKCODE] THEN [RESULT] END, CASE WHEN 33.03 = [WORKCODE] THEN [RESULT] END, CASE WHEN 33.04 = [WORKCODE] THEN [RESULT] END, The output I get is not grouping the records into a single record entry. SampleNo 33_00 33_01 33_02 33_03211 50211 51211 54211 32212 23 212 34212 24Can someone please help out with a query to get the records grouping in a single entry as SampleNo 33_00 33_01 33_02 33_03211 50 51 54 32212 23 34 24 11213 35214 36 34 45Thank you |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-02-20 : 02:45:10
|
[code]-- prepare sample datadeclare @t table (sampleno int, workcode varchar(5), result int)insert @tselect 211, '33', 50 union allselect 211, '33.01', 51 union allselect 211, '33.02', 54 union allselect 211, '33.03', 32 union allselect 212, '33', 23 union allselect 212, '33.01', 34 union allselect 212, '33.02', 24 union allselect 212, '33.03', 11 union allselect 213, '33.01', 35 union allselect 214, '33', 36 union allselect 214, '33.01', 34 union allselect 214, '33.02', 45-- show the dataselect sampleno, max(case when workcode = '33' then result end) as [33_00], max(case when workcode = '33.01' then result end) as [33_01], max(case when workcode = '33.02' then result end) as [33_02], max(case when workcode = '33.03' then result end) as [33_03]from @tgroup by samplenoorder by sampleno[/code]Peter LarssonHelsingborg, Sweden |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-02-20 : 02:45:34
|
[code]declare @yourtable table( sampleno int, workcode decimal(10,2), result int)insert into @yourtableselect 211, 33, 50 union allselect 211, 33.01, 51 union allselect 211, 33.02, 54 union allselect 211, 33.03, 32 union allselect 212, 33, 23 union allselect 212, 33.01, 34 union allselect 212, 33.02, 24 union allselect 212, 33.03, 11 union allselect 213, 33.01, 35 union allselect 214, 33, 36 union allselect 214, 33.01, 34 union allselect 214, 33.02, 45select sampleno, [33.00] = sum(case when workcode = 33.00 then result end), [33.01] = sum(case when workcode = 33.01 then result end), [33.02] = sum(case when workcode = 33.02 then result end), [33.03] = sum(case when workcode = 33.03 then result end)from @yourtable group by sampleno[/code] KH |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-02-20 : 02:52:26
|
Only a mere 24 seconds...Peter LarssonHelsingborg, Sweden |
 |
|
|
|
|