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.Orde rNo, dbo.Table4.Orde rTotal
FROM dbo.Table1 LEFT OUTER JOIN
dbo.Table4 INNER JOIN
dbo.Table2 ON dbo.Table4.Tabl e4ID =
dbo.Table2.Tabl e4ID INNER JOIN
dbo.Table3 ON dbo.Table2.Tabl e2ID =
dbo.Table3.Tabl e3ID ON dbo.Table1.Tabl e2ID = dbo.Table2.Tabl e2ID AND
dbo.Table1.Tabl e3ID = dbo.Table3.Tabl e3ID
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_Gene ral_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_Gene ral_CP1_CI_AS NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Table4] (
[Table4ID] [int] IDENTITY (1, 1) NOT NULL ,
[OrderTotal] [varchar] (50) COLLATE SQL_Latin1_Gene ral_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_Table 2] FOREIGN KEY
(
[Table2ID]
) REFERENCES [dbo].[Table2] (
[Table2ID]
),
CONSTRAINT [FK_Table1_Table 3] FOREIGN KEY
(
[Table3ID]
) REFERENCES [dbo].[Table3] (
[Table3ID]
)
GO
ALTER TABLE [dbo].[Table3] ADD
CONSTRAINT [FK_Table3_Table 2] FOREIGN KEY
(
[Table3ID]
) REFERENCES [dbo].[Table2] (
[Table2ID]
)
GO 6 1996
Do:
SELECT Table2.Table3ID , Table4.OrderTot al
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" <an***@bizdatas olutions.com> wrote in message news:<b0******* **************@ bgtnsc04-news.ops.worldn et.att.net>... Do:
SELECT Table2.Table3ID , Table4.OrderTot al 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" <an***@bizdatas olutions.com> wrote in message news:<b0******* **************@ bgtnsc04-news.ops.worldn et.att.net>...
Do:
SELECT Table2.Table3ID , Table4.OrderTot al 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 <so****@algonet .se> wrote in message news:<Xn******* *************** @127.0.0.1>... Matt Creely (cr*****@hotmai l.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 <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
Matt Creely (cr*****@hotmai l.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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 gogle1.com This is lee's website.
1 lee's gogle2.com This is lee's website.
2 Jon's yaho1.com This is Jon's website.
2 Jon's yaho2.com This is Jon's website.
|
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, and all
the same fields and field types. Each database holds a particular
vendors data, lets call them VDB1, VDB2, ... VDBX. Then I have a temp
database that is structured just like the VDBs, which is populated by a
script I wrote. My goal is to...
|
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 .asp that I've stripped
down.. feel free to paste the code for your own testing.. it works:
----------------- begin paste-- -----------
<%
' logon_user will be in DOMAIN\LANID format (NT4 Format)
logonuser = Request.ServerVariables("LOGON_USER")
|
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 of the datagrid expaning down to
much. I basically want to display the first 50 characters in the grid cell
and then display the rest as a tooltip in the grid cell. I'm not sure if this
is possible but if anyone can help me do this I would be very...
|
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 seconds
'cause I set them to 0.
I need to be able to save them in the database (Access) where the
format of the field is for example:
12/22/2004 14:08:00
Now, I do have already code that does that but my supervisor doesn't
like it because it's...
| |
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 update/insert/delete
statements and application B has 80-20 mix of select and
update/insert/delete statements. Being able to scale the databases as
needed so the performance is unaffected, is one of our critical
requirements. We've been investigating...
|
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
|
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
minimal size
|
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 ?
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| | |