SQL使用三个表选择列,SUM和ORDER BY

时间:2014-05-22 09:51:44

标签: mysql sql

Stocks
Id        size   qty
100       90     80
123       180    100
100       90     100
100       180    10 

Prices
Id        size   price     priceDt
100       90     100       2014-05-10 19:00:00
123       180    150       2014-05-22 19:10:19
100       180    180       2014-05-20 19:10:19
100       90     120       2014-05-22 19:10:19

Sales
Id        size   qtySold   
100       90     15
100       90     5
123       180    10

现在我需要检索最新价格和数量可用(Sum(Stocks.qty) - Sum(Sales.qtySold)) 在Id&大小

因此Id ='100'& size = '90' 目前的价格 是120

可用数量 是160

4 个答案:

答案 0 :(得分:2)

获取最新价格日期并加入价格的子查询: -

SELECT stocks.id, stocks.size, prices.price, SUM(stocks.qty) - sales.qtySold   
FROM stocks
INNER JOIN
(
    SELECT id, size, MAX(priceDT) AS MaxPriceDate
    FROM prices
    GROP BY id, size
) Sub1
ON stocks.id = Sub1.id AND stocks.size = Sub1.size
INNER JOIN prices
ON Sub1.id = prices.id AND Sub1.size = prices.size AND Sub1.MaxPriceDate = prices.priceDT
INNER JOIN sales
ON stocks.id = sales.id AND stocks.size = sales.size
GROUP BY stocks.id, stocks.size

我担心的是,每个ID /尺寸的销售都有多行

编辑 - 使用额外的子查询来处理id / size的销售中的多行: -

SELECT stocks.id, stocks.size, prices.price, SUM(stocks.qty) - Sub2.tot_qtySold   
FROM stocks
INNER JOIN
(
    SELECT id, size, MAX(priceDT) AS MaxPriceDate
    FROM prices
    GROUP BY id, size
) Sub1
ON stocks.id = Sub1.id AND stocks.size = Sub1.size
INNER JOIN prices
ON Sub1.id = prices.id AND Sub1.size = prices.size AND Sub1.MaxPriceDate = prices.priceDT
INNER JOIN
(
    SELECT id, size, SUM(qtySold) AS tot_qtySold
    FROM sales
    GROUP BY id, size
) Sub2
ON stocks.id = Sub2.id AND stocks.size = Sub2.size
GROUP BY stocks.id, stocks.size

ON sqlfiddle: -

http://www.sqlfiddle.com/#!2/f7d37/2

编辑 - 回答评论中发布的问题: -

原因是股票表上有2个匹配的记录。

因此,对于品牌100和90的大小,这些来自股票的2条记录: -

brandId size    qtyArr
(100 ,  90   ,  10),
(100 ,  90   ,  100),

这个来自销售: -

brandId size    qtySold
(100,   90, 35),

因此,MySQL将构建最初包含一组2行的表。第一行将包含stock的第一行和sales中唯一匹配的行。第二行将有股票的第二行和(再次来自销售的匹配行)。

brandId size    qtyArr  brandId size    qtySold
(100,   90, 10, 100,    90, 35),
(100,   90, 100,    100,    90, 35),

然后它执行qtySold的SUM,但数量被计算两次(即,对于每个股票的匹配记录一次)。

要解决此问题,可能需要使用子查询来获取每个品牌/尺寸的总qtysold,然后将该子查询的结果与库存表相结合

SELECT SUM(s.qtyArr), SUM(l.qtySold) 
FROM stocks s 
INNER join 
(
    SELECT brandId, size, sum(l.qtySold)
    FROM sales
    GROUP BY brandId, size
) l 
ON l.brandId = s.brandId
AND l.size = s.size
WHERE s.brandId='100' AND s.size='90';

答案 1 :(得分:1)

我认为这就是你所需要的

SELECT b.id, sum(b.qty), sum(b.qty)-s.qtysold, max(a.price )
FROM stocks b INNER JOIN (select * from prices
                          where pricedt in(
                            select max(priceDT) as priceDt 
                          from prices
                          group by id, size))a ON (a.Id = b.Id 
and b.size=a.size)
inner join sales s on (b.id=s.id and b.size=s.size)
group by b.id, b.size
ORDER BY a.priceDt

Fiddle

答案 2 :(得分:0)

SELECT p.id, p.price, (Sum(s.qty) - sa.qtySold)  
FROM prices p inner join stocks s on p.id=s.id 
             inner join sales sa on p.id=sa.id 
WHERE id=100 and size=90 
ORDER BY pricedt ASC LIMIT 1 

答案 3 :(得分:0)

我刺伤了它:

   select   p.id, p.size, x.qty - y.qty as Stock, p.price
   from (
     (select *
     from prices )  p
    inner join (
            select id, Size, Max(pricedt) as  pricedt
            from prices 
            group by id, Size)q  
    on  p.pricedt   = q.pricedt  and p.id = q.id and p.size = q.size
    inner join 
            (
            select SUM(qty) as qty, size, id
            from stocks
            group by size, id   ) x 
    on p.id = x.id and p.size = x.size
    inner join 
            (
            select SUM(qtysold) as qty, size, id
            from sales
            group by size, id ) y 
    on x.id = y.id and x.size = y.size
)