Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
robmays
Starting Member
3 Posts |
Posted - 2005-10-14 : 06:01:20
|
| rob writes "I am trying to get this query to pull out rows where the customer has part installed work, ie the customer has many orders but the order is not complete until all work is installed, so for example the customer may have 3 jobs but if one of the jobs has an Installed Date of NULL but at least one job is installed then i need the record to come out.here is my query so far, but i dont know how to count how many of the jobs are not installed???SELECT TOP 100 PERCENT dbo.Customers.CustomerID, COUNT(dbo.Sales.CustomerID) AS JobCountFROM dbo.Customers INNER JOINdbo.Sales ON dbo.Customers.CustomerID = dbo.Sales.CustomerID INNER JOINdbo.SaleItems ON dbo.Sales.SaleID = dbo.SaleItems.SaleIDWHERE (dbo.SaleItems.TypeOfJob <> 7) AND (dbo.SaleItems.InstalledDate IS NULL) AND (dbo.SaleItems.CancelledDate IS NULL)GROUP BY dbo.Customers.CustomerID, dbo.Customers.Title, dbo.Customers.LastName, dbo.Customers.HouseNo, dbo.Customers.Street, dbo.Customers.Area, dbo.Customers.City, dbo.Customers.County, dbo.Customers.PostcodeHAVING (COUNT(dbo.Sales.CustomerID) > 1)ORDER BY dbo.Customers.CustomerID" |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-10-14 : 06:13:00
|
| Instead of COUNT(dbo.Sales.CustomerID) AS JobCountTry thisSum(Case when dbo.SaleItems.InstalledDate is null then 0 else 1 end) AS JobCountOtherwise post some sample data and the result you wantMadhivananFailing to plan is Planning to fail |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-10-14 : 07:40:08
|
>>but i dont know how to count how many of the jobs are not installed???just reverse madhivanan's CASEs of 0 and 1 to count the NULLs.or something like this:select c.customerid count(*) as [unInstalledCount]from dbo.Customers cjoin dbo.Sales s on s.customerid = c.customeridjoin dbo.salesitems si on si.salesid = s.salesidwhere si.installedDate is NULLgroup by c.customerid Be One with the OptimizerTG |
 |
|
|
|
|
|
|
|