根据物料主数据计算客户

时间:2014-07-03 04:33:28

标签: sql sql-server-2008 count

我需要您帮助我在SQL Server 2008中编写两个查询,根据项目主文件显示以下信息:

  1. 品牌明智地依靠客户主人加上购买该品牌的客户
  2. 项目客户主人的明智数量加上购买物品的客户
  3. 此处显示表格信息和我尝试的查询的链接。

    Click here to view the table in SQL Fiddle

    SELECT 
       brandname,
       division,
       route,
       DivisionTotalCustomersCount = MAX(DivisionTotalCustomersCount),
       RouteTotalCustomersCount = MAX(RouteTotalCustomersCount),
       PurchasedCustomersCount = SUM(PurchasedCustomersCount) 
    FROM
       (SELECT 
           i.brandname,
           c.division,
           c.route,
           DivisionTotalCustomersCount = 
              (SELECT COUNT(distinct x.CustomerID) 
               FROM CustomerMaster x 
               WHERE x.division = c.division),
           RouteTotalCustomersCount =
              (SELECT COUNT(distinct x.CustomerID) 
               FROM CustomerMaster x 
               WHERE x.Route = c.route),
           PurchasedCustomersCount = count(distinct C.CustomerID)
                                           FROM CustomerMaster c 
                                           LEFT OUTER JOIN SalesData s on c.CustomerID = s.CustomerID 
                                           right outer join ItemMaster i on s.item = i.itemcode
    
                                           GROUP BY i.brandname, c.division, c.route) A
    
        GROUP BY 
            brandname, division, route
        ORDER BY 1
    

    结果应如下

    Excelsheet

4 个答案:

答案 0 :(得分:0)

我认为您需要重新考虑该报告,并将其拆分为多个报告。

如果计算不同聚合级别的事物,那么路由计数和分区计数是没有意义的。所以有一个路线计数和分区计数报告。

无论哪种方式,100PLUS的分割和路线都将为空,因为该品牌没有客户,这意味着没有可用的路线或分部信息。

--Division Count
SELECT BrandName, Division, COUNT(CustomerMaster.CustomerID) [Customer Count]
FROM ItemMaster LEFT OUTER JOIN
      SalesData ON ItemMaster.BrandName = SalesData.Brand LEFT OUTER JOIN
      CustomerMaster ON SalesData.CustomerID = CustomerMaster.CustomerID
GROUP BY BrandName, Division

--Route Count
SELECT BrandName, Route, Division, COUNT(CustomerMaster.CustomerID) [Customer Count]
FROM ItemMaster LEFT OUTER JOIN
      SalesData ON ItemMaster.BrandName = SalesData.Brand LEFT OUTER JOIN
      CustomerMaster ON SalesData.CustomerID = CustomerMaster.CustomerID
GROUP BY BrandName, Route, Division

答案 1 :(得分:0)

使用您的sqlfiddle数据有25个销售记录& 18个不同的品牌/部门/路线/客户记录,没有销售活动100PLUS

select
        B.BrandName
      , V.Division
      , coalesce(Brand_count,0) as Brand_count
      , coalesce(Division_count,0) as Division_count
from (select distinct BrandName from ItemMaster) as B
cross join (select distinct Division from CustomerMaster) as V
left join (
            select
                    Brand
                  , Division
                  , sum(cust_count) over (partition by Brand)    as Brand_count
                  , sum(cust_count) over (partition by Division) as Division_count
            from (
                  select
                          S.Brand
                        , C.Division
                        , count(distinct S.CustomerID) cust_count
                  from salesdata as S
                  inner join CustomerMaster as C
                                on S.CustomerID  = C.CustomerID
                  inner join ItemMaster as I
                                on S.item = I.ItemCode
                  group by
                          S.Brand
                        , C.Division
                  ) as S
          ) as D
                  on B.BrandName = D.Brand
                 and V.Division = D.Division
order by
        B.BrandName
      , V.Division
;

| BRANDNAME | DIVISION | BRAND_COUNT | DIVISION_COUNT |
|-----------|----------|-------------|----------------|
|   100PLUS |    Dubai |           0 |              0 |
|   100PLUS |      RAK |           0 |              0 |
|     KITCO |    Dubai |           9 |             11 |
|     KITCO |      RAK |           9 |              7 |
|  Red Bull |    Dubai |           9 |             11 |
|  Red Bull |      RAK |           9 |              7 |

http://sqlfiddle.com/#!3/fecb0/27

答案 2 :(得分:0)

全部归功于@kevriley

select 
A.BrandName,
B.Division,
B.Route,
B.DivisionTotalCustomers,
B.RouteTotalCustomers,
isnull(C.PurchasedCustomersCount,0) as PurchasedCustomersCount
from 
(
select distinct
BrandName, Route, Division
from dbo.ItemMaster
cross join dbo.CustomerMaster
) A
join
(
select distinct
Division,
Route,
DENSE_RANK() over (partition by Division order by c.CustomerID asc) + DENSE_RANK() over                  (partition by Division order by c.CustomerID desc) - 1 as DivisionTotalCustomers   ,
DENSE_RANK() over (partition by ROUTE order by c.CustomerID asc) + DENSE_RANK() over     (partition by ROUTE order by c.CustomerID desc) - 1 as RouteTotalCustomers
from    CustomerMaster c
    left join SalesData s on c.CustomerID = s.CustomerID
) B on B.Division = A.Division and B.Route = A.Route
left join
(
select 
   s.brand,
   c.division,
   c.route,
   PurchasedCustomersCount = count(distinct C.CustomerID)
 FROM CustomerMaster c 
 JOIN SalesData s on c.CustomerID = s.CustomerID 
 --join ItemMaster i on s.item = i.itemcode
GROUP by s.brand, c.division, c.route
) C on A.Brandname = C.Brand and C.Division = A.Division and C.Route = A.Route

SQL Fiddle

上查看相同内容

答案 3 :(得分:0)

Select B.Brandname,B.Division,C AS DivisionTotalCustomerCount,
ISNULL(T.Count,0) AS PURCHASEDCUSTOMERSCOUNT
from 
(
Select CM.Division,M.BrandName,COUNT(distinct CM.CustomerID) AS C
from dbo.CustomerMaster CM
CROSS JOIN ItemMaster M 
GROUP BY CM.Division,M.BrandName
)B
LEFT JOIN
(Select Division,Brand,COUNT(Distinct C.CustomerID) As Count from CustomerMaster C
JOIN salesdata D
On C.CustomerID=D.CustomerID
where D.Brand='Red Bull'
GROUP BY Division,Brand
)T
ON B.Brandname=T.Brand
and B.Division=T.Division
Order by 1,2