Unix Technical Forum

Need help with this select statement

This is a discussion on Need help with this select statement within the SQL Server forums, part of the Microsoft SQL Server category; --> using mssql 2000... Table Def... CREATE TABLE [dbo].[tblEmployee] ( [EmpID] [int] IDENTITY (1, 1) NOT FOR REPLICATION NOT NULL ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 07:48 PM
rhaazy
 
Posts: n/a
Default Need help with this select statement

using mssql 2000...

Table Def...


CREATE TABLE [dbo].[tblEmployee] (
[EmpID] [int] IDENTITY (1, 1) NOT FOR REPLICATION NOT NULL ,
[EmployeeID] [nvarchar] (50) COLLATE
SQL_Latin1_General_CP1_CI_AS NULL
,
[DateCreated] [datetime] NULL ,
[LastModified] [datetime] NULL ,
[Deleted] [bit] NULL
) ON [PRIMARY]


CREATE TABLE [dbo].[tblEmployeeOrgNode] (
[EmpID] [int] NOT NULL ,
[OrgSystemID] [int] NOT NULL ,
[OrgNodeID] [int] NOT NULL ,
[DateCreated] [datetime] NULL ,
[LastModified] [datetime] NULL ,
[Deleted] [bit] NULL
) ON [PRIMARY]


CREATE TABLE [dbo].[tblOrgSystemNode] (
[OrgSystemID] [int] NOT NULL ,
[OrgNodeID] [int] NOT NULL ,
[OrgNode] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS
NULL ,
[DateCreated] [datetime] NULL ,
[LastModified] [datetime] NULL ,
[Deleted] [bit] NULL
) ON [PRIMARY]


When I run this query:
select Employeeid, tblEmployee.EmpID, OrgNode
from tblEmployee JOIN tblEmployeeOrgNode a
on tblEmployee.EmpId = a.EmpID,
tblOrgSystemNode JOIN tblEmployeeOrgNode b
on tblOrgSystemNode.OrgNOdeId = b.OrgNodeID


I get this as a result set:
Name ID Location
rh 1 Mano
rm 2 Mano
rd 3 Mano
rh 1 Huso
rm 2 Huso
rd 3 Huso
rh 1 Oso
rm 2 Oso
rd 3 Oso


This is what I am trying to get:
Name ID Location
rh 1 Mano
rm 2 Huso
rd 3 Oso

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 07:48 PM
Erland Sommarskog
 
Posts: n/a
Default Re: Need help with this select statement

rhaazy (rhaazy@gmail.com) writes:
> When I run this query:
> select Employeeid, tblEmployee.EmpID, OrgNode
> from tblEmployee JOIN tblEmployeeOrgNode a
> on tblEmployee.EmpId = a.EmpID,
> tblOrgSystemNode JOIN tblEmployeeOrgNode b
> on tblOrgSystemNode.OrgNOdeId = b.OrgNodeID
>...
>
> This is what I am trying to get:
> Name ID Location
> rh 1 Mano
> rm 2 Huso
> rd 3 Oso


It's good to have the table, but with out sample data and an explanation
of the table, it's difficult to make much useful out of it. It had also
help if you had included definitions of primary keys and foreign keys.

But I note your query includs a cross join with tblOrgSystemNode. This
looks suspicious. Maybe that is an error. So as a complete guess

select E.Employeeid, E.EmpID, S.OrgNode
from tblEmployee E
JOIN tblEmployeeOrgNode EON on E.EmpId = EON.EmpID,
JOIN tblOrgSystemNode S ON S.OrgNOdeId = EON.OrgNodeID

If this does not meet your requirements, please supply sample data
(as INSERT statements) and briefly explain the business rules.

--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 07:48 PM
rhaazy
 
Posts: n/a
Default Re: Need help with this select statement

Sorry for not including the rest, but you hit the nail right on the
head anyway, thanks a lot that did the trick.


Erland Sommarskog wrote:
> rhaazy (rhaazy@gmail.com) writes:
> > When I run this query:
> > select Employeeid, tblEmployee.EmpID, OrgNode
> > from tblEmployee JOIN tblEmployeeOrgNode a
> > on tblEmployee.EmpId = a.EmpID,
> > tblOrgSystemNode JOIN tblEmployeeOrgNode b
> > on tblOrgSystemNode.OrgNOdeId = b.OrgNodeID
> >...
> >
> > This is what I am trying to get:
> > Name ID Location
> > rh 1 Mano
> > rm 2 Huso
> > rd 3 Oso

>
> It's good to have the table, but with out sample data and an explanation
> of the table, it's difficult to make much useful out of it. It had also
> help if you had included definitions of primary keys and foreign keys.
>
> But I note your query includs a cross join with tblOrgSystemNode. This
> looks suspicious. Maybe that is an error. So as a complete guess
>
> select E.Employeeid, E.EmpID, S.OrgNode
> from tblEmployee E
> JOIN tblEmployeeOrgNode EON on E.EmpId = EON.EmpID,
> JOIN tblOrgSystemNode S ON S.OrgNOdeId = EON.OrgNodeID
>
> If this does not meet your requirements, please supply sample data
> (as INSERT statements) and briefly explain the business rules.
>
> --
> Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se
>
> Books Online for SQL Server 2005 at
> http://www.microsoft.com/technet/pro...ads/books.mspx
> Books Online for SQL Server 2000 at
> http://www.microsoft.com/sql/prodinf...ons/books.mspx


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 03:01 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com