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.
| Author |
Topic |
|
maffc
Starting Member
1 Post |
Posted - 2011-05-02 : 15:24:52
|
| Hi AllI have a problem trying to create a query to return data from one table that does not exist in another. The problem is the 'Other' Table is a Junction table and the data does exist, just not for that particular union.Table 1 lists data called Schedule types and has the following columnsST_id int [PK]ST_Desc nvarchar(10)ST_active bitTable 2 lists data called Result types and has the following columnsRST_id int [PK]RST_RESCAT_id intRST_desc nvarchar(50)RST_Active bitThe junction table was created to allow many to many relationships between Table 1 and Table 2 and contains the following columnsJRSTST_id int [PK]JRSTST_RST_id int [FK to table 2] JRSTST_ST_id int [FK to table 1]I have easily created a stored procedure to return all schedule types (table 1) that exist in the junction table for any given result type.SELECT dbo.[JUNCTION REST ST].JRSTST_id, dbo.SCHEDTYPES.ST_DescFROM dbo.[JUNCTION REST ST] INNER JOIN dbo.SCHEDTYPES ON dbo.[JUNCTION REST ST].JRSTST_ST_id = dbo.SCHEDTYPES.ST_idWHERE (dbo.[JUNCTION REST ST].JRSTST_RST_id = @selID)ORDER BY dbo.SCHEDTYPES.ST_DescHowever what i am finding hard to do is create one to return the schedule types that don't exist in the table for a given result type.I've tried the followingSELECT dbo.SCHEDTYPES.ST_id, dbo.SCHEDTYPES.ST_Desc, dbo.[JUNCTION REST ST].JRSTST_RST_idFROM dbo.SCHEDTYPES INNER JOIN dbo.[JUNCTION REST ST] ON dbo.SCHEDTYPES.ST_id = dbo.[JUNCTION REST ST].JRSTST_ST_idWHERE (dbo.[JUNCTION REST ST].JRSTST_RST_id = @selID) AND (dbo.[JUNCTION REST ST].JRSTST_ST_id IS NULL)ORDER BY dbo.SCHEDTYPES.ST_DescUnfortunately this gives me zero returns. The sample RST_id I provide to the query is known to have only 3 of the 5 possible ST_id associated to it in the junction table.I'm assuming the problem is because other two ST_ids do exist in the junction table, just not for the RST_id.Any help on resolving this would be very much appreciated.Thanks For LookingMat |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-05-02 : 15:36:26
|
| Think you are looking for all the possible combiations that don't existselect a.ST_ID, b.RST_ID(select distinct ST_ID from scheculetypes) across join(select distinct Rst_iD from resulttypes) bleft join junctiontable con a.ST_ID = c.JRSTST_ST_IDand b.RST_ID = c.JRSTST_RST_IDwhere c.JRSTST_ST_ID is null==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
|
|
|