新列中的MySQL摘要

时间:2016-04-25 05:49:54

标签: mysql

我有2个表,其中包含一组数据,如下所示,我想得到结果中的结果,该结果将在字段余额中进行计算:< / p>

我被困在平衡场上,我如何让平衡运行?

tblIn

in_date    | code    | in_qty  
-----------|---------|---------
2016-04-01 | aaa     | 100
2016-04-02 | aaa     | 100
2016-04-03 | aaa     | 200
2016-04-06 | aaa     | 400

tblOut

out_date   | code    | out_qty  
-----------|---------|---------
2016-04-02 | aaa     | 100
2016-04-08 | aaa     | 400

RESULT

date       | code    | in_qty   | out_qty  | balance
-----------|---------|----------|----------|---------
2016-04-01 | aaa     | 100      | 0        | 100
2016-04-02 | aaa     | 100      | 0        | 200
2016-04-02 | aaa     | 0        | 100      | 100
2016-04-03 | aaa     | 200      | 0        | 300
2016-04-06 | aaa     | 400      | 0        | 700
2016-04-08 | aaa     | 0        | 400      | 300

QUERY (感谢@ 1000111)

SELECT 
t.*,
@prevBalance := (t.in_qty - t.out_qty) + IFNULL(@prevBalance,0) AS balance
FROM 
(
    SELECT 
        in_date date,
        code,
        in_qty,
        0 AS out_qty
    FROM tblin

    UNION 

    SELECT 
        out_date,
        code,
        0,
        out_qty
    FROM tblout
) t , (SELECT @prevBalance := NULL) var
ORDER BY t.date;

此查询将获得结果中的结果,但如果我想要这种结果,该怎么办? MySQL将作为单行汇总至2016-04-03,并继续详细说明下一个日期。

RESULT1

date       | code    | in_qty   | out_qty  | balance
-----------|---------|----------|----------|---------
2016-04-03 | aaa     | 0        | 0        | 300
2016-04-06 | aaa     | 400      | 0        | 700
2016-04-08 | aaa     | 0        | 400      | 300

3 个答案:

答案 0 :(得分:0)

SELECT 
t.*,
@prevBalance := (t.in_qty - t.out_qty) + IFNULL(@prevBalance,0) AS balance
FROM 
(
    SELECT 
        in_date in_date,
        Somedate out_date,
        code,
        in_qty,
        0 AS out_qty
    FROM tblin

    UNION 

    SELECT 
        somedate in_date,
        out_date out_date,
        code,
        0,
        out_qty
    FROM tblout
) t , (SELECT @prevBalance := NULL) var
ORDER BY t.date;

尝试使用与您使用的相同的SOMEDATE(我的意思是任何固定日期)&#34; 0&#34;在两边。 它可能有用。

答案 1 :(得分:0)

试试这个,我假设您的约会对象是 2016-04-03 ;)

SELECT
    @ROWNO:=@ROWNO+1 AS row_no,
    TMP1.date,
    TMP1.code,
    IF(@ROWNO=1,0,TMP1.in_qty)  AS in_qty,
    IF(@ROWNO=1,0,TMP1.out_qty) AS out_qty,
    TMP1.balance
FROM
    (
        SELECT
            TMP.date,
            TMP.code,
            TMP.in_qty,
            TMP.out_qty,
            @BALANCE:=@BALANCE + TMP.BALANCE AS balance
        FROM
            (
                SELECT
                    out_date AS DATE,
                    code,
                    0 AS in_qty,
                    out_qty,
                    0-out_qty AS balance
                FROM
                    tblOut
                UNION
                SELECT
                    in_date AS DATE,
                    code,
                    in_qty,
                    0      AS out_qty,
                    in_qty AS balance
                FROM
                    tblIn) TMP,
            (SELECT @BALANCE:=0) B
        ORDER BY TMP.date) TMP1,
    (SELECT @ROWNO:=0) R
WHERE
    TMP1.date >= '2016-04-03'
ORDER BY
    TMP1.DATE

答案 2 :(得分:0)

尝试此查询..我使用2016-04-02对其进行了测试,但您可以随意更改评论的一行,以便将日期更改为您喜欢的任何日期

SELECT @startDate date,
       -- all this below to determine code since code has to come from somewhere
       -- for now we'll just select code of the latest date that is before
       -- or equal to @startDate
       (SELECT TCode.code FROM
          (SELECT code,in_date FROM tblin   WHERE in_date <= @startDate
           UNION ALL
           SELECT code,out_date FROM tblout WHERE out_date <= @startDate)TCode
         ORDER BY TCode.in_date DESC
        LIMIT 1
        ) as Code,
        0,0,
        balance 
        FROM
        (SELECT @prevbalance :=(SELECT SUM(in_qty) FROM tblin,
-- Change the date in below line to any date you desire as @startDate is used throughout this whole query
                                (SELECT @startDate := DATE('2016-04-02'))TDATE WHERE in_date <= @startDate)-
                               (SELECT SUM(out_qty) FROM tblout WHERE out_date <= @startDate) as balance)T4
UNION ALL
SELECT * FROM
  (SELECT T.*,@balance := @balance + (t.in_qty - t.out_qty) AS balance
   FROM 
    (SELECT in_date date,code,in_qty,0 AS out_qty FROM tblin WHERE in_date > @startDate
     UNION ALL
     SELECT out_date,    code,     0,     out_qty FROM tblout WHERE out_date > @startDate
    )T,(SELECT @balance:=@prevbalance)initial
   ORDER BY T.date ASC,T.in_qty DESC
  )T3;

sqlfiddle

UPDATE 要防止NULL,只需使用IFNULL()

SELECT @startDate date,
       -- all this below to determine code since code has to come from somewhere
       -- for now we'll just select code of the latest date that is before
       -- or equal to @startDate
       (SELECT TCode.code FROM
          (SELECT code,in_date FROM tblin   WHERE in_date <= @startDate
           UNION ALL
           SELECT code,out_date FROM tblout WHERE out_date <= @startDate)TCode
         ORDER BY TCode.in_date DESC
        LIMIT 1
        ) as Code,
        0,0,
        balance 
        FROM
        (SELECT @prevbalance :=IFNULL((SELECT SUM(in_qty) FROM tblin,
-- Change the date in below line to any date you desire as @startDate is used throughout this whole query
                                                               (SELECT @startDate := DATE('2016-03-02'))TDATE WHERE in_date <= @startDate)-
                                      (SELECT SUM(out_qty) FROM tblout WHERE out_date <= @startDate),0
                                     ) 
                               as balance
        )T4
UNION ALL
SELECT * FROM
  (SELECT T.*,@balance := @balance + (t.in_qty - t.out_qty) AS balance
   FROM 
    (SELECT in_date date,code,in_qty,0 AS out_qty FROM tblin WHERE in_date > @startDate
     UNION ALL
     SELECT out_date,    code,     0,     out_qty FROM tblout WHERE out_date > @startDate
    )T,(SELECT @balance:=@prevbalance)initial
   ORDER BY T.date ASC,T.in_qty DESC
  )T3;