如果不为空则格式化字符串
Formatting string if it's not null
基于GMB's answer on my 我简化了查询:
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 Hand
和Emon
之间放一个->
,比如如果那一列不为空,否则为空字符串。
不幸的是,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
如果你想用像 '->'
这样的分隔符连接多个列,并且有空列,那么你可以像这样对每个列使用 COALESCE()
:
coalesce(columnn1, '') || coalesce('->' || column2, '') || coalesce('->' || column3, '') || ....
基于GMB's answer on my
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 Hand
和Emon
之间放一个->
,比如如果那一列不为空,否则为空字符串。
不幸的是,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
如果你想用像 '->'
这样的分隔符连接多个列,并且有空列,那么你可以像这样对每个列使用 COALESCE()
:
coalesce(columnn1, '') || coalesce('->' || column2, '') || coalesce('->' || column3, '') || ....