我有这个查询,我认为我的条件是真的,但不知何故它给出了不正确的输出。
SELECT CASE WHEN j.DocTypeID=0 AND CreditOrDebit = 'd' THEN SecDebit END AS 'debit',
CASE WHEN j.DocTypeID=0 AND CreditOrDebit = 'c' THEN SecCredit END AS 'credit',docdate,DATEDIFF(d,DocDate, GETDATE()) AS days,
CASE WHEN DATEDIFF(d,DocDate, GETDATE()) <= 365 THEN SecDebit ELSE
CASE WHEN j.DocTypeID=0 AND CreditOrDebit = 'c' THEN -seccredit ELSE 0 END END AS 'secdebitlessthen365',
CASE WHEN DATEDIFF(d, DocDate, GETDATE()) > 365 AND DATEDIFF(d, DocDate, GETDATE()) <= 730 THEN SecDebit ELSE
CASE WHEN j.DocTypeID=0 AND CreditOrDebit = 'c' THEN -seccredit ELSE 0 END END AS 'secdebitgreaterthen365lessthenorequa730',
CASE WHEN DATEDIFF(d, DocDate, GETDATE()) > 730 AND DATEDIFF(d, DocDate, GETDATE()) <= 1095 THEN SecDebit ELSE
CASE WHEN j.DocTypeID=0 AND CreditOrDebit = 'c' THEN -seccredit ELSE 0 END END AS 'secdebitgreaterthen730lessthenorequal1095',
CASE WHEN DATEDIFF(d, DocDate, GETDATE()) > 1095 THEN SecDebit ELSE
CASE WHEN j.DocTypeID=0 AND CreditOrDebit = 'c' THEN -seccredit ELSE 0 END END AS 'secdebitgreaterthen1095'
FROM J_Transactions (NOLOCK) j
WHERE AccountID IN (871446)
AND j.DocTypeID=0
仅用于信用我的其他陈述是错误的
我的输出是
debit credit days secdebit<365 secdebit>365<=730 secdebit>730<=1095 secdebit>1095
24196.5000 NULL 783 0.0000 0.0000 24196.5000 0.0000
NULL 24471.6500 418 -24471.6500 0.0000 -24471.6500 -24471.6500
NULL 10.5800 418 -10.5800 0.0000 -10.5800 -10.5800
对于信用,这两天都是418,它应该在secdebit&gt; 365&lt; = 730之下,但它会进入除正确的那个之外的每个地方。有人能告诉我这个错误吗
期望的输出
debit credit days secdebit<365 secdebit>365<=730 secdebit>730<=1095 secdebit>1095
24196.5000 NULL 783 0.00 0.0000 24196.5000 0.00
NULL 24471.6500 418 0.00 -24471.65 0.00 0.00
NULL 10.5800 418 0.00 -10.5800 0.00 0.00
答案 0 :(得分:1)
我不会重写你的整个查询,但我相信你想要修改你的case语句,以遵循更像这样的结构:
CASE WHEN DATEDIFF(d, DocDate, GETDATE()) <= 365 THEN
CASE WHEN CreditOrDebit = 'd' THEN SecDebit
ELSE -seccredit END
ELSE 0 END AS 'secdebitlessthan365'
答案 1 :(得分:0)
无法理解您的业务逻辑。根据您提供的代码,问题是“Else”语句没有Days检查逻辑。我添加了相同的内容。 请试一试。
{
"a":"1",
"b":"2"
}
{
"c":"3",
"d":"4"
}