带Cast和SUM的嵌套大小写

时间:2019-03-06 15:21:15

标签: sql excel nested case-when

我正在通过EXCEL查询ODBC,并且已经设法使语句达到某个阶段,但我希望将其进一步发展。这是因为我发现,如果批准,它也被视为未决。因此,我整理了以下声明,但仍在继续。是否有更好的方法应该嵌套此SUM CAST CASE WHEN语句,因为我将需要再次重复此操作,但我不希望累加计数,而是求和我目前只有一个大桶的收入。 >

SELECT 

Company_0.CoaCompanyName
, SUM(CASE WHEN CAST( InvoiceCreditItem_0.IciInvoiceApproved as INT) IS 1 THEN 1) AS 'Approved' 
    ELSE SUM(CASE WHEN InvoiceCreditItem_0.InvoiceCreditID IS NULL THEN 1 ) AS 'Prepared'
    ELSE SUM(CASE WHEN InvoiceCreditItem_0.InvoiceCreditID IS NOT NULL THEN 1 END) AS 'Pending'
, InvoiceCreditItem_0.IciCreatedDate
, SUM(InvoiceCreditItem_0.IciTotalNettValue) As 'Total Nett Value'
, SUM(InvoiceCreditItem_0.IciVatValue) AS 'VAT Value'


FROM

SBS.PUB.Company Company_0
, SBS.PUB.InvoiceCreditItem InvoiceCreditItem_0

WHERE

InvoiceCreditItem_0.CompanyID = Company_0.CompanyID
AND (Company_0.CoaCompanyName<>'ATOS')
AND InvoiceCreditItem_0.IciCreatedDate >= ?

GROUP BY 

Company_0.CoaCompanyName
, InvoiceCreditItem_0.IciCreatedDate

我目前看到的是:

Approved Prepared Pending
1         0           1
1         0           3
0         1           0
1         1           0

我想看的

Approved Prepared Pending
    1         0           0
    1         0           2
    0         1           0
    1         1           0

我正在考虑的下一个阶段是:

CASE WHEN InvoiceCreditItem_0.InvoiceCreditID IS NULL THEN SUM(InvoiceCreditItem_0.IciTotalNettValue) END) AS 'Prepared Value'

我希望显示:

Approved Prepared Pending 
0          1000     0

0 个答案:

没有答案