好的,所以我有一张桌子让我们称之为A(这里有更多的项目):
ITEMID NAME
10001 Boat
我有另一张桌子叫它B(这里有更多的项目):
Itemid Price Pricetype Dimension Valid_from Valid_To
10001 10 1 Allblank 2014-10-10 2014-10-25
10001 5 2 200 2014-10-09 2014-10-20
10001 99 2 200 2014-10-08 2014-10-10
10001 20 1 Allblank 2014-10-08 2014-10-10
10001 22 2 500 2014-10-10 2014-10-19
价格类型决定了商品在哪里销售或者没有,所以我想列出其尺寸的常规价格和尺寸的销售价格,以便表格看起来像这样(假设1是正常价格,2是销售价格):
Itemid Itemname RegularPrice DiscountPrice200 DiscountPrice500
10001 boat 10 5 22
我已经连接了内部表,但我无法正确管理字段,case语句在内部联接上返回了大量重复项。我希望自己清楚明白:)
我的查询:
SELECT a.itemid,
case when b.inventdimid = '00000101_090' and (b.labelissue ='1' or b.labelissue = '2' and (b.todate > getdate() or b.todate ='1900-01-01 00:00:00.000') ) then b.amount else null end as Price500
FROM a
inner join b
on a.itemid = b.itemrelation
答案 0 :(得分:0)
尝试此查询:
SELECT b.itemid,
a.name,
b.price,
b200.price,
b500.price
FROM tb b
JOIN ta a
ON b.itemid = a.itemid
JOIN tb b200
ON b.itemid = b200.itemid
AND b200.valid_to > getdate()
AND b200.pricetype = 2
AND b200.dimension = 200
JOIN tb b500
ON b.itemid = b500.itemid
AND b500.valid_to > getdate()
AND b500.pricetype = 2
AND b500.dimension = 500
WHERE b.valid_to > getdate()
AND b.pricetype = 1