473,388 Members | 1,342 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Tough Problem, need help

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
Jul 20 '05 #1
6 1973
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 )
Jul 20 '05 #2
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" <an***@bizdatasolutions.com> wrote in message news:<b0*********************@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 ;

Jul 20 '05 #3
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" <an***@bizdatasolutions.com> wrote in message news:<b0*********************@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 ;


Jul 20 '05 #4
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 <so****@algonet.se> wrote in message news:<Xn**********************@127.0.0.1>...
Matt Creely (cr*****@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.

Jul 20 '05 #5
In article <25**************************@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
Jul 20 '05 #6
Matt Creely (cr*****@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, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: leegold2 | last post by:
Let's I do a mysql query and then I do a, for( $i = 1; $row = mysql_fetch_array($result); $i++ ) {...} and it gives me this: PageID Title URL Description 1 lee's ...
3
by: frank.baris | last post by:
I usually can figure out anything by searching the groups or the net all day, but this problem has been bugging me for awhile now. I have multiple databases, all of which have the same 2 tables,...
2
by: Anthony | last post by:
I am trying to get a users DN by translating the LOGON_USER NT4 format variable. I am ONLY using windows authentication for security settings: This is a Windows 2000 IIS 5 Server. Here is the...
2
by: Stephen | last post by:
I'm trying to work with a datagrid column in order to display a tooltip in a datagrid cell. The reason I am doing this is because I have some long strings being returned and I don't want the rows...
1
by: simina | last post by:
Hi... I have an "appointments" page where the user should (or not necessarily) choose a date and time for his appointment, from 6 combo boxes:year, month, day, hour, minute and AM or PM, without...
198
by: Sy Borg | last post by:
Hello: We are designing two multi-user client server applications that performs large number of transactions on database servers. On an average Application A has a 50% mix of select and...
12
by: Bill Bob | last post by:
I am going mad with this Query. I need to join 3 Tables. Their Formats are Vouchers NOT NULL , NOT NULL , NULL , NOT NULL , (255) CONSTRAINT PRIMARY KEY CLUSTERED
9
by: denis | last post by:
Hi there, I got a tough interview questions lately, and I would like to hear your opinion: An array of N chars is given Write an efficient algorithm to find all the repeating substring with a ...
4
by: Jim Rutledge | last post by:
ok ok , anyone know anything on this tough question? How do you determine the length in seconds that a midi file is , or any audio file for that matter ?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.