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 |
Mits
Starting Member
48 Posts |
Posted - 2006-09-15 : 11:36:41
|
Hi all,I have got table deisgn as followsEmployee EmpIdEmp NameInvoiceTotals InvoiceNoDateTotalNetEmpIDValues in Employee TableEmpId Emp Name 1 ABC2 XYZ3 MNOValues in InvoiceTotalsInvoiceNo Date TotalNet EmpId 1234 15/09/2006 1000 12356 15/09/2006 1000 24567 15/09/2006 500 15678 15/09/2006 700 2 How can I get the Following Result by one queryEmp Name Total NetABC 1500XYZ 1700MNO 0I have got the query that will give me above result but without last row where the records of Emp is not in InvoiceTotals Table. Mits |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-09-15 : 11:43:42
|
SELECT Employee.Name AS [Emp Name], ISNULL(SUM(InvoiceTotals.TotalNet), 0) AS [Total Net]FROM Employee LEFT JOIN InvoiceTotals ON InvoiceTotals.EmpID = Employee.EmpIDGROUP BY Employee.NameORDER BY Employee.NamePeter LarssonHelsingborg, Sweden |
 |
|
Mits
Starting Member
48 Posts |
Posted - 2006-09-15 : 12:01:58
|
This is my Queryselect StaffDetails.[Employee],ISNULL(SUM(InvoiceTotals.[Total Net]), 0) AS TotalNet,ISNULL(SUM(InvoiceTotals.[Total CostBC]), 0) AS TotalCostfrom StaffDetails Left Join InvoiceTotals on InvoiceTotals.[Nominal Code] = StaffDetails.[Nominal Code]where StaffDetails.Active ='Y' and StaffDetails.[Department]='Sales'and (InvoiceTotals.[Invoice Date] between '15/09/2006' and '15/09/2006')group by StaffDetails.[Employee]When i use this "InvoiceTotals.[Invoice Date] between '15/09/2006' and '15/09/2006'" in where condition then i dont see the name of the Employee who havent sold anything on that particular date.Mits |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-09-15 : 12:05:28
|
select StaffDetails.[Employee],ISNULL(SUM(InvoiceTotals.[Total Net]), 0) AS TotalNet,ISNULL(SUM(InvoiceTotals.[Total CostBC]), 0) AS TotalCostfrom StaffDetails Left Join InvoiceTotals on InvoiceTotals.[Nominal Code] = StaffDetails.[Nominal Code] and (InvoiceTotals.[Invoice Date] between '15/09/2006' and '15/09/2006')where StaffDetails.Active ='Y' and StaffDetails.[Department]='Sales'group by StaffDetails.[Employee]Peter LarssonHelsingborg, Sweden |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-09-15 : 12:08:02
|
If you don't have time on the date, both dates are interpreted as 15/09/2006 00:00:00,so having records that only second is very slim chance.select StaffDetails.[Employee],ISNULL(SUM(InvoiceTotals.[Total Net]), 0) AS TotalNet,ISNULL(SUM(InvoiceTotals.[Total CostBC]), 0) AS TotalCostfrom StaffDetails Left Join InvoiceTotals on InvoiceTotals.[Nominal Code] = StaffDetails.[Nominal Code] and (InvoiceTotals.[Invoice Date] between '15/09/2006' and DATEADD(day, 1, '15/09/2006'))where StaffDetails.Active ='Y' and StaffDetails.[Department]='Sales'group by StaffDetails.[Employee]Peter LarssonHelsingborg, Sweden |
 |
|
Mits
Starting Member
48 Posts |
Posted - 2006-09-15 : 12:08:42
|
Hi Peter Thanks for you time. Your query works.Mits |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-09-15 : 12:10:50
|
Great!Ignore my previous posting if there is no time added to the date column.Peter LarssonHelsingborg, Sweden |
 |
|
|
|
|
|
|