需求
思路:期初数据做个对账开始时间之前的数据,查询时以时间为排序条件查询
- 分别做出 收款 核销 期初 等基础数据
SELECT
billdate,
CASE
WHEN tradetypecode = 'D2' THEN
'收款单'
WHEN tradetypecode = 'F2-Cxx-001' THEN
'余额预收单'
WHEN tradetypecode = 'A0' THEN
'期初'
END tradetypecode,
billno,
businame,
remark,
customercode,
CASE
WHEN tradetypecode = 'D2' THEN
money ELSE 0
END gather,
CASE
WHEN tradetypecode = 'F2-Cxx-001' THEN
money ELSE 0
END verification,
CASE
WHEN tradetypecode = 'A0' THEN
money ELSE 0
END beginning
FROM
teamwork_verify_gather
WHERE
customercode = 'P0680102'
ORDER BY
billdate ASC,
tradetypecode DESC
- 增加余额字段
SELECT
*,
@total := ( beginning + @total + gather - verification ) AS 'balance'
FROM
(
SELECT
billdate,
CASE
WHEN tradetypecode = 'D2' THEN
'收款单'
WHEN tradetypecode = 'F2-Cxx-001' THEN
'余额预收单'
WHEN tradetypecode = 'A0' THEN
'期初'
END tradetypecode,
billno,
businame,
remark,
customercode,
CASE
WHEN tradetypecode = 'D2' THEN
money ELSE 0
END gather,
CASE
WHEN tradetypecode = 'F2-Cxx-001' THEN
money ELSE 0
END verification,
CASE
WHEN tradetypecode = 'A0' THEN
money ELSE 0
END beginning
FROM
teamwork_verify_gather
WHERE
customercode = 'P0680102'
ORDER BY
billdate ASC,
tradetypecode DESC
) AS bb,
( SELECT @total := 0 ) AS T1
- 解决分页问题
SELECT
*
FROM
(
SELECT
*,
@total := ( beginning + @total + gather - verification ) AS 'balance'
FROM
(
SELECT
billdate,
CASE
WHEN tradetypecode = 'D2' THEN
'收款单'
WHEN tradetypecode = 'F2-Cxx-001' THEN
'余额预收单'
WHEN tradetypecode = 'A0' THEN
'期初'
END tradetypecode,
billno,
businame,
remark,
customercode,
CASE
WHEN tradetypecode = 'D2' THEN
money ELSE 0
END gather,
CASE
WHEN tradetypecode = 'F2-Cxx-001' THEN
money ELSE 0
END verification,
CASE
WHEN tradetypecode = 'A0' THEN
money ELSE 0
END beginning
FROM
teamwork_verify_gather
WHERE
customercode = 'P0680102'
ORDER BY
billdate ASC,
tradetypecode DESC
) AS bb,
( SELECT @total := 0 ) AS T1
) AS cc
LIMIT 1,1
标签:CASE,END,账单,money,tradetypecode,WHEN,ELSE,mysql,上条 From: https://www.cnblogs.com/fuqian/p/16773230.html查询条件及其它字段自定义