我想显示何时drcr 2然后金额为借方,何时drcr 1然后金额为贷方,当IsOpening为1并且drcr为2时金额为OpeningDebit,当Isopening为1并且drcr为1则金额为openCredit 。
SELECT
GL.AccountId,Ca.Code as Code,
CA.AccountName as AccountName,
SUM(CASE GL.DrCr WHEN 2 THEN GL.Amount END) AS Debit,
SUM(CASE GL.DrCr WHEN 1 THEN GL.Amount END) AS Credit,
SUM(CASE GL.IsOpening WHEN 1 THEN Case GL.DrCr When 2 Then GL.Amount END) AS OpeningDebit,
SUM(CASE GL.IsOpening WHEN 1 THEN Case GL.DrCr When 1 Then GL.Amount END) AS OpeningCredit
FROM
GeneralLedgerLine GL
JOIN
ClientAccount CA On GL.AccountId = CA.Id
WHERE
CA.ClientId = 1
GROUP BY
GL.AccountId, CA.Code, CA.AccountName
答案 0 :(得分:2)
您的情况可能不对。
SELECT
GL.AccountId,
Ca.Code as Code,
CA.AccountName as AccountName,
SUM(CASE WHEN GL.DrCr = 2 THEN GL.Amount ELSE 0 END) AS Debit,
SUM(CASE WHEN GL.DrCr = 1 THEN GL.Amount ELSE 0 END) AS Credit,
SUM(CASE WHEN GL.IsOpening = 1 AND GL.DrCr = 2 THEN GL.Amount ELSE 0 END) AS OpeningDebit,
SUM(CASE WHEN GL.IsOpening = 1 AND GL.DrCr = 1 THEN GL.Amount ELSE 0 END) AS OpeningCredit
FROM
GeneralLedgerLine GL
INNER JOIN ClientAccount CA On GL.AccountId = CA.Id
Where
CA.ClientId = 1
GROUP BY
GL.AccountId,
CA.Code,
CA.AccountName
当条件不满足时,您应该处理自己的CASE
条件,否则它将变成Null
。
答案 1 :(得分:1)
您可以尝试以下操作-您错过了get_i_th_element <- function(test, i) {
sapply(strsplit(test, "-"), function(x) if(length(x) >= i) x[[i]] else "")
}
get_i_th_element(test, 1)
#[1] "AAA" "abcd" "blah" "blah"
get_i_th_element(test, 3)
#[1] "ccc" "" "" "blah"
表达式的END
outer CASE WHEN
或者您可以简单地执行以下操作-
SELECT GL.AccountId,Ca.Code as Code,CA.AccountName as AccountName,
SUM(CASE GL.DrCr WHEN 2 THEN GL.Amount END) AS Debit,
SUM(CASE GL.DrCr WHEN 1 THEN GL.Amount END) AS Credit,
SUM(CASE GL.IsOpening WHEN 1 THEN Case GL.DrCr When 2 Then GL.Amount END END) AS OprningDebit,
SUM(CASE GL.IsOpening WHEN 1 THEN Case GL.DrCr When 1 Then GL.Amount END END) AS OpeningCredit
FROM GeneralLedgerLine GL Join ClientAccount CA On GL.AccountId = CA.Id Where CA.ClientId = 1
GROUP BY GL.AccountId,CA.Code,CA.AccountName