0

我有下表:

$ sqlite3 :memory:
BEGIN TRANSACTION;
CREATE TABLE trades (date, stock, buyer, seller, quantity);
INSERT INTO "trades" VALUES(1,'X','A','B',10);
INSERT INTO "trades" VALUES(1,'X','A','C',10);
INSERT INTO "trades" VALUES(1,'Y','C','A',20);
INSERT INTO "trades" VALUES(2,'X','B','A',10);
INSERT INTO "trades" VALUES(2,'Y','B','A',10);
INSERT INTO "trades" VALUES(2,'Z','D','E',11);
COMMIT;

select * from trades;
date        stock       buyer       seller      quantity  
----------  ----------  ----------  ----------  ----------
1           X           A           B           10        
1           X           A           C           10        
1           Y           C           A           20        
2           X           B           A           10        
2           Y           B           A           10        
2           Z           D           E           11        

我希望为每一天和每只股票做一个总结,包含每个经纪人买入和卖出股票的总量,即每个经纪人对每只股票和每一天的汇总活动。我正在寻找的输出是这样的:

select ...magic statement ... from trades;
date        stock       broker      bought      sold
----------  ----------  ----------  ----------  ----------
1           X           A           20          0
1           X           B           0           10
1           X           C           0           10
1           Y           A           0           20
1           Y           B           0           0
1           Y           C           20          0
2           X           A           0           10
2           X           B           10          0
2           X           C           0           0
2           Y           A           0           10
2           Y           B           10          0
2           Y           C           0           0

股票列表和经纪人名称不是固定的,即它们必须从表本身中检索(因此不能在 sql select 语句中“硬编码”)。

这可以在单个(SQLite)SQL 语句中实现吗?

4

2 回答 2

1

我有点像使用两个子查询和一个联合作弊,但是你去吧。

SELECT date, stock, broker, SUM(bought) bought, SUM(sold) sold FROM (
  SELECT date, stock, buyer broker, SUM(quantity) bought, 0 sold FROM trades GROUP BY date, stock, buyer
  UNION
  SELECT date, stock, seller broker, 0 as bought, SUM(quantity) FROM trades GROUP BY date, stock, seller
) GROUP BY date, stock, broker;

结果:

date        stock       broker      bought      sold      
----------  ----------  ----------  ----------  ----------
1           X           A           20          0         
1           X           B           0           10        
1           X           C           0           10        
1           Y           A           0           20        
1           Y           C           20          0         
2           X           A           0           10        
2           X           B           10          0         
2           Y           A           0           10        
2           Y           B           10          0         
2           Z           D           11          0         
2           Z           E           0           11       

编辑:我刚刚看到您想列出所有内容,包括。所有经纪人全天,即使他们没有对任何股票进行任何交易。所以这里是完整的总结:

SELECT date, stock, broker, SUM(bought) bought, SUM(sold) sold FROM (
  SELECT date, stock, buyer broker, SUM(quantity) bought, 0 sold FROM trades GROUP BY date, stock, buyer
  UNION
  SELECT date, stock, seller broker, 0 as bought, SUM(quantity) FROM trades GROUP BY date, stock, seller
  UNION
  SELECT DISTINCT d.date, s.stock, b.broker, 0 bought, 0 sold FROM (SELECT DISTINCT date FROM trades) d, (SELECT DISTINCT stock FROM trades) s, (SELECT DISTINCT seller broker FROM trades UNION SELECT DISTINCT buyer broker FROM trades) b
) GROUP BY date, stock, broker;

结果:

date        stock       broker      bought      sold      
----------  ----------  ----------  ----------  ----------
1           X           A           20          0         
1           X           B           0           10        
1           X           C           0           10        
1           X           D           0           0         
1           X           E           0           0         
1           Y           A           0           20        
1           Y           B           0           0         
1           Y           C           20          0         
1           Y           D           0           0         
1           Y           E           0           0         
1           Z           A           0           0         
1           Z           B           0           0         
1           Z           C           0           0         
1           Z           D           0           0         
1           Z           E           0           0         
2           X           A           0           10        
2           X           B           10          0         
2           X           C           0           0         
2           X           D           0           0         
2           X           E           0           0         
2           Y           A           0           10        
2           Y           B           10          0         
2           Y           C           0           0         
2           Y           D           0           0         
2           Y           E           0           0         
2           Z           A           0           0         
2           Z           B           0           0         
2           Z           C           0           0         
2           Z           D           11          0         
2           Z           E           0           11      

让我们看一下查询:

SELECT date, stock, buyer broker, SUM(quantity) bought, 0 sold
FROM trades
GROUP BY date, stock, buyer

仅列出每个(分组)日期、股票、买家的购买总和。我们还需要有一个已售出的列,以便UNIONs 可以工作,所以只需制作一个具有恒定值的列0。与销售相同。

SELECT DISTINCT d.date, s.stock, b.broker, 0 bought, 0 sold
FROM
 (SELECT DISTINCT date FROM trades) d,
 (SELECT DISTINCT stock FROM trades) s,
 (SELECT DISTINCT seller broker FROM trades
   UNION SELECT DISTINCT buyer broker FROM trades) b

这个很恶心 它将(在子查询中选择)所有日期、股票和经纪人连接在一起,并为它们分配不买不卖。这只是为了获得完整的表格,即这些行只是填充物。

SELECT date, stock, broker, SUM(bought) bought, SUM(sold) sold
FROM ( ... )
GROUP BY date, stock, broker;

子查询将生成很多行。这个顶级查询只是聚合数据。

于 2013-08-18T14:40:42.820 回答
0

我认为您可以在当前表格中添加一些列,计算摘要并每天填写它们。然后你可以使用 SELECT 语句。

于 2013-08-18T14:17:25.463 回答