【发布时间】:2014-08-27 12:53:14
【问题描述】:
到目前为止,我已经编写了非常简单的查询,因此我现在正在寻找帮助来编写 SQL 语句,以便它将表中的两个单独的句点结束行合并为一行。这些行基本上可以通过它们的 PId、Region、Market、Code、Source 进行匹配。例如-
如果第一行是:
Id Region Market CODE Source Period_End Amt Pct
100 CAN CABLE V1 SA 20120930 100.00 0.2
第二行是:
Id Region Market CODE Source Period_End Amt Pct
100 CAN CABLE V1 SA 20121231 200.00 0.5
那么 SQL 应该返回这个结果:
Id Region Market CODE Source Period_End_1 Amt_1 Pct_1 Period_End_2 Amt_2 Pct_2
100 CAN CABLE V1 SA 20120930 100.00 0.2 20121231 200.00 0.5
非常感谢您的帮助。
安娜。
感谢您的回复。这是我开始的,但我不确定我的方向是否正确。我还注意到,由于我会根据 Period End 在行中添加越来越多的信息,因此下面的查询将太长,并且每个选择中都有多余的“案例条件”。
select
A.id , A.region, A.market, A.code, A.source ,
case when period_end = @day_id1 then period_end else '' end as Period_End_1,
case when period_end = @day_id2 then period_end else '' end as Period_End_2,
case when period_end = @day_id1 then Amt else 0.0 end as Amt_1,
case when period_end = @day_id2 then Amt else 0.0 end as Amt_2,
case when period_end = @day_id1 then Pct else 0.0 end as Pct_1,
case when period_end = @day_id2 then pct else 0.0 end as Pct_2,
from
products A with (nolock)
where
A.product_id in (select product_id from #products) -- temp table holding multiple Ids
【问题讨论】:
-
您说您正在寻求帮助,但您没有给我们任何帮助,除非我们只是为您编写查询。您是否有一些现有的代码或错误消息要分享,我们可以帮助修复?
标签: sql sql-server-2008