簡體   English   中英

使用Dynamics CRM的SQL報表時遇到問題

[英]Having trouble with a SQL report for Dynamics CRM

以下SQL代碼創建一個兩行表,其中一行上有發票,而付款則在另一行上分組。 但是,這不是所需的表示形式。 真正的目標是將其顯示為對帳單,其中發票在頂部,然后是按日期排序的付款清單。

根據顯示的信息,是否可以編寫查詢來實現此目的? (隨時索取更多信息)。 誰能建議一種方法?

這是SQL SELECT代碼:

SELECT     FilteredInvoice.accountidname,
           FilteredInvoice.createdon,
           FilteredInvoice.duedate,
           FilteredInvoice.invoicenumber,               
           FilteredInvoice.statecodename,
           FilteredInvoice.totalamount_base,
           FilteredMag_Payment.mag_paymentdate,
           FilteredMag_Payment.mag_amount_base,
           GETDATE() AS Today

FROM            FilteredInvoice
LEFT OUTER JOIN FilteredAccount ON FilteredInvoice.accountid = FilteredAccount.accountid
LEFT OUTER JOIN FilteredMag_Payment ON FilteredInvoice.invoiceid = FilteredMag_Payment.mag_invoiceid

WHERE     (FilteredInvoice.statecodename <> N'Canceled')
ORDER BY FilteredInvoice.createdon

在我看來,付款信息應與發票位於同一行。 獲得第二行的唯一方法是如果您有兩次付款。 由於您是按發票日期排序,因此兩行將具有相同的日期,並且彼此相鄰排序。

根據您的說法,我的猜測是,如果沒有付款,則要按發票日期排序;如果有付款,則要按付款日期排序。 嘗試:

Order by Coalesce(FilteredMag_Payment.mag_paymentdate, FilteredInvoice.createdon)

原始查詢有一些奇怪之處-帳戶ID名稱確實保存在發票表上,而不是帳戶表上嗎? 從發票到客戶的左外部聯接也使看起來好像有沒有相應客戶的發票-假設相反,這是更正常的情況,尤其是在對帳單報表中。

假設原始查詢正確選擇了所需數據,我建議:

SELECT    FilteredInvoice.accountidname, 
    FilteredInvoice.createdon,
    FilteredInvoice.createdon AS sort_date,
    FilteredInvoice.duedate,
    FilteredInvoice.invoicenumber,
    FilteredInvoice.statecodename, 
    FilteredInvoice.totalamount_base,
    CONVERT(datetime,NULL) AS mag_paymentdate,
    0 AS mag_amount_base,
    GETDATE() AS Today
FROM    FilteredInvoice 
LEFT OUTER JOIN    FilteredAccount ON FilteredInvoice.accountid = FilteredAccount.accountid 
WHERE    (FilteredInvoice.statecodename <> 'Canceled')
UNION ALL
SELECT    FilteredInvoice.accountidname, 
    FilteredInvoice.createdon,
    FilteredInvoice.createdon AS sort_date,
    FilteredInvoice.duedate,
    FilteredInvoice.invoicenumber,
    FilteredInvoice.statecodename, 
    FilteredInvoice.totalamount_base,
    FilteredMag_Payment.mag_paymentdate,
    FilteredMag_Payment.mag_amount_base,
    GETDATE() AS Today
FROM    FilteredInvoice 
LEFT OUTER JOIN    FilteredAccount ON FilteredInvoice.accountid = FilteredAccount.accountid 
JOIN    FilteredMag_Payment ON FilteredInvoice.invoiceid = FilteredMag_Payment.mag_invoiceid
WHERE    (FilteredInvoice.statecodename <> 'Canceled')
ORDER BY 3

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM