-1

根据GMB对我上一个问题的回答,我简化了查询:

SELECT tn.Date, 
case when tn.DrControl = 7 
    then b1.Name || ' -> ' || c1.Name || ' ' || ifnull(l1.Name, '') || ' ' || ifnull(s1.Name, '') || ' ' || ifnull(p1.Name, '') || ' ' || ifnull(m1.Name , '')
    else b2.Name || ' -> ' || c2.Name || ' ' || ifnull(l2.Name, '') || ' ' || ifnull(s2.Name, '' ) || ' ' || ifnull(p2.Name, '') || ' ' || ifnull(m2.Name, '') 
end Particulars,
case when tn.DrControl = 7 then tn.Amount end DrAmount,
case when tn.CrControl = 7 then tn.Amount end CrAmount,
tn.Narration
FROM Transactions tn
LEFT JOIN Books b1 ON b1.Id = tn.DrBook
LEFT JOIN Books b2 ON b2.Id = tn.CrBook
LEFT JOIN ControlLedgers c1 ON c1.Id = tn.DrControl
LEFT JOIN ControlLedgers c2 ON c2.Id = tn.CrControl
LEFT JOIN Ledgers l1 ON l1.Id = tn.DrLedger
LEFT JOIN Ledgers l2 ON l2.Id = tn.CrLedger
LEFT JOIN SubLedgers s1 ON s1.Id = tn.DrSubLedger
LEFT JOIN SubLedgers s2 ON s2.Id = tn.CrSubLedger
LEFT JOIN Parties p1 ON p1.Id = tn.DrParty
LEFT JOIN Parties p2 ON p2.Id = tn.CrParty
LEFT JOIN Members m1 ON m1.Id = tn.DrMember
LEFT JOIN Members m2 ON m2.Id = tn.CrMember
WHERE 7 IN (tn.DrControl, tn.CrControl)

以减少在应用程序代码中创建Particulars列的开销。它产生这样的结果:

Date        Particulars                             DrAmount     CrAmount     Narration
----------------------------------------------------------------------------------------
2020-06-13  Current Assets -> Cash In Hand Emon     100000       NULL         Some Text

我现在想要的是->Cash In Handand之间放置一个Emon,例如,如果该列不为空,否则为空字符串。

4

2 回答 2

0

不幸的是,SQLite 不支持concat_ws(),这使得这样的操作无缝。

选项使用case表达式:

case when tn.DrControl = 7 
then 
    b1.Name 
    || ' -> ' || c1.Name 
    || (case when l1.Name is null then '' else ' -> ' || l1.Name end)
    || (case when s1.Name is null then '' else ' -> ' || s1.Name end )
    || (case when p1.Name is null then '' else ' -> ' || p1.Name end)
    || (case when m1.Name is null then '' else ' -> ' || m1.Name end)
else    
    b2.Name 
    || ' -> ' || c2.Name 
    || (case when l2.Name is null then '' else ' -> ' || l2.Name end)
    || (case when s2.Name is null then '' else ' -> ' || s2.Name end )
    || (case when p2.Name is null then '' else ' -> ' || p2.Name end)
    || (case when m2.Name is null then '' else ' -> ' || m2.Name end)
end Particulars
于 2020-06-13T14:42:24.187 回答
0

如果你想用分隔符连接多列'->'并且有空列,那么你可以COALESCE()像这样对每一列使用:

coalesce(columnn1, '') || coalesce('->' || column2, '') || coalesce('->' || column3, '') || ....
于 2020-06-13T14:53:22.970 回答