SQL 加价生产 table 个蛋糕

SQL adding prices to produce table of cakes

我真的希望有人能帮我解决这个 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

好的, 所以你想获得按大小分组的所有口味的总价, 总价是每种口味的价格+馅料的价格+糖衣的价格,对吗?

选项 1。 如果大小是静态的并且永远不会改变, 你可以这样做

select Flavor, SUM(aPrice) as aPrice, SUM(bPrice) as bPrice, SUM(cPrice) as cPrice, SUM(dPrice) as dPrice
from (
    select c.Name_en as Flavor, 
        ISNULL(b.Price, 0) + ISNULL(c.Price, 0) + ISNULL(d.Price, 0) +  ISNULL(f.Price, 0) as aPrice,
        0 as bPrice, 0 as cPrice, 0 as dPrice
    from cake a 
        Left JOIN CakeSize b ON a.SizeId = b.SizeId
        Left JOIN CakeFlavor c ON a.FlavorId = c.FlavorId
        Left JOIN CakeFilling d ON a.FillingId = d.FillingId
        Left JOIN CakeIcing f ON a.IcingId = f.IcingId
    where b.sizeid = '1'

    UNION ALL

    select c.Name_en as Flavor, 0 as aPrice,
        ISNULL(b.Price, 0) + ISNULL(c.Price, 0) + ISNULL(d.Price, 0) +  ISNULL(f.Price, 0) as bPrice, 
        0 as cPrice, 0 as dPrice
    from cake a 
        Left JOIN CakeSize b ON a.SizeId = b.SizeId
        Left JOIN CakeFlavor c ON a.FlavorId = c.FlavorId
        Left JOIN CakeFilling d ON a.FillingId = d.FillingId
        Left JOIN CakeIcing f ON a.IcingId = f.IcingId
    where b.sizeid = '2'

    UNION ALL

    select c.Name_en as Flavor, 0 as aPrice, 0 as bPrice,
        ISNULL(b.Price, 0) + ISNULL(c.Price, 0) + ISNULL(d.Price, 0) +  ISNULL(f.Price, 0) as cPrice,
        0 as dPrice
    from cake a 
        Left JOIN CakeSize b ON a.SizeId = b.SizeId
        Left JOIN CakeFlavor c ON a.FlavorId = c.FlavorId
        Left JOIN CakeFilling d ON a.FillingId = d.FillingId
        Left JOIN CakeIcing f ON a.IcingId = f.IcingId
    where b.sizeid = '3'

    UNION ALL

    select c.Name_en as Flavor, 0 as aPrice, 0 as bPrice, 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 JOIN CakeSize b ON a.SizeId = b.SizeId
        Left JOIN CakeFlavor c ON a.FlavorId = c.FlavorId
        Left JOIN CakeFilling d ON a.FillingId = d.FillingId
        Left JOIN CakeIcing f ON a.IcingId = f.IcingId
    where b.sizeid = '4'        
) a
where flavor is not null
group by Flavor

选项 2。 如果大小是动态的,你可以在列和行之间转置...... 你可以在这里学习:Simple way to transpose columns and rows in Sql?

已编辑:在第三个和第四个 select 之间添加 'UNION ALL',感谢 Sam Ax 编辑:在第二个查询中添加逗号 编辑:添加 "where flavor is not null" 以避免 null flavor

我可以完成这个,但我认为必须有更好的方法it.so我只是把它放在这里作为备用答案

首先,我们现在可以创建一个 table 来存储您 table 的数据并添加一个尺寸列:

 SELECT 
  c.Name_en as Flavor,
  b.SizeId,
   ISNULL(b.Price,0)+ISNULL(c.Price,0)+ISNULL(d.Price,0)+ISNULL(f.Price,0) as Price -- do the price 4 times is meaningless so i cut them out just keep 1 left
 INTO #cakeP --i saw your tag as sqlserver so i create temp table like this
  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

然后我们必须做 1 件蠢事:

    select 
       coalesce(a.Flavor,b.Flavor,c.Flavor,d.Flavor) as Flavor,
       a.price as aPrice,
       b.price as bPrice,
       c.price as cPrice,
       d.price as dPrice
       from 
         (select Flavor,size,price from #cakeP where SizeId=1) a
       full join (select Flavor,size,price from #cakeP where SizeId=2) b
            on a.Flavor = b.Flavor
       full join (select Flavor,size,price from #cakeP where SizeId=3) c
            on a.Flavor = c.Flavor
       full join (select Flavor,size,price from #cakeP where SizeId=4) d
            on a.Flavor = d.Flavor

它会完成的。

注:

  1. 如果 Flavor 没有大小 1 那么这将被搞砸 -- 好吧,我找到了解决这个问题的方法使用完全连接!
  2. Flavor 的大小为 1 就可以了,但会发生这种情况:

    exp:风味巧克力尺寸 1、3、4。那么数据将是这样的:chocolate 10 null 30 40

希望对您有所帮助