我真的希望有人可以帮助我处理这个 sql 查询,一直在绞尽脑汁,但我知道这是可能的......这是我当前的查询并产生正确的格式:
DECLARE
@Price1 NVARCHAR(20),
@Price2 NVARCHAR(20),
@Price3 NVARCHAR(20),
@Price4 NVARCHAR(20)
SET @Price1 = (select Price from CakeSize where SizeId = '1')
SET @Price2 = (select Price from CakeSize where SizeId = '2')
SET @Price3 = (select Price from CakeSize where SizeId = '3')
SET @Price4 = (select Price from CakeSize where SizeId = '4')
SELECT
c.Name_en as Flavor,
@Price1 as Price1,
@Price2 as Price2,
@Price3 as Price3,
@Price4 as Price4
FROM
cake a
Left outer JOIN CakeSize b ON a.SizeId = b.SizeId
Left outer JOIN CakeFlavor c ON a.FlavorId = c.FlavorId
Left outer JOIN CakeFilling d ON a.FillingId = d.FillingId
Left outer JOIN CakeIcing f ON a.IcingId = f.IcingId
group by c.Name_en
我似乎无法从所有表格和显示中获得所有价格的总和。
我能够检索数据但不能像上面那样格式化它?
SELECT
c.Name_en as Flavor,
ISNULL(b.Price, 0) + ISNULL(c.Price, 0) + ISNULL(d.Price, 0) + ISNULL(f.Price, 0) as aPrice,
ISNULL(b.Price, 0) + ISNULL(c.Price, 0) + ISNULL(d.Price, 0) + ISNULL(f.Price, 0) as bPrice,
ISNULL(b.Price, 0) + ISNULL(c.Price, 0) + ISNULL(d.Price, 0) + ISNULL(f.Price, 0) as cPrice,
ISNULL(b.Price, 0) + ISNULL(c.Price, 0) + ISNULL(d.Price, 0) + ISNULL(f.Price, 0) as dPrice
FROM
cake a
Left Outer JOIN CakeSize b
ON a.SizeId = b.SizeId
Left Outer JOIN CakeFlavor c
ON a.FlavorId = c.FlavorId
Left Outer JOIN CakeFilling d
ON a.FillingId = d.FillingId
Left Outer JOIN CakeIcing f
ON a.IcingId = f.IcingId
我想要上面的输出,而不是巧克力蛋糕的 4 行;1 排巧克力蛋糕。(胡萝卜蛋糕比其他的少 5 美元) 正确的数据,错误的格式 列 aPrice 行 1、2、3、4 包含巧克力蛋糕的正确值。
(希望每种口味的格式如下)
巧克力 18.95 18.95 23.50 38.50