MySQL sum()在不同的group bys上

时间:2011-03-09 15:48:33

标签: mysql

好的,我对两个表有查询。我需要得到两笔钱。我执行group by,以便sum()正常工作。

SELECT sum(a.x), sum(b.y) FROM a,b GROUP BY a.n where a.n=b.m

到目前为止,这种方法效果很好,但问题是我需要对第二个总和(sum(b.y))进行不同的分组,而不是第一个总和(sum(a.x))。

真正的查询有点复杂,但这是我的主要问题。

这就是我实际尝试选择的sum(stock.amount) - if( sold.amount IS NULL , 0, sum( sold.amount ) )

如何在一个查询中解决这个问题?

3 个答案:

答案 0 :(得分:1)

既然你没有写下表格,我会猜测并假设表格如下:

股票:id,item_id,金额 已售出:id,item_id,金额

然后我再次假设您需要stock_in_total,sold_total,left_total计数

SELECT 
  stock_sums.item_id, 
  stock_sums.st_sum as stock_in_total,
  COALESCE(sold_sums.so_sum,0) as sold_total,
  (stock_sums.st_sum - COALESCE(sold_sums.so_sum,0)) as left_total
FROM (
  SELECT stock.item_id as item_id, SUM(stock.amount) as st_sum 
  FROM stock 
  GROUP BY item_id
) as stock_sums 
LEFT JOIN (
  SELECT sold.item_id as item_id, SUM(sold.amount) as so_sum 
  FROM sold
  GROUP by item_id
) as sold_sums ON stock_sums.item_id = sold_sums.item_id

我希望这会有所帮助。

答案 1 :(得分:1)

我将如何做到这一点。我假设Stock是主表,带有ID和金额,而Sold通过ID值映射到Stock,并且每个Stock项目有0到多个记录。

SELECT Q1.id, Q1.Total1, Q2.Total2
, Q1.Total1 - COALESCE(Q2.Total2,0) as Outstanding
FROM ( 
    SELECT id, SUM(amount) as Total1
        FROM Stock GROUP BY id
) as Q1
LEFT OUTER JOIN (
        SELECT id, SUM(Amount) as Total2
        FROM Sold GROUP BY id
) as Q2
  ON Q2.id = Q1.id

请注意,简单地将SQL格式化为干净的方式会强制您将其分解为逻辑部分,并且通常会明确显示查询的错误。 上面的示例也正确处理Sold表中不匹配的情况。

干杯, 丹尼尔

(代码假设)

DROP TABLE Stock
CREATE TABLE Stock (
 id integer
, amount decimal(10,2)
) 

INSERT INTO Stock (id, amount ) VALUES ( 1, 10.1);
INSERT INTO Stock (id, amount ) VALUES ( 2, 20.2);
INSERT INTO Stock (id, amount ) VALUES ( 3, 30.3);

SELECT * FROM STOCK

DROP TABLE Sold
CREATE TABLE Sold (
 id integer
, amount decimal(10,2)
) 

INSERT INTO Sold (id, amount ) VALUES ( 1, 1.1);
INSERT INTO Sold (id, amount ) VALUES ( 1, 2.2);
INSERT INTO Sold (id, amount ) VALUES ( 1, 3.3);
INSERT INTO Sold (id, amount ) VALUES ( 2, 2.22);

SELECT * FROM Sold


SELECT Q1.id, Q1.Total1, Q2.Total2
, Q1.Total1 - COALESCE(Q2.Total2,0) as Outstanding
FROM ( 
    SELECT id, SUM(amount) as Total1
    FROM Stock GROUP BY id
) as Q1
LEFT OUTER JOIN (
    SELECT id, SUM(Amount) as Total2
    FROM Sold GROUP BY id
) as Q2
  ON Q2.id = Q1.id

结果:

id  Total1  Total2  Outstanding
1   10.10   6.60    3.50
2   20.20   2.22    17.98
3   30.30       30.30

答案 2 :(得分:0)

<强> REVISION

听起来您希望每种不同库存的库存总量为一个。然后,您需要根据已售出的库存为每种库存留下多少库存。正确的吗?

若是,请查看:

select stock, sum(a.x) as sharesBeforeSale, (sum(a.x) - sum(b.y)) as sharesAfterSale 
FROM db.table1 a, db.table2 b  
WHERE a.UNIQUEID = b.UNIQUEID AND b.y IS NOT NULL
GROUP BY a.UNIQUEID;

这是否能达到您的目的?

stock       sharesBeforeSale       sharesAfterSale
duk         100                    25
orc         101                    101
yrc          54                    41

享受!




  

示例表

db.table1 (stock owned):
UNIQUEID    x   stock
1         100     duk
2         101     orc
3          54     yrc

db.table2 (stock sold):
UNIQUEID    y   
1          75   
2           0     
3          13