vBulletin Search Engine Optimization
| |||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| I have a very strange database with a very strange problem. Consider 4 tables: Table1: ---------------- Table1ID INT PK Table2ID INT FK Table3ID INT FK OrderNo VARCHAR(50) Table2 ---------------- Table2ID INT PK Table4ID INT FK Table3 ---------------- Table3ID INT PK Table2ID INT FK Table4 ---------------- Table4ID INT PK OrderTotal VARCHAR(50) With Data: Table1: ------------ 1 1 NULL 90001 2 2 NULL 90002 3 NULL 1 90003 4 NULL 2 90004 Table2: ------------ 1 1 2 1 Table3: ------------ 1 1 2 2 Table4: ------------ 1 500 2 1000 Table1 can have either a Table2ID OR a Table3ID but not both. This is the query I'm attempting: --------------------------- SELECT dbo.Table1.OrderNo, dbo.Table4.OrderTotal FROM dbo.Table1 LEFT OUTER JOIN dbo.Table4 INNER JOIN dbo.Table2 ON dbo.Table4.Table4ID = dbo.Table2.Table4ID INNER JOIN dbo.Table3 ON dbo.Table2.Table2ID = dbo.Table3.Table3ID ON dbo.Table1.Table2ID = dbo.Table2.Table2ID AND dbo.Table1.Table3ID = dbo.Table3.Table3ID Which gives me: --------------------------- 90001 NULL 90002 NULL 90003 NULL 90004 NULL When I really want: ---------------------------- 90001 500 90002 500 90003 500 90003 1000 (NOT 500) I don't know how to do this. Are any of you sql guru's up to the challenge? Thanks in advance -Matt p.s. sql to recreate tables included CREATE TABLE [dbo].[Table1] ( [Table1ID] [int] IDENTITY (1, 1) NOT NULL , [OrderNo] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [Table2ID] [int] NULL , [Table3ID] [int] NULL ) ON [PRIMARY] GO CREATE TABLE [dbo].[Table2] ( [Table2ID] [int] IDENTITY (1, 1) NOT NULL , [Table4ID] [int] NULL ) ON [PRIMARY] GO CREATE TABLE [dbo].[Table3] ( [Table3ID] [int] IDENTITY (1, 1) NOT NULL , [Table2ID] [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ) ON [PRIMARY] GO CREATE TABLE [dbo].[Table4] ( [Table4ID] [int] IDENTITY (1, 1) NOT NULL , [OrderTotal] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ) ON [PRIMARY] GO ALTER TABLE [dbo].[Table1] WITH NOCHECK ADD CONSTRAINT [PK_Table1] PRIMARY KEY CLUSTERED ( [Table1ID] ) ON [PRIMARY] GO ALTER TABLE [dbo].[Table2] WITH NOCHECK ADD CONSTRAINT [PK_Table2] PRIMARY KEY CLUSTERED ( [Table2ID] ) ON [PRIMARY] GO ALTER TABLE [dbo].[Table3] WITH NOCHECK ADD CONSTRAINT [PK_Table3] PRIMARY KEY CLUSTERED ( [Table3ID] ) ON [PRIMARY] GO ALTER TABLE [dbo].[Table4] WITH NOCHECK ADD CONSTRAINT [PK_Table4] PRIMARY KEY CLUSTERED ( [Table4ID] ) ON [PRIMARY] GO ALTER TABLE [dbo].[Table1] ADD CONSTRAINT [FK_Table1_Table2] FOREIGN KEY ( [Table2ID] ) REFERENCES [dbo].[Table2] ( [Table2ID] ), CONSTRAINT [FK_Table1_Table3] FOREIGN KEY ( [Table3ID] ) REFERENCES [dbo].[Table3] ( [Table3ID] ) GO ALTER TABLE [dbo].[Table3] ADD CONSTRAINT [FK_Table3_Table2] FOREIGN KEY ( [Table3ID] ) REFERENCES [dbo].[Table2] ( [Table2ID] ) GO |
| |||
| Do: SELECT Table2.Table3ID, Table4.OrderTotal FROM Table1 LEFT OUTER JOIN Table2 ON COALESCE(Table1.Table2ID, 1) = Table2.Table2ID INNER JOIN table4 ON Table2.Table2ID = Table4.Table4ID ; -- - Anith ( Please reply to newsgroups only ) |
| |||
| Thanks for your help. However, your query only returns: (changed Table2.Table3ID to Table1.OrderNo) ------------------------------------------- 90001 500 90003 500 90004 500 Instead of: ------------------------------------------- 90001 500 90002 500 90003 500 90003 1000 (NOT 500) But I'm getting closer at least. Any other ideas? "Anith Sen" <anith@bizdatasolutions.com> wrote in message news:<b0VZa.97163$0v4.6672501@bgtnsc04-news.ops.worldnet.att.net>... > Do: > > SELECT Table2.Table3ID, Table4.OrderTotal > FROM Table1 > LEFT OUTER JOIN Table2 > ON COALESCE(Table1.Table2ID, 1) = Table2.Table2ID > INNER JOIN table4 > ON Table2.Table2ID = Table4.Table4ID ; |
| |||
| Matt, Is this what you want? select Table1ID, OrderTotal from Table1, Table2, Table4 where Table1.Table3ID is null and Table1.Table2ID = Table2.Table2ID and Table2.Table4ID = Table4.Table4ID union all select Table1ID, OrderTotal from Table1, Table3, Table4 where Table1.Table2ID is null and Table1.Table3ID = Table3.Table3ID and Table3.Table4ID = Table4.Table4ID -- Steve Kass -- Drew University -- Ref: 53DDD827-B99C-4787-9D20-46F3D86EA51B Matt Creely wrote: >Thanks for your help. However, your query only returns: > >(changed Table2.Table3ID to Table1.OrderNo) >------------------------------------------- >90001 500 >90003 500 >90004 500 > >Instead of: >------------------------------------------- >90001 500 >90002 500 >90003 500 >90003 1000 (NOT 500) > >But I'm getting closer at least. Any other ideas? > > > > >"Anith Sen" <anith@bizdatasolutions.com> wrote in message news:<b0VZa.97163$0v4.6672501@bgtnsc04-news.ops.worldnet.att.net>... > > >>Do: >> >>SELECT Table2.Table3ID, Table4.OrderTotal >> FROM Table1 >> LEFT OUTER JOIN Table2 >> ON COALESCE(Table1.Table2ID, 1) = Table2.Table2ID >> INNER JOIN table4 >> ON Table2.Table2ID = Table4.Table4ID ; >> >> |
| |||
| Actually, I DID include the Create Statements, and although I didn't include the INSERT statements (I will next time) I did include the data that should have existed in the tables :P Erland Sommarskog <sommar@algonet.se> wrote in message news:<Xns93D5E1F91623FYazorman@127.0.0.1>... > Matt Creely (cr33d09@hotmail.com) writes: > > Thanks for your help. However, your query only returns: > > > > (changed Table2.Table3ID to Table1.OrderNo) > > ------------------------------------------- > > 90001 500 > > 90003 500 > > 90004 500 > > > > Instead of: > > ------------------------------------------- > > 90001 500 > > 90002 500 > > 90003 500 > > 90003 1000 (NOT 500) > > > > But I'm getting closer at least. Any other ideas? > > Yes. To wit the standard suggestion. Include in your posting the > following: > > o CREATE TABLE statments for the involved tables. > o INSERT statements for the sample data. > o The desired output. (Which you did include, but I repeat it for > completeness sake.) > > Since you did not provide this, it was not possible for Anith to post a > tested solution. |
| |||
| In article <25b6bad5.0308140747.761ecfca@posting.google.com >, cr33d09 @hotmail.com says... > Actually, I DID include the Create Statements, and although I didn't > include the INSERT statements (I will next time) I did include the > data that should have existed in the tables :P You don't understand the way these guys work here, Matt. :-) They sit at their computer and read the newsgroup with an SQL Query Analyzer (or something like it) window open. When they come to one of these questions they cut and paste the table creation statements into QA and execute them. They then execute the data insertion statements and voila, they are working on live data. Listing the contents of a table is less than useless for them. -- Rick |
| ||||
| Matt Creely (cr33d09@hotmail.com) writes: > Actually, I DID include the Create Statements, and although I didn't > include the INSERT statements (I will next time) I did include the > data that should have existed in the tables :P I reviewed the thread, and as an extra precaution I also checked Google, in case my newsserver would have missed a posting of yours, but I could find no CREATE TABLE statements. There were outlines of the table, but that is not the same. As Guinness Mann points out, the deal is that with the SQL statements prepared, it's an easy affair to cut and paste the statements into Query Analyzer for test. You may think that I am lazy, because I don't want to type CREATE TABLE statements, but, well, it's always more rewarding to help someone who also makes an effort himself. -- Erland Sommarskog, SQL Server MVP, sommar@algonet.se Books Online for SQL Server SP3 at http://www.microsoft.com/sql/techinf...2000/books.asp |