mySQL:在事务表中查找每日统计信息的查询/ sql解决方案

时间:2014-02-02 15:16:21

标签: mysql sql

我的“交易”表包含以下列: transactionDateTime,数量,价格,客户名称,卖家名称,交易类型(卖/买) ....

现在我在网页(php)的表格中列出所有这些交易。我想在每个交易类型(买入或卖出)中添加每个'卖家'的每日摘要。所以至少我正在寻找将使用表格进行汇总的查询,最多(不确定是否可能)一个查询与我之前做同样的平面列表加上摘要结果一旦transactionDateTime变为新的一天。

提前谢谢。

交易表样本:

  

transactionDateTime,数量,价格,clientName,stationID,   transactionType,seller,typeID

  • '2014-01-04 05:44:02','2630','150.00','ClientA','60014917','买','卖家A','2399'
  • '2014-01-04 05:41:02','2630','50 .00','ClientB','60014914','买','卖家A','2398'
  • '2014-01-04 04:23:45','2','1900000.00','ClientC','60014056','sell','SellerA','27918'
  • '2014-01-04 01:08:33','3','2949499.99','ClientD','60014056','sell','SellerB','31119'

  • '2014-01-01 15:57:35','1','168999999.99','ClientE','60014917','sell','SellerA','19281'

  • '2014-01-01 13:00:18','1','38999999.00','ClientF','60014917','sell','SellerB','14206'

现在我在网页上列出几乎未经处理的行数据。 我想要的应该看起来像每天的相同列表+摘要。像:

  • 摘要'2014-01-04':卖家A,买=(2630 * 150 + 2630 * 50)
  • 摘要'2014-01-04':SellerA,Sell =(2 * 1900000)
  • 摘要'2014-01-04':卖方B,卖=(3 * 2949499.99)
  • '2014-01-04 05:44:02','2630','150.00','ClientA','60014917','买','卖家A','2399'
  • '2014-01-04 05:41:02','2630','50 .00','ClientB','60014914','买','卖家A','2398'
  • '2014-01-04 04:23:45','2','1900000.00','ClientC','60014056','sell','SellerA','27918'
  • '2014-01-04 01:08:33','3','2949499.99','ClientD','60014056','sell','SellerB','31119'
  • 摘要'2014-01-01':SellerA,Sell =(1 * 168999999.99)
  • 摘要'2014-01-01':卖方B,卖=(1 * 38999999.00)
  • '2014-01-01 15:57:35','1','168999999.99','ClientE','60014917','sell','SellerA','19281'
  • '2014-01-01 13:00:18','1','38999999.00','ClientF','60014917','sell','SellerB','14206'

甚至可以在SQL查询中使用吗?或者我应该通过编程方法来做到这一点?

1 个答案:

答案 0 :(得分:0)

这样的事情可以解决问题:

/* Your details. */
select 'DETAIL' type_of_record
,      1 sorting_order
,      something_to_order_details_on sorting_order_detail
,      transactionType
,      sellerName
,      transactionDateTime
,      quantity
,      price
from   transactions
union
all
/* Your totals. */
select 'DAILY' type_of_record
,      2 sorting_order
,      null sorting_order_detail
,      transactionType
,      sellerName
,      trunc(transactionDateTime) /* Or whatever MySQL uses to round dates. */
,      sum(quantity) quantity_sum
,      sum(price) price_sum
from   transactions
where  transactionDateTime < trunc(now)
order
by     sorting_order
,      sorting_order_detail