Introduction

In the past week my TL give me some work. In the work I have three tables and every table is linked with a primary key and foreign key.

table

Ay first look this table looks very simple but if we add one more condition then that can make this query a bit messy. The preceding records get more than three tables. The table details are shown below.

  1. select CustomerID,CompanyName from tbl_Customer
  1. CustomerID CompanyName
  2. 1 Love Lights
  3. 4 Edmundson Electrical (Guernsey)
  4. 5 PJH Group Ltd
  5. 6 O Neills Kitchen Supplies Ltd
  6. 7 Edmundson Electrical (kingslynn)
  7. 8 Dream Home T/AS Urban Myth
  8. 9 Onefit Ltd
  9. 10 Mereway Kitchens Ltd
  10. 11 Edmunson Electrical (Tonbridge)
  11. 12 Express Indian Cuisine **
  1. select Fk_customerID,Login_Time from tbl_loginHistory
  1. Fk_customerID Login_Time
  2. 30365 2014-12-16 12:07:56.160
  3. 30365 2014-12-16 12:13:07.727
  4. 30365 2014-12-16 15:23:55.143
  5. 30365 2014-12-16 16:58:00.090
  6. 30365 2014-12-16 17:08:26.430
  7. 30365 2014-12-16 17:10:37.100
  8. 30365 2014-12-16 17:11:17.453
  9. 30365 2014-12-16 17:24:20.257
  10. 30365 2014-12-16 17:58:22.473
  1. select CustomerID,OrderDate from tbl_Orderheader
  1. CustomerID Order Date
  2. 30365 2014-12-16 12:07:56.160
  3. 30365 2014-12-16 12:13:07.727
  4. 30365 2014-12-16 1:13:07.727
  5. 30365 2014-12-16 2:13:07.727
  6. 30365 2014-12-16 12:1:07.727
  7. 30365 2014-12-16 12:3:07.727
  8. 30365 2014-12-16 12:30:07.727
  9. 30365 2014-12-16 12:34:07.727
  10. 30365 2014-12-16 12:20:07.727
  11. 30365 2014-12-16 12:17:07.727
Try One:
  1. select C.CompanyName,COUNT(lh.Fk_customerID) from tbl_LoginHistory LH
  2. inner join tbl_Customer C on LH.Fk_customerID=c.CustomerID
  3. inner join tbl_OrderHeader OH on LH.Fk_customerID=oh.CustomerID
  4. where MONTH(LH.login_time)= 12 and year(LH.login_time)=2014 group by LH.Fk_customerID
If we run this query then compiler throws the error:

Column 'tbl_Customer.CompanyName' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

Note: The receding query can't be solved because we need a count of the until date.

Finally, I found one simple and best way to solve this type of problem.

  1. ALTER proc [dbo].[Proc_CustomerLoginHistory] 2014,12
  2. (
  3. @years varchar(max),
  4. @month varchar(max)
  5. )
  6. as
  7. begin
  8. ;with customerlist(csid,companyname)
  9. as
  10. (
  11. select distinct CustomerID ,CompanyName from tbl_Customer
  12. ),
  13. monthtotallogin(csid,monthlycount)
  14. as
  15. (
  16. select fk_customerid as csid,count(*) monthlycount from tbl_LoginHistory
  17. where MONTH(login_time)= @month and year(login_time)=@years group by Fk_customerID having Fk_customerID>0
  18. ),
  19. totalcount(csid,totallogincount)
  20. as
  21. (
  22. select fk_customerid as csid,count(*) as totallogincount from tbl_LoginHistory group by Fk_customerID having Fk_customerID>0
  23. ),
  24. Totalorderofmonth(csid,monthlyordercount)
  25. as
  26. (
  27. select CustomerID as csid,COUNT(*) as monthlyordercount from tbl_OrderHeader
  28. where MONTH(OrderDate)= @month and year(OrderDate)=@years
  29. group by CustomerID having CustomerID>0
  30. ),
  31. Totalordero(csid,ordercount)
  32. as
  33. (
  34. select CustomerID as csid,COUNT(*) as monthlyordercount from tbl_OrderHeader group by CustomerID having CustomerID>0
  35. )
  36. select CS.companyname as [Company Name],isnull(ML.monthlycount,0) as [This Month Total Login],
  37. isnull(TL.totallogincount,0) as [Total Login Till Date],isnull(TM.monthlyordercount,0) as [Total Order this Month], isnull(TOR.ordercount,0)as [Total Order Till Date]
  38. from customerlist cs
  39. left join monthtotallogin ML on cs.csid=ML.csid
  40. left join totalcount TL on cs.csid=TL.csid
  41. left join Totalorderofmonth TM on cs.csid=TM.csid
  42. left join Totalordero TOR on cs.csid=TOR.csid
  43. where monthlycount>0
  44. end
Output of above query
  1. Company Name This Month Total Login Total Login Till Date Total Order this Month Total Order Till Date
  2. Website Testing 37 37 1 1
Final word

If you have a question or information about this problem then drop your comments below in the comment box.