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.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Query Help

Author  Topic 

brandonl
Yak Posting Veteran

58 Posts

Posted - 2004-12-14 : 15:15:00
I have two tables:


CREATE TABLE [dbo].[PACKAGING_SYSTEM_RECORDS] (
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[StateID] [int] NULL
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[PACKAGING_SYSTEM_STATE] (
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[PackageState] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[FlowOrder] [int] NOT NULL
) ON [PRIMARY]
GO
[code]


The data in the tables is as follows:

[code]
INSERT INTO PACKAGING_SYSTEM_RECORDS (StateID) VALUES(7);
INSERT INTO PACKAGING_SYSTEM_STATE (PackageState, FlowOrder) VALUES ('Flow One', 1);
INSERT INTO PACKAGING_SYSTEM_STATE (PackageState, FlowOrder) VALUES ('Flow Two', 2);
INSERT INTO PACKAGING_SYSTEM_STATE (PackageState, FlowOrder) VALUES ('Flow Three', 3);
INSERT INTO PACKAGING_SYSTEM_STATE (PackageState, FlowOrder) VALUES ('Flow Four', 4);
INSERT INTO PACKAGING_SYSTEM_STATE (PackageState, FlowOrder) VALUES ('Flow Five', 5);
INSERT INTO PACKAGING_SYSTEM_STATE (PackageState, FlowOrder) VALUES ('Flow Six', 6);
INSERT INTO PACKAGING_SYSTEM_STATE (PackageState, FlowOrder) VALUES ('Flow Seven', 7);


What I want is a list of all the PACKAGING_SYSTEM_STATE ID's and PackageState names for those flow orders that are equal to or less than the current PACKAGING_SYSTEM_RECORDS.StateID. Make sense? I'm lost :)

~BrandonL

rwolfcastle
Starting Member

8 Posts

Posted - 2004-12-15 : 03:06:47
I'm the first to admit that, without the MMC to get all the INNER JOINs going, I'm not the best at constructing hand-made queries, but I am having difficulty finding a relationship between those two tables. From my point of view the two tables do not share a common field that could be used in a JOIN.

My eyes... the goggles do nothing!
Go to Top of Page

brandonl
Yak Posting Veteran

58 Posts

Posted - 2004-12-15 : 08:43:42
PACKAGING_SYSTEM_RECORDS.StateID = PACKAGING_SYSTEM_STATE.ID

~BrandonL
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2004-12-15 : 08:48:02
maybe
select t1.*
from PACKAGING_SYSTEM_STATE t1
inner join PACKAGING_SYSTEM_RECORDS t2 on t2.StateID <= t1.ID


Go with the flow & have fun! Else fight the flow
Go to Top of Page

brandonl
Yak Posting Veteran

58 Posts

Posted - 2004-12-15 : 10:21:56
spirit1, that was pretty close to what I finally did. I took your query and modified it a bit. The ID's in the state table are not in an order that signifies their order in the app. The FlowOrder field does that. So, I needed to sort by FlowOrder. here's what I came up with (maybe could use some tweaking if ya'll see anything I can do better):

select t1.ID, t1.PackageState, t1.FlowOrder
from packaging_system_state t1
inner join (SELECT PACKAGING_SYSTEM_RECORDS.ID, PACKAGING_SYSTEM_STATE.FlowOrder
FROM PACKAGING_SYSTEM_RECORDS INNER JOIN
PACKAGING_SYSTEM_STATE ON PACKAGING_SYSTEM_RECORDS.StateID = PACKAGING_SYSTEM_STATE.ID) t2 on t2.FlowOrder >=t1.FlowOrder
where t2.id = 363 and t1.FlowOrder < 7


~BrandonL
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2004-12-15 : 10:26:16
what's wrong with:
select t1.ID, t1.PackageState, t1.FlowOrder
from packaging_system_state t1
inner join
PACKAGING_SYSTEM_RECORDS t2 ON t2.StateID = t1.ID and t2 on t2.FlowOrder >= t1.FlowOrder
where t2.id = 363 and t1.FlowOrder < 7

Go with the flow & have fun! Else fight the flow
Go to Top of Page

brandonl
Yak Posting Veteran

58 Posts

Posted - 2004-12-15 : 10:40:18
Flow order isn't a field in the records table, only in the state table-so I had to do a join of the two tables to get the flow order for the record.

~BrandonL
Go to Top of Page
   

- Advertisement -