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 |
ronnyr
Starting Member
21 Posts |
Posted - 2006-09-27 : 21:28:25
|
Hi All,I need to produce a report where is showing total order per office.I have 4 different table: order, orderassignment, employee, and office.This query will show office that have the order assignment ONLY, and NOT showing anything if that office do not have order assign to them:select of.OfficeName, SUM(OrderID) FROM Order o JOIN OrderAssignment oa ON oa.OrderID = o.OrderID JOIN Office of ON of.Code = e.OfficeCode JOIN Employee e ON e.EmployeeID = oa.EmployeeIDGROUP BY OfficeNameHowever, i want to show ALL office on my report and if that office do not have order assignment yet, it will show zero. HOW TO DO THAT?Ron.“Know where to find the information and how to use it - That's the secret of success” |
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2006-09-27 : 21:45:28
|
[code]select of.OfficeName, SUM(case when o.OrderID is null then 0 else 1 end)FROM Office of left join Employee e ON of.Code = e.OfficeCode join OrderAssignment oa ON e.EmployeeID = oa.EmployeeID join Order o ON oa.OrderID = o.OrderIDGROUP BY of.OfficeName[/code]CODO ERGO SUM |
 |
|
ronnyr
Starting Member
21 Posts |
Posted - 2006-09-27 : 22:35:28
|
Thanks Mike,I still cannot showing the office name, i think because i join the with the orderassignment table. is there anyway to do like union or something. thanks.Ron.“Know where to find the information and how to use it - That's the secret of success” |
 |
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
|
ronnyr
Starting Member
21 Posts |
Posted - 2006-09-27 : 23:19:30
|
Ok, thanks for the blog.This is my order_assignment table:CREATE TABLE [oms].[OrderAssignment]( [AssignmentID] [int] NOT NULL, [OrderID] [int] NOT NULL, [EmployeeID] [int] NOT NULL, [RoleCode] [char](3) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [FeeSplit] [float] NOT NULL, [FeeSplitTypeCode] [char](3) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [AssignDT] [datetime] NOT NULL, [LastModifiedDT] [datetime] NOT NULLthis is my employee table:CREATE TABLE [dbo].[Employee]( [EmployeeID] [int] NOT NULL, [PersonID] [int] NOT NULL, [EmployeeCode] [char](3) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [DomainUser] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [OfficeCode] [char](3) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [DivisionCode] [char](3) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [CreatedDT] [datetime] NOT NULL, [LastModifiedDT] [datetime]this is my office code table:CREATE TABLE [dbo].[OfficeCode]( [Code] [char](3) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [Office] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [Sequence] [int]and this is my order table:CREATE TABLE [oms].[Order]( [OrderID] [int] NOT NULL, [ProductID] [int] NOT NULL, [AccountID] [char](8) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [CompanyID] [int] NULL, [PersonID] [int] NOT NULL, [OrderFee] [int] NULL, [DueDT] [datetime] NULL, [CreatedDT] [datetime] NOT NULL, [StatusCode] [char](3) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [StatusCodeDT] [datetime] NOT NULL, [SpecialInstruction] [varchar](512)What I want for the result is:San Francisco Office, Total Order A: 8 Total Order B: 0 Total Order : 8Seattle Office, Total Order A: 0 Total Order B: 0 Total Order: 0ThanksRon.“Know where to find the information and how to use it - That's the secret of success” |
 |
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2006-09-27 : 23:29:20
|
So none of your tables have primary keys defined and none of your tables have foreign key relationships defined?You didn't post any sample data.What does the Total in your result represent? Total of what?CODO ERGO SUM |
 |
|
ronnyr
Starting Member
21 Posts |
Posted - 2006-09-27 : 23:52:45
|
Order table: OrderID as PKOfficeCode table: Code as PKEmployee table: employeeID as PKOrderAssignment: assignmentID as PKTotal Order is Total of one of our product. For example productA and productB.This is product table:CREATE TABLE [oms].[Product]( [ProductID] [int] NOT NULL, [ProductTypeCode] [char](3) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [ProductName] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [DefaultFee] [int] NOT NULL, [ProductTerm] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [ProductDescription] [varchar](512) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, CONSTRAINT [PK_Product] PRIMARY KEY CLUSTERED ( [ProductID] ASCHope this helps. ThanksRon.“Know where to find the information and how to use it - That's the secret of success” |
 |
|
|
|
|
|
|