-1

我真的希望有人可以帮助我处理这个 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

4

2 回答 2

0

好的,所以你想得到按大小分组的所有口味的总价,总价是根据每种口味的价格 + 馅料的价格 + 糖衣的价格计算出来的,对吗?

选项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.如果大小是动态的,你可以在列和行之间转置......你可以在这里学习:在Sql中转置列和行的简单方法?

已编辑:在第三个和第四个选择之间添加“UNION ALL”,感谢 Sam Ax 已编辑:在第二个查询中添加逗号已编辑:添加“风味不为空”以避免空风味

于 2016-01-15T03:29:47.077 回答
0

我可以做到这一点,但我认为必须有更好的方法来做到这一点。所以我把它放在这里作为备用答案

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

 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

那么我们必须做一件愚蠢的事情:

    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

希望这可以帮助你

于 2016-01-15T03:57:35.027 回答