Author |
Topic |
dataguru1971
Master Smack Fu Yak Hacker
1464 Posts |
Posted - 2010-12-02 : 19:29:37
|
Hi all,I have a need/want to simplify some lengthy queries...amongst the tasks is taking a source table with 16 possible columns and returning the result into 4 columns, with four different criteria. and example of one column/criteria is below (the 2nd column would be for "B" etc...).the table result is only about 6 or 7 columns, but the t-sql is 6 pages of this kind of thing...and the conditionals in the column selection are separated in the where clause by OR's...sample code belowCreate table foobar (ID int identity(1,1),C1 char(1) null,C2 char(1) null,C3 char(1) null,C4 char(1) null,C5 char(1) null,C6 char(1) null,C7 char(1) null,C8 char(1) null,C9 char(1) null,C10 char(1) null,C11 char(1) null,C12 char(1) null)Insert into foobarSelect 'A','A', '', '', '', '', '', '', '', '', '', '' UNIONSelect 'B','A','A', '', '', '', '', '', '', '', '', '' UNIONSelect 'C','B','B', '', '', '', '', '', '', '', '', '' UNIONSelect '', '', '','A', '', '', '', '', '', '', '', '' UNIONSelect '', '', '', '','A','A', '', '', '', '', '', '' UNIONSelect '', '', '', '','B','A','A', '', '', '', '', '' UNIONSelect '', '', '', '','C','B','B', '', '', '', '', '' UNIONSelect '', '', '', '', '', '', '','A', '', '', '', '' UNIONSelect '', '', '', '', '', '', '', '','A','A', '', '' UNIONSelect '', '', '', '', '', '', '', '','B','A','A', '' UNIONSelect '', '', '', '', '', '', '', '','C','B','B', '' UNIONSelect '', '', '', '', '', '', '', '', '', '', '','A'GoSelect ID, ColA = Case 'A' When C1 then C1 When C2 then C2 When C3 then C3 When C4 then C4 When C5 then C5 When C6 then C6 When C7 then C7 When C8 then C8 When C9 then C9 When C10 then C10 When C11 then C11 When C12 then C12 endFROM FoobarWhere ( Case 'A' When C1 then C1 When C2 then C2 When C3 then C3 When C4 then C4 When C5 then C5 When C6 then C6 When C7 then C7 When C8 then C8 When C9 then C9 When C10 then C10 When C11 then C11 When C12 then C12 end) = 'A' This does what is required, but there has to be an easier way to return the below? ID ColA1 A 2 A 3 A 5 A 6 A 7 A 9 A 10 A 11 A Any ideas? The actual query I inherited to rewrite is much more complicated, but this column selection method is used on 4 columns, with 4 different criteria, against 3 datasets...looking to simplify it if possible...(this isn't so bad, just trying to see if there is a better way) Poor planning on your part does not constitute an emergency on my part. |
|
malpashaa
Constraint Violating Yak Guru
264 Posts |
Posted - 2010-12-02 : 22:06:58
|
Try this:SELECT ID, CASE WHEN LEN(C) > LEN(REPLACE(C, 'A', '')) THEN 'A' END AS ColA FROM (SELECT ID, ISNULL(C1, '') + ISNULL(C2, '') + ISNULL(C3, '') + ISNULL(C4, '') + ISNULL(C5, '') + ISNULL(C6, '') + ISNULL(C7, '') + ISNULL(C8, '') + ISNULL(C9, '') + ISNULL(C10, '') + ISNULL(C11, '') + ISNULL(C12, '') AS C FROM dbo.foobar) AS T WHERE LEN(C) > LEN(REPLACE(C, 'A', '')); |
|
|
dataguru1971
Master Smack Fu Yak Hacker
1464 Posts |
Posted - 2010-12-02 : 22:31:27
|
Thanks! Hadn't thought of just concatonating the columns. I will try it out with the actual data..here is part two..which complicates. your suggesting will help me clean up colA, but ColB is similar, although it returns the value from a corresponding tandem columns. In reality there are 16 of each, but this shows the principal.Note in this case colC1-6 and cols D1-6. If C? = 'A' then return D? where ? is the numerical incremental of the column as below..not sure if the isnull bit will work but welcome any thoughts.Create table foobar (ID int identity(1,1),C1 char(1) null,C2 char(1) null,C3 char(1) null,C4 char(1) null,C5 char(1) null,C6 char(1) null,D1 char(1) null,D2 char(1) null,D3 char(1) null,D4 char(1) null,D5 char(1) null,D6 char(1) null)Insert into foobarSelect 'A','A', '', '', '', '', 'X','', 'S', '', '', '' UNIONSelect 'B','A','A', '', '', '', 'X','S','', '', '', '' UNIONSelect 'C','B','B', 'A','', '', '', '', 'T','X', '', '' UNIONSelect '', '', '', 'A','', '', '', '', '', 'T', '', '' UNIONSelect '', '', '', '','A','A', '', '', '', '', 'X', '' UNIONSelect '', '', '', '','B','A','A', '', '', '', '', 'X' UNIONSelect '', '', '', '','C','B','B', '', '', '', '', '' UNIONSelect '', '', '', '', '', '', '','', '', '', '', '' UNIONSelect '', '', 'A','', '', '', '', '','X','', '', '' UNIONSelect '', '', '', 'A','', '', '', '','','X','', '' UNIONSelect '', '', '', '', 'A','', '', '','','','X', '' UNIONSelect '', '', 'B','', 'C','A','', '', '', '', '','Z'Select * FROM FoobarGoSelect ID ,ColA = Case 'A' When C1 then C1 When C2 then C2 When C3 then C3 When C4 then C4 When C5 then C5 When C6 then C6 end,ColB = Case 'A' When C1 then D1 When C2 then D2 When C3 then D3 When C4 then D4 When C5 then D5 When C6 then D6 endFROM FoobarWhere (Case 'A' When C1 then C1 When C2 then C2 When C3 then C3 When C4 then C4 When C5 then C5 When C6 then C6 end) = 'A' Drop table foobar/*resultsID ColA ColB2 A X3 A X4 A X6 A T7 A X8 A X9 A Z10 A X11 A S12 A X */ Poor planning on your part does not constitute an emergency on my part. |
|
|
malpashaa
Constraint Violating Yak Guru
264 Posts |
Posted - 2010-12-03 : 06:38:47
|
Try this:SELECT ID, CASE WHEN CHARINDEX('A', C) > 0 THEN 'A' END AS ColA, CASE WHEN CHARINDEX('A', C) > 0 THEN SUBSTRING(D, CHARINDEX('A', C), 1) END AS ColB FROM (SELECT ID, ISNULL(C1, '') + ISNULL(C2, '') + ISNULL(C3, '') + ISNULL(C4, '') + ISNULL(C5, '') + ISNULL(C6, '') AS C, ISNULL(D1, '') + ISNULL(D2, '') + ISNULL(D3, '') + ISNULL(D4, '') + ISNULL(D5, '') + ISNULL(D6, '') AS D FROM dbo.foobar) AS T WHERE CHARINDEX('A', C) > 0; |
|
|
|
|
|