2010-08-16 65 views
0

我正在嘗試創建一個類似發票報表的聲明,至此它顯示發票和應付金額以及付款(如果已付款),我有所有這一切都在一行中的發票上,而另一張上的付款與他們分組。我真正要做的是讓他們按日期排序,並使其看起來像一個聲明。在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 
+1

是的,這是可能的 - 如何做當前的SQL達不到你的要求是什麼? – 2010-08-16 21:49:21

+0

目前,它是按發票日期排序的,如果有付款,它會直接將它放在底下,因爲我希望它們都是按日期順序排列的。 – Simon 2010-08-16 22:00:49

+0

你真的在使用SSRS Tablix嗎?普通的SSRS表格對象不會更有用嗎? – 2010-08-17 12:17:30

回答

0

原始查詢存在一些奇怪現象 - 帳戶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 
0

在我看來,支付信息應該與發票在同一行。您獲得第二排的唯一方法是如果您有兩筆付款。由於您在發票日期進行排序,因此兩行的日期和排序都相同。

根據您的說法,我的猜測是,如果沒有付款和付款日期(如果有),您希望按發票日期排序。請嘗試:

Order by Coalesce(FilteredMag_Payment.mag_paymentdate, FilteredInvoice.createdon) 
相關問題