0

我有收据和支出的两个表,正在使用联合来合并它们并生成报告

SELECT  MIN(date) AS trx, MAX(date), COUNT(*), SUM(amount), CONCAT(' - ','-'),
    $f as _ 
FROM expenditure WHERE date >= '$start_date' AND date <= '$end_date'
    AND client_id like '{$_SESSION['client']['id']}'
GROUP BY _

UNION

SELECT  MIN(date) AS trx, MAX(date), COUNT(*),CONCAT(' - ','-'), SUM(amount),
    $f as _ 
FROM receipts
WHERE  date >= '$start_date'  AND date <= '$end_date'
   AND client_id like '{$_SESSION['client']['id']}'
GROUP BY _
ORDER BY trx

我得到以下结果

  Array ( 
          [0] => Array ( [trx] => 2012-03-06 [MAX(date)] => 2012-03-06 [COUNT(*)] => 1 [SUM(amount)] => 120000 [CONCAT(' - ','-')] => - - [_] => 2012-03-06 )
          [1] => Array ( [trx] => 2012-03-08 [MAX(date)] => 2012-03-08 [COUNT(*)] => 1 [SUM(amount)] => 120000 [CONCAT(' - ','-')] => - - [_] => 2012-03-08 )
          [2] => Array ( [trx] => 2012-06-06 [MAX(date)] => 2012-06-06 [COUNT(*)] => 2 [SUM(amount)] => 60000 [CONCAT(' - ','-')] => - - [_] => 2012-06-06 ) 
          [3] => Array ( [trx] => 2012-06-06 [MAX(date)] => 2012-06-06 [COUNT(*)] => 1 [SUM(amount)] => - - [CONCAT(' - ','-')] => 487200 [_] => 2012-06-06 )
          [4] => Array ( [trx] => 2012-06-08 [MAX(date)] => 2012-06-08 [COUNT(*)] => 1 [SUM(amount)] => 120000 [CONCAT(' - ','-')] => - - [_] => 2012-06-08 )
          [5] => Array ( [trx] => 2012-06-29 [MAX(date)] => 2012-06-29 [COUNT(*)] => 2 [SUM(amount)] => 320000 [CONCAT(' - ','-')] => - - [_] => 2012-06-29 )  )

问题 如果您注意到 3 和 4,一天有两条记录。

[trx] => 2012-06-06

因为那天客户进行了贷记和借记。有没有办法只将像 3 和 4 这样的行组合成一行。SELECT DISTINCT dindnt 要么工作,要么 mysql 可以对同一日期出现的两个表中的记录求和。

4

2 回答 2

0

您可以UNIONexpendituresreceipts表,然后从联合结果中选择。请注意,联合结果对于receipt.value和具有单独的列expenditure.value

SELECT
  MIN(date) as trx,
  MAX(date),
  COUNT(exp_amount) + COUNT(rct_amount) AS Tran_Count,
  COALESCE(SUM(exp_amount), ' - -') AS Exp_Total,
  COALESCE(SUM(rct_amount), ' - -') AS Rct_Total,
  _
FROM (
  SELECT whatever AS _, date, amount AS exp_amount, NULL AS rct_amount
    FROM expenditure
    WHERE date >= start AND date <= end AND client_id LIKE 'whatever'
  UNION SELECT whatever, date, NULL, amount
    FROM receipts
    WHERE date >= start AND date <= end AND client_id LIKE 'whatever'
) exp_rct
GROUP BY _
ORDER BY trx
于 2013-08-16T21:40:47.347 回答
0

您可以使用自定义别名的子选择来分组

$query = "
    SELECT q.* 
    FROM (
        SELECT  
            MIN(date) AS trx, 
            MAX(date) as max_date, 
            COUNT(*), 
            SUM(amount), 
            CONCAT(' - ','-'),
            $f as _ 
        FROM 
            expenditure 
        WHERE 
            date >= '$start_date'  
            AND date <= '$end_date'  
            AND client_id like '{$_SESSION['client']['id']}' 
        GROUP BY _ 
    UNION
        SELECT  
            MIN(date) AS trx, 
            MAX(date), 
            COUNT(*),
            CONCAT(' - ','-'),
            SUM(amount), 
            $f as _ 
        FROM 
            receipts 
        WHERE  
            date >= '$start_date'  
            AND  date <= '$end_date'  
            AND  client_id like '{$_SESSION['client']['id']}' 
        GROUP BY _ ORDER BY trx 
    ) q GROUP BY q.max_date
";

上面的查询将结果分组 MAX(date) as max_date

对于同一日期的金额总和,您可以修改为

$query = "
    SELECT 
        q.*, 
        SUM(q.amount) AS grand_sum 
    FROM (
        SELECT  
            MIN(date) AS trx, 
            MAX(date) as max_date, 
            COUNT(*), 
            SUM(amount) AS amount, 
            CONCAT(' - ','-'),
            $f as _ 
        FROM 
            expenditure 
        WHERE 
            date >= '$start_date'  
            AND date <= '$end_date'  
            AND client_id like '{$_SESSION['client']['id']}' 
        GROUP BY _ 
    UNION
        SELECT  
            MIN(date) AS trx, 
            MAX(date), 
            COUNT(*),
            CONCAT(' - ','-'),
            SUM(amount), 
            $f as _ 
        FROM 
            receipts 
        WHERE  
            date >= '$start_date'  
            AND  date <= '$end_date'  
            AND  client_id like '{$_SESSION['client']['id']}' 
        GROUP BY _ ORDER BY trx 
) q GROUP BY q.max_date
";

grand_sum列包含同一日期的金额总和

于 2013-08-16T21:29:18.937 回答