案例陈述中的Where子句

时间:2016-11-27 17:03:29

标签: sql sql-server drilldown

我需要详细了解现有报告,该报告向我展示了每个客户每月在电信公司的总盈利能力。

该公司销售电话,服务费和折扣。

这是我使用的查询,特别是使用一个客户:

SELECT 
    custumer_name,
    ISNULL(SUM(CASE CONVERT(VARCHAR(10), month_end, 103)
                  WHEN '31/10/2016' 
                     THEN totcall + isnull(totrec, 0) - discount
                     ELSE 0
               END), 0) AS '31/10/2016',
    SUM(totcall) AS 'Total Calls',
    SUM(totrec) AS 'Total Rec',
    SUM(discount) AS 'Discounts'
FROM 
    total_sales
INNER JOIN 
    customer_details ON billingaddress = customer_details .siteid 
INNER JOIN 
    sales_month d ON total_sales.periodid = d.monthid 
INNER JOIN 
    customer b ON customer_details .id = b.id AND b.is_customer = 1 
WHERE 
    b.custumer_name = '2 
GROUP BY 
    b.custumer_name
ORDER BY 
    b.custumer_name ASC

这会带回正确的总数,但是当我需要显示电话总数的详细信息时,rec& amp;折扣它显示了我每个月的实际总和'存储在该表上的数据。

我不确定如何在不指定实际月份的情况下将最后3列列入总项目(因为报告中包含过去12个月数据的列。

请帮帮忙?

谢谢!

PS。数据库是SQL Server 2008

0 个答案:

没有答案