question

shabah99 avatar image
shabah99 asked

Count active Customers based on item master

Hi There, I need you to help me on writing two queries in SQL 2008 that shows the following information based on item master 1- Brand wise count on customer master plus customer who purchased the brand 2- Item Wise count of customer master plus customer who purchased the item Here the link that shows the table information and the query which I tried. The problem I'm facing is the count for Division and Route are showing as null. Base on brands available in Item master I need to show the count of available customers for each brand and also the count of customer who purchased the brand. [CLick here to view the sample data on SQL Fiddle][1] 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 [1]: http://sqlfiddle.com/#!3/df2bb/6
sql-server-2008queryjoins
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Kev Riley avatar image
Kev Riley answered
Does this query get anywhere near what you are expecting: 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
3 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

shabah99 avatar image shabah99 commented ·
your query does not show all the brands in the result. Please check the sample result [HERE]( https://ethercalc.org/mmnzizii2u)
0 Likes 0 ·
Kev Riley avatar image Kev Riley ♦♦ commented ·
I've updated my answer with amended code - the fiddle link isn't working so I can't get to your original data.
0 Likes 0 ·
shabah99 avatar image shabah99 commented ·
Thanks a lot :))
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.