描述我想要的东西有点难。所以我只会展示我想要实现的目标,你可以告诉我它是否可能以及如何实现。
这是针对股票交易日志。
DATA:
t_type productid t_date t_stock t_stock_after
1 2 2013-11-06 16:52:30 1 80
1 3 2013-11-06 15:50:40 1 60
1 2 2013-11-06 13:52:30 1 81
1 2 2013-11-06 13:48:30 1 82
1 2 2013-11-05 13:52:30 1 83
1 2 2013-11-04 14:56:30 1 84
1 2 2013-11-04 13:55:30 2 85
1 2 2013-11-04 13:54:30 1 87
2 2 2013-11-04 13:53:30 10 88
1 2 2013-11-04 13:52:30 1 78
目标:
t_type productid t_date t_stock t_stock_after
1 2 2013-11-06 3 80
1 2 2013-11-05 1 83
1 2 2013-11-04 4 84
2 2 2013-11-04 10 88
1 2 2013-11-04 1 78
所以我想要做的只是将*(星号)分组,因为它们共享相同的日期和类型,并且它们是连续的。
t_type productid t_date t_stock t_stock_after
* 1 2 2013-11-06 16:52:30 1 80
* 1 2 2013-11-06 13:52:30 1 81
* 1 2 2013-11-06 13:48:30 1 82
1 2 2013-11-05 13:52:30 1 83
* 1 2 2013-11-04 14:56:30 1 84
* 1 2 2013-11-04 13:55:30 2 85
* 1 2 2013-11-04 13:54:30 1 87
2 2 2013-11-04 13:53:30 10 88
1 2 2013-11-04 13:52:30 1 78
更新
我也意识到了什么,t_stock_after不是SUM。它只显示最后一个t_stock_after。 样本数据已得到纠正,以模拟现实生活中的数据并帮助您找到答案。
为了尽可能地提供帮助:
这是表格:
id(int和auto increment),productid,userid,t_type,t_date,t_stock,t_stock_after,t_reason
更新2 - 工作代码
正如我所说,这是我最终创建的代码=)
SQL:
if(isset($_GET['mindate']) && $_GET['mindate']!="" && isset($_GET['maxdate']) && $_GET['maxdate']!="")
$stocklog = $dataserver->query("SELECT t_type, productid, t_date, SUM(t_stock) AS stock, SUM(t_stock_after) AS stockafter FROM store_log_transactions WHERE productid = ".$_GET['id']." AND (DATE(t_date) BETWEEN DATE_SUB('".$_GET['mindate']."', INTERVAL 1 MONTH) AND '".$_GET['maxdate']."') GROUP BY t_date, t_type");
else
$stocklog = $dataserver->query("SELECT t_type, productid, t_date, SUM(t_stock) AS stock, SUM(t_stock_after) AS stockafter FROM store_log_transactions WHERE productid = ".$_GET['id']." AND (t_date BETWEEN DATE_SUB(NOW(), INTERVAL 1 MONTH) AND NOW()) GROUP BY t_date, t_type");
PHP:
$id=1;
$temp_date = "";
$temp_type="";
$temp_stock=0;
$temp_stock_after=9999999999;
while($log = $stocklog->fetch_object())
{
$current_date = date("Y-m-d",strtotime($log->t_date));
$current_type = $log->t_type;
$current_stock = $log->stock;
$current_after = $log->stockafter;
if($current_date==$temp_date || $id==1)
{
if($current_type==$temp_type || $id==1)
{
$temp_stock+=$current_stock;
if($temp_stock_after>$current_after);
$temp_stock_after=$current_after;
$temp_date=$current_date;
$temp_type=$current_type;
}
else
{
$transactiontype;
switch($temp_type)
{
case 1: $transactiontype='net sales'; break;
case 2: $transactiontype='net return'; break;
case 3: $transactiontype='transfer in'; break;
case 4: $transactiontype='trasnfer out'; break;
}
echo '<div class="listitem"><div>'.$temp_stock.'</div><div>'.$temp_stock_after.'</div><div>'.$transactiontype.'</div><div>'.$temp_date.'</div></div>';
$temp_date=$current_date;
$temp_type=$current_type;
$temp_stock_after=$current_after;
$temp_stock=$current_stock;
}
}
else
{
$transactiontype;
switch($temp_type)
{
case 1: $transactiontype='net sales'; break;
case 2: $transactiontype='net return'; break;
case 3: $transactiontype='transfer in'; break;
case 4: $transactiontype='trasnfer out'; break;
}
echo '<div class="listitem"><div>'.$temp_stock.'</div><div>'.$temp_stock_after.'</div><div>'.$transactiontype.'</div><div>'.$temp_date.'</div></div>';
$temp_date=$current_date;
$temp_type=$current_type;
$temp_stock_after=$current_after;
$temp_stock=$current_stock;
}
$id++;
if($id>$stocklog->num_rows) {
$transactiontype;
switch($temp_type)
{
case 1: $transactiontype='net sales'; break;
case 2: $transactiontype='net return'; break;
case 3: $transactiontype='transfer in'; break;
case 4: $transactiontype='trasnfer out'; break;
}
echo '<div class="listitem"><div>'.$temp_stock.'</div><div>'.$temp_stock_after.'</div><div>'.$transactiontype.'</div><div>'.$temp_date.'</div></div>';
}
}
答案 0 :(得分:1)
你正在寻找这个...
GROUP BY t_type, productid, DATE(t_date)
答案 1 :(得分:0)
如果我理解正确你想要仅按日期和其他字段分组日期,那么总结t_stock_after。如果这是正确的,这将是非常容易的,将是
SELECT t_type, procuctid, DATE(t_date) as t_date, t_stock, SUM(t_stock_after) as t_stock_after
FROM table
GROUP BY t_type, product_id, DATE(t_date);
答案 2 :(得分:0)
所以无论你想要什么,首先关闭,你想要这个查询返回的行,对吗?...
SELECT x.*
FROM
( SELECT a.*
, COUNT(*) rank
FROM stock_transaction_log a
JOIN stock_transaction_log b
ON b.id <= a.id
GROUP
BY id
) x
LEFT
JOIN
( SELECT a.*
, COUNT(*) rank
FROM stock_transaction_log a
JOIN stock_transaction_log b
ON b.id <= a.id
GROUP
BY id
) y
ON y.t_type = x.t_type
AND y.productid = x.productid
AND DATE(y.t_date) = DATE(x.t_date)
AND y.rank = x.rank - 1
WHERE y.id IS NULL;
编辑以允许id
不是一个完整的序列。