我正在尝试在特定人员的同一OrderID
的基础上连接多行。目标是实现一个包含每个特定OrderID
的总顺序的行。
我有一个查询数据如下:
结果应该是:
PartyName | partyCode | OrderID | particulars | Price | Total
Fine Mark | 3 | 5069 | suji50kg/1500/2,maida50kg/1/2,suji50kg/1500/3| 7802 | 19432
我做了什么:
SELECT
@col = COALESCE(@col, ' ') + ' , ' + particulars
FROM (
SELECT
a.orderID,
CONCAT(a.item, '/', a.unitPrice, '/', a.quantity) as particulars
FROM tblOrderDetails a, tblAddParty b, tblPartyOrder c
WHERE a.orderID = c.orderID
AND b.partyID = c.partyID
AND b.partyName = 'Fine Marketing Islamabad'
AND c.date >= '2014-03-06'
AND c.date <= '2016-03-25') mine
SELECT
date,
partyName,
partyCode,
orderID,
@col as Particulars,
MIN(CASE WHEN total > credit THEN price ELSE 0 END) AS debit,
MIN(CASE WHEN total < credit THEN price ELSE 0 END) AS credit,
MIN(credit) + SUM(totalprice) AS balance
FROM (
SELECT
c.date,
b.partyName,
b.partyCode,
a.orderID,
CONCAT(a.item, '/', a.unitPrice, '/', a.quantity) AS particulars,
a.totalprice,
c.credit,
c.total,
c.price
FROM tblOrderDetails a, tblAddParty b, tblPartyOrder c
WHERE a.orderID = c.orderID
AND b.partyID = c.partyID
AND b.partyName = 'Fine Marketing Islamabad'
AND c.date >= '2014-03-06'
AND c.date <= '2016-03-25') marked
GROUP BY
partyName,
partyCode,
orderID,
date
ORDER BY date
结果: