sql server - How would I pivot this type of data in T-SQL? -


i have query generates results such this

 id | column2 | sentence ----|---------|--------- 1   | sameval |this sentence 1   | sameval |this unique sentence 1   | sameval |a third unique sentence 2   | sameval |this sentence 2   | sameval |this unique sentence 2   | sameval |a third unique sentence 3   | sameval |this sentence 3   | sameval |this unique sentence 3   | sameval |a third unique sentence 

is possible pivot data somehow results so

 id | column2 | sentence ----|---------|--------- 1   | sameval |this sentence 2   | sameval |this unique sentence 3   | sameval |a third unique sentence 

query below:

select distinct      t1.id, t2.department, t1.reportmonth, t2.supplier,     t1.repid, t1.customerid, t4.sentence      table1 t1 join      table2 t2 on t2.id = t1.id join      table3 rt on rt.lookupcd = t1.programcd join      table4 t4 on t4.customerid = t1.customerid left join      table5 t5 on t1.customerid = t5.customerid     t1.code = 'ax' ,     t2.program = 'qx' ,     t1.date = '7/7/12' 

you still can try distinct or group by:

select distinct * (     select t1.id,t2.department,t1.reportmonth,t2.supplier,t1.repid,t1.customerid,t4.sentence     table1 t1     join table2 t2 on t2.id = t1.id     join table3 rt on rt.lookupcd = t1.programcd     join table4 t4 on t4.customerid = t1.customerid     left join table5 t5 on t1.customerid = t5.customerid             t1.code = 'ax' ,         t2.program = 'qx' ,         t1.date = '7/7/12' ) t 

or

select t1.id,t2.department,t1.reportmonth,t2.supplier,t1.repid,t1.customerid,t4.sentence table1 t1 join table2 t2 on t2.id = t1.id join table3 rt on rt.lookupcd = t1.programcd join table4 t4 on t4.customerid = t1.customerid left join table5 t5 on t1.customerid = t5.customerid     t1.code = 'ax' ,     t2.program = 'qx' ,     t1.date = '7/7/12' group t1.id,t2.department,t1.reportmonth,t2.supplier,t1.repid,t1.customerid,t4.sentence 

Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -