日期:2014-05-18 浏览次数:20457 次
select qid,cid,pid,QuotePrice from quote
qid cid pid QuotePrice ----------- ----------- ----------- ----------------------------------------------------- 3 3 6 500.0 4 2 7 1000.0 5 3 8 1500.0 6 1 6 1000.0 7 2 6 8000.0 8 1 7 500.0 10 1 8 1000.0 11 2 8 5000.0 12 2 11 500.0 13 1 11 1000.0 14 3 11 1500.0 15 1 10 500.0 16 1 12 500.0 17 4 6 1111.0 18 4 7 1111.0 19 2 10 131.0 20 3 10 123.0 (所影响的行数为 17 行)
select qID,cid,pid, max(case px when 1 then cid else '' end) 'comName1', max(case px when 1 then QuotePrice else '' end) 'Price1', max(case px when 2 then cid else '' end) 'comName2', max(case px when 2 then QuotePrice else '' end) 'Price2', max(case px when 3 then cid else '' end) 'comName3', max(case px when 3 then QuotePrice else '' end) 'Price3' from (select top 1000 px=(select count(1) from quote where pid=a.pid and QuotePrice<a.QuotePrice)+1 ,* from quote as a order by pid,QuotePrice) as t group by qid,cid,pid,QuotePrice
qID cid pid comName1 Price1 comName2 Price2 comName3 Price3 --- ---- ---- -------- ------ -------- ------- -------- ------- 3 3 6 3 500.0 0 0.0 0 0.0 6 1 6 0 0.0 1 1000.0 0 0.0 17 4 6 0 0.0 0 0.0 4 1111.0 7 2 6 0 0.0 0 0.0 0 0.0 8 1 7 1 500.0 0 0.0 0 0.0 4 2 7 0 0.0 2 1000.0 0 0.0 18 4 7 0 0.0 0 0.0 4 1111.0 10 1 8 1 1000.0 0 0.0 0 0.0 5 3 8 0 0.0 3 1500.0 0 0.0 11 2 8 0 0.0 0 0.0 2 5000.0 20 3 10 3 123.0 0 0.0 0 0.0 19 2 10 0 0.0 2 131.0 0 0.0 15 1 10 0 0.0 0 0.0 1 500.0 12 2 11 2 500.0 0 0.0 0 0.0 13 1 11 0 0.0 1 1000.0 0 0.0 14 3 11 0 0.0 0 0.0 3 1500.0 16 1 12 1 500.0 0 0.0 0 0.0 (所影响的行数为 17 行)
--因为你的QID是不重复的 你GROUP BY QID 就会全部显示出来 --你这样试一下 select max(qID),cid,pid, max(case px when 1 then cid else '' end) 'comName1', max(case px when 1 then QuotePrice else '' end) 'Price1', max(case px when 2 then cid else '' end) 'comName2', max(case px when 2 then QuotePric