嗨我正在使用这个查询,所以不知怎的,我可以将值合并到1列,但是当我使用case时,它会产生多列。
这是我的查询
SELECT
SR.id,
CONVERT(VARCHAR(13), CAST(DR.doc_date AS DATE), 100) as DOCDATE,
--FOR DOC TYPE
CASE
WHEN
SR.net_total IS NOT NULL THEN 'RENT TOTAL'
END as [DOC TYPE],
CASE
WHEN
SR.dell_col_charge IS NOT NULL THEN 'CHARGED'
END as [DOC TYPE],
CASE
WHEN
SR.CDW IS NOT NULL THEN 'CDW'
END as [DOC TYPE],
CASE
WHEN
SP.amount IS NOT NULL THEN 'PAYMENT'
END as [DOC TYPE],
--FOR DEBIT AND CREDIT
CASE WHEN
SR.net_total IS NOT NULL THEN SR.net_total
END as [DEBIT],
CASE WHEN
SR.dell_col_charge IS NOT NULL THEN SR.dell_col_charge
END as [DEBIT],
CASE WHEN
SR.CDW IS NOT NULL THEN SR.CDW
END as [CREDIT],
CASE WHEN
SP.amount IS NOT NULL THEN SP.amount
END as [CREDIT]
FROM [dbo].[doc_customer] DC
LEFT JOIN [dbo].[doc_rent] DR ON DR.doc_sourced_customer_id = DC.id
LEFT JOIN [dbo].[slip_rent] SR ON SR.doc_sourced_doc_rent_id = DR.id
LEFT JOIN [dbo].[slip_rent_payment] SP ON SP.doc_sourced_rent_id = DR.id
WHERE DC.id = '1-1---1-1-1---1--1~1'
AND DR.deleted = 0
AND DR.void = 0
你可以看到我希望我的DOC TYPE只有一列与DEBIT和CREDIT相同
有什么想法或解决方案吗?
谢谢
这也是我想要的结果
EDIT!
答案 0 :(得分:1)
为每个表达式使用一个case
:
(CASE WHEN SR.net_total IS NOT NULL THEN 'RENT TOTAL'
WHEN SR.dell_col_charge IS NOT NULL THEN 'CHARGED'
. . .
END) as [DOC TYPE],
(CASE WHEN SR.net_total IS NOT NULL THEN SR.net_total
WHEN SR.dell_col_charge IS NOT NULL THEN SR.dell_col_charge
. . .
END) as [DEBIT],
. . .
但是,这假设每行只有一个doc类型。
答案 1 :(得分:0)
您的查询应该像
SELECT
SR.id,
CONVERT(VARCHAR(13), CAST(DR.doc_date AS DATE), 100) as DOCDATE,
--FOR DOC TYPE
CASE
WHEN SR.net_total IS NOT NULL THEN 'RENT TOTAL'
WHEN SR.dell_col_charge IS NOT NULL THEN 'CHARGED'
WHEN SR.CDW IS NOT NULL THEN 'CDW'
WHEN SP.amount IS NOT NULL THEN 'PAYMENT'
END as [DOC TYPE],
--FOR DEBIT AND CREDIT
CASE
WHEN SR.net_total IS NOT NULL THEN SR.net_total
WHEN SR.dell_col_charge IS NOT NULL THEN SR.dell_col_charge
END as [DEBIT],
CASE
WHEN SR.CDW IS NOT NULL THEN SR.CDW
WHEN SP.amount IS NOT NULL THEN SP.amount
END as [CREDIT]
FROM [dbo].[doc_customer] DC
LEFT JOIN [dbo].[doc_rent] DR ON DR.doc_sourced_customer_id = DC.id
LEFT JOIN [dbo].[slip_rent] SR ON SR.doc_sourced_doc_rent_id = DR.id
LEFT JOIN [dbo].[slip_rent_payment] SP ON SP.doc_sourced_rent_id = DR.id
WHERE DC.id = '1-1---1-1-1---1--1~1'
AND DR.deleted = 0
AND DR.void = 0
答案 2 :(得分:0)
使用包含多个案例的case
表达式,coalesce()
使用null
和debit
的第一个非credit
列
select
sr.id
, convert(varchar(13), cast(dr.doc_date as date), 100) as docdate
--for doc type
, case
when sr.net_total is not null then 'rent total'
when sr.dell_col_charge is not null then 'charged'
when sr.cdw is not null then 'cdw'
when sp.amount is not null then 'payment'
end as [doc type]
--for debit and credit
, coalesce(sr.net_total,sr.dell_col_charge) as [debit]
, coalesce(sr.cdw,sp.amount) as [credit]
from [dbo].[doc_customer] dc
left join [dbo].[doc_rent] dr
on dr.doc_sourced_customer_id = dc.id
left join [dbo].[slip_rent] sr
on sr.doc_sourced_doc_rent_id = dr.id
left join [dbo].[slip_rent_payment] sp
on sp.doc_sourced_rent_id = dr.id
where dc.id = '1-1---1-1-1---1--1~1'
and dr.deleted = 0
and dr.void = 0