mysql SQL报表查询

vql8enpb  于 2023-03-07  发布在  Mysql
关注(0)|答案(1)|浏览(124)

目前,我尝试使用SQL查询从SAP表生成一些报告。我已经在SAP本身上创建了一个UDF(用户定义字段)选项,其中包含选项“已交付”、“已取消”。如果用户没有填写此UDF部分,数据库将显示数据NULL
我编写了下面的查询,但是当我尝试运行它们时,它仍然显示包含所有可用选项的结果,尽管我只想显示包含NULL值的UDF

SELECT DISTINCT CASE WHEN T1.TransType = '18' THEN (SELECT T3.U_Dlvr_Date ) ELSE (SELECT T5.U_Dlvr_Date) END AS 'Delivery Date',T1.RefDate AS 'Posting Date', --T1.Memo AS 'Description',
CASE WHEN T1.TransType = '18' THEN (SELECT T4.ItemCode) ELSE (SELECT T6.ItemCode) END AS 'Item Code', 
CASE WHEN T1.TransType = '18' THEN (SELECT T4.Dscription) ELSE (SELECT T6.Dscription) END AS 'Item Name',
CASE WHEN T1.TransType = '18' THEN (SELECT T4.Quantity) ELSE (SELECT T6.Quantity) END AS 'Quantity',
CASE WHEN T1.TransType = '18' THEN (SELECT CASE WHEN T4.UomCode = 'Manual' THEN 'KG' END AS 'UoM') ELSE (SELECT T6.UomCode) END AS 'UoM', 
--CASE WHEN T1.TransType = '18' THEN (SELECT T4.LineTotal) ELSE (SELECT T6.LineTotal) END AS 'Price',
CASE T1.TransType WHEN 18 THEN 'A/P Invoice' WHEN 30 THEN 'Journal Entry'  WHEN 46 THEN 'Outgoing Payments' WHEN 60 THEN 'Goods Issue' END AS 'Document Source', --T1.BaseRef AS 'Document Source Number', 
CASE WHEN T1.TransType = '18' THEN (SELECT T3.CardName) ELSE (SELECT T5.CardName) END AS 'Supplier Name' ,
CASE WHEN T1.TransType = '18' THEN (SELECT T3.U_Itm_Src) ELSE (SELECT T5.U_Itm_Src) END AS 'Source', 
--CASE WHEN T1.TransType = '18' THEN (SELECT CASE WHEN T3.U_Ref_No IS NULL THEN (SELECT T3.DocNum) ELSE (SELECT CAST (T3.U_Ref_No AS VARCHAR (1200))) END AS 'Reference Number') ELSE (SELECT CASE WHEN T5.U_Ref_No IS NULL THEN (SELECT T5.DocNum) ELSE (SELECT CAST (T5.U_Ref_No AS varchar (1200))) END AS 'Reference Number') END AS 'Reference Number', 
CASE WHEN T1.TransType = '18' THEN (SELECT T1.BaseRef) ELSE (SELECT T5.U_Ref_No) END AS 'Reference Number', 
CASE WHEN T1.TransType = '18'  THEN (SELECT T3.U_Itm_Orgn) ELSE (SELECT T5.U_Itm_Orgn) END AS 'Origins', 
CASE WHEN T1.TransType = '18' THEN (SELECT T3.U_Dlvr_Frm) ELSE (SELECT T5.U_Dlvr_Frm) END AS 'Delivery From',
CASE WHEN T1.TransType = '18' THEN (SELECT T3.U_Dlvr_Dest) ELSE (SELECT T5.U_Dlvr_Dest) END AS 'Destination', 
CASE WHEN T1.TransType = '18' THEN (Select T3.U_Dlvr_Courier) ELSE (SELECT T5.U_Dlvr_Courier) END AS 'Courier',
CASE WHEN T1.TransType = '18' THEN (SELECT T3.U_awb_No) ELSE (SELECT T5.U_Awb_No)END AS 'AwB No', 
--Case WHEN T7.BatchNum IS NULL THEN (SELECT T3.U_Btch_Stck) WHEN T3.U_Btch_Stck IS NULL THEN (SELECT T5.U_Btch_Stck) ELSE (SELECT T7.BatchNum) END AS 'Batch Number' ,
CASE WHEN T1.TransType = '18' THEN (SELECT CAST (T3.U_QC_Rslt AS varchar (1200))) ELSE (SELECT CAST (T3.U_QC_Rslt AS varchar (1200))) END AS 'Test Result',
CASE WHEN T1.TransType = '18' THEN (SELECT CAST (T3.U_Dlvr_Prp AS varchar (1200))) ELSE (SELECT CAST (T5.U_Dlvr_Prp AS varchar (1200))) END AS 'Purpose',
T1.BaseRef AS 'Document Reference Number',
CASE WHEN T1.TransType = '18' THEN (SELECT T3.U_Doc_Status) ELSE (SELECT T5.U_Doc_Status)END AS 'Document Status'
FROM OJDT T1
LEFT JOIN JDT1 T2 ON T1.TransID = T2.TransId
LEFT JOIN OPCH T3 ON T1.BaseRef =  T3.DocNum
LEFT JOIN PCH1 T4 ON T3.DocEntry = T4.DocEntry
LEFT JOIN OIGE T5 ON T1.BaseRef = T5.DocNum
LEFT JOIN IGE1 T6 ON T6.DocEntry = T5.DocEntry
LEFT JOIN IBT1 T7 ON T6.ObjType = T7.BaseType and T5.DocEntry = T7.BaseEntry and T6.LineNum = T7.BaseLinNum and T6.ItemCode = T7.ItemCode
LEFT JOIN OPOR T8 ON T7.BatchNum = T8.DocNum
WHERE T2.Account = '610218' AND T1.RefDate BETWEEN '2023-02-01' AND '2023-12-31' AND T3.U_Doc_Status + T5.U_Doc_Status IS NULL
ORDER BY T1.RefDate

自定义项部分称为U_Doc_StatusSample Documents Generate
您可以看到下面的图像,当我运行查询时,尽管我输入了ISNULL,查询仍然提取其中有值的UDF
感谢你的帮助谢谢
问候杰拉德

nkoocmlb

nkoocmlb1#

条件:

T3.U_Doc_Status + T5.U_Doc_Status IS NULL

如果其中一列不为空,则变为true(1)。
由于您的文档状态定义为:

CASE WHEN T1.TransType = '18' THEN (SELECT T3.U_Doc_Status) ELSE (SELECT T5.U_Doc_Status) AS 'Document Status'

您将获得非空值。要仅获得NULL值,应使用:

T3.U_Doc_Status IS NULL AND T5.U_Doc_Status IS NULL

要清理代码,可以使用IF:

IF (T1.TransType = '18', T3.U_Doc_Status, T5.U_Doc_Status) AS 'Document Status'

相关问题