将行中的SQL和提取到列中?
我有一个非常缓慢的搜索,通过查找主要记录,然后在线程中循环来汇总相关交易。我试图让它在一个单一的报表中工作,虽然已经接近,但仍然有交替借记和贷记的记录 我不知道如何将借方和贷方行向上拉到列中,因此每个日期和作业都有一行结果将行中的SQL和提取到列中?,sql,mysql,Sql,Mysql,我有一个非常缓慢的搜索,通过查找主要记录,然后在线程中循环来汇总相关交易。我试图让它在一个单一的报表中工作,虽然已经接近,但仍然有交替借记和贷记的记录 我不知道如何将借方和贷方行向上拉到列中,因此每个日期和作业都有一行结果 SELECT j.dtmInvoicedOn, j.strJobKey, c.strCustName, strTransType, SUM(r.dblTransactionAmount) AS SUM_dblTotalCharge FROM tbljo
SELECT j.dtmInvoicedOn, j.strJobKey, c.strCustName, strTransType,
SUM(r.dblTransactionAmount) AS SUM_dblTotalCharge
FROM tbljobs AS j
INNER JOIN tblreceivledger AS r ON j.strJobKey = r.strJobKey
INNER JOIN tblcustomers AS c ON j.intCustomerID = c.intCustomerID
WHERE c.strCustomerName = 'Acme Runners Inc'
GROUP BY j.strJobKey, c.strCustName, strTransType
ORDER BY dtmInvoicedOn, strJobKey;
产生像这样的输出,借贷总额几乎交替
+----------------+---------------+------------------+--------------------+--------------------+
| dtmInvoicedOn | strJobKey | strCustomerName | strTransactionType | SUM_dblTotalCharge |
+----------------+---------------+------------------+--------------------+--------------------+
| 2008-07-03 | 270876-1 | Acme Runners Inc | credit | -5531.52 |
| 2008-07-11 | 270880-1 | Acme Runners Inc | debit | 5058.54 |
| 2008-07-11 | 270880-1 | Acme Runners Inc | credit | -5058.54 |
| 2008-07-18 | 271468-1 | Acme Runners Inc | debit | 5290.17 |
| 2008-07-18 | 271468-1 | Acme Runners Inc | credit | -5290.17 |
| 2008-11-07 | 286049-1 | Acme Runners Inc | debit | 5230.44 |
| 2008-11-14 | 286051-1 | Acme Runners Inc | debit | 5375.14 |
| 2008-11-21 | 286107-1 | Acme Runners Inc | debit | 5572.33 |
| 2008-11-28 | 286112-1 | Acme Runners Inc | debit | 5123.42 |
所以我希望它看起来像:
+----------------+---------------+------------------+----------+----------+
| dtmInvoicedOn | strJobKey | strCustomerName | credit | debit |
+----------------+---------------+------------------+----------+----------+
| 2008-07-03 | 270876-1 | Acme Runners Inc | -5531.52 | 0 |
| 2008-07-11 | 270880-1 | Acme Runners Inc | -5058.54 | 5058.54 |
| 2008-07-18 | 271468-1 | Acme Runners Inc | -5290.17 | 5290.17 |
| 2008-11-07 | 286049-1 | Acme Runners Inc | 0 | 5230.44 |
| 2008-11-14 | 286051-1 | Acme Runners Inc | 0 | 5375.14 |
| 2008-11-21 | 286107-1 | Acme Runners Inc | 0 | 5572.33 |
| 2008-11-28 | 286112-1 | Acme Runners Inc 0 | 5123.42 |
请注意,服务器当前正在运行mysql,但稍后将迁移到postgres和sqlite
谢谢这应该可以完成这项工作:
SELECT j.dtmInvoicedOn, j.strJobKey, c.strCustName, strTransType,
SUM(CASE WHEN strTransType='credit' THEN r.dblTransactionAmount ELSE 0 END) AS SUM_CREDIT,
SUM(CASE WHEN strTransType='debit' THEN r.dblTransactionAmount ELSE 0 END) AS SUM_DEBIT
FROM tbljobs AS j
INNER JOIN tblreceivledger AS r ON j.strJobKey = r.strJobKey
INNER JOIN tblcustomers AS c ON j.intCustomerID = c.intCustomerID
WHERE c.strCustomerName = 'Acme Runners Inc'
GROUP BY j.strJobKey, c.strCustName
ORDER BY dtmInvoicedOn, strJobKey;
您希望摆脱交易类型,并拥有两个字段,这两个字段的总数取决于借方或贷方
SELECT j.dtmInvoicedOn
, j.strJobKey
, c.strCustName
, Sum(Case When strTransactionType = 'credit' then r.dblTransactionAmount else 0 end) as credit
, Sum(Case When strTransactionType = 'debit' then r.dblTransactionAmount else 0 end) as debit
FROM tbljobs AS j
INNER JOIN tblreceivledger AS r ON j.strJobKey = r.strJobKey
INNER JOIN tblcustomers AS c ON j.intCustomerID = c.intCustomerID
WHERE c.strCustomerName = 'Acme Runners Inc'
GROUP BY j.dtmInvoicedOn
, j.strJobKey
, c.strCustName
ORDER BY dtmInvoicedOn, strJobKey;
我把这个问题弄糊涂了,因为我的老板一直严格要求在任何公开讨论中都不要发布实际的模式细节,排错修复了,谢谢。谢谢你的提示-我在其他上下文中使用了很多CASE语句,我没有想到要放入SUM!