2010-08-16 39 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) 
相关问题