使用Dynamics CRM的SQL报告时遇到问题

时间:2010-08-16 21:43:33

标签: sql sql-server reporting-services dynamics-crm 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

2 个答案:

答案 0 :(得分:0)

在我看来,付款信息应与发票在同一行。获得第二行的唯一方法是,如果您有两笔付款。由于您要对发票日期进行排序,因此两行将具有相同的日期并且彼此相邻排序。

根据您所说的,我的猜测是,如果没有付款和付款日期(如果有的话),您希望对发票日期进行排序。尝试:

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

答案 1 :(得分: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