| Author |
Topic |
|
djorre
Yak Posting Veteran
94 Posts |
Posted - 2009-05-29 : 09:11:15
|
| Hi friends,I could use some help creating a query. The source table is like this:Create Table Batch (BatchID as integer, Strategy as varchar (100), Description as varchar(100), Value as varchar (100))Insert into Batch Values(1000001, 'Startmixing', NULL, NULL)Insert into Batch Values(1000001, NULL, 'pump', '25%')Insert into Batch Values(1000001, NULL, 'mixer', '20%')Insert into Batch Values(1000002, NULL, 'pump', '50%')Insert into Batch Values(1000002, NULL, 'valve1', '30%')Insert into Batch Values(1000002, NULL, 'speed', '10m/s')Insert into Batch Values(1000002, NULL, 'amount', '1000kg')And I want this out:BatchID, Operation, Variables(100001, Startmixing, NULL)100001, Startmixing, pump 25%100001, Startmixing, mixer 20%100002, No_Operation, pump 50%100002, No_Operation, valve1 30%100002, No_Operation, speed 10m/s100002, No_Operation, amount 1000kgIt could also be simplified to this:startabel1 operation11 Null2 Null 2 Nullquery1 operation11 operation12 Null2 NullI can't solve it yet, anyone suggestions please?Grtz Djorre |
|
|
degraft
Starting Member
10 Posts |
Posted - 2009-05-29 : 09:38:32
|
| Hi djorre,Your query should look like this:Select BatchID,COALESCE(Strategy,'No_Operation'),Description,ValueFrom BatchThink this should do.RegardsDegraftDD |
 |
|
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2009-05-29 : 09:46:49
|
| Hi try thisCreate Table #Batch (BatchID integer, Strategy varchar (100), [Description] varchar(100), [Value] varchar (100))goInsert into #Batch Values(1000001, 'Startmixing', NULL, NULL)Insert into #Batch Values(1000001, NULL, 'pump', '25%')Insert into #Batch Values(1000001, NULL, 'mixer', '20%')Insert into #Batch Values(1000002, NULL, 'pump', '50%')Insert into #Batch Values(1000002, NULL, 'valve1', '30%')Insert into #Batch Values(1000002, NULL, 'speed', '10m/s')Insert into #Batch Values(1000002, NULL, 'amount', '1000kg')--select * from #batchselect b.batchid, isnull(bb.Operation,'No_Operation'), [Description]+' '+[Value] as Variablesfrom #batch binner join( select batchid,max(strategy) as Operationfrom #batchgroup by batchid) as bb on bb.batchid = b.batchid |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-05-30 : 03:00:51
|
| can there be a case like this?Insert into Batch Values(1000001, 'Startmixing', NULL, NULL)Insert into Batch Values(1000001, NULL, 'pump', '25%')Insert into Batch Values(1000001, 'Some other string', 'mixer', '20%')if yes, in such case what should be your output? |
 |
|
|
|
|
|