| Author |
Topic |
|
im1dermike
Posting Yak Master
222 Posts |
Posted - 2008-06-03 : 11:36:30
|
| This subject probably isn't correct, but I'm not sure how else to describe what I want to do.I have two tables. Table 1:Act_Num.....Code1...........332...........233...........224...........265...........20Table 2:Part_ID.....Act_Listings1927..........33 145819..........23 26What I need to do is identify records from Table 2 that do not have an Activity in the Act_Listing field contained in Table 1. So for Part_ID=1927, code 33 exists in Table 1, but code 14 doesn't, so that record should be selected. |
|
|
rohitkumar
Constraint Violating Yak Guru
472 Posts |
Posted - 2008-06-03 : 11:39:39
|
| so does act_listings has numbers separated by a space? |
 |
|
|
im1dermike
Posting Yak Master
222 Posts |
Posted - 2008-06-03 : 11:45:42
|
| Yes. I know I need to use some form of this code for that:CHARINDEX(Table2.Code, Table1.Act_Listings) = 0 |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-06-03 : 12:28:22
|
| Will there only be two activities in Act_Listings in Table2? Show us a data example of each possible format for that column.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Database maintenance routines:http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx |
 |
|
|
im1dermike
Posting Yak Master
222 Posts |
Posted - 2008-06-03 : 12:33:32
|
| No, there can be anywhere between 1 and 4 or maybe even 5. The format should be activity numbers separated by spaces eg. "24 36 33". Regardless of how they codes are separated, I thought using CHARINDEX would work. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-03 : 13:11:25
|
Create a function like this:-CREATE FUNCTION dbo.ParseValues(@Str varchar(1000))RETURNS @RESULT TABLE(ID int IDENTITY(1,1),Value int)ASBEGINDECLARE @Number varchar(10)WHILE @Str IS NOT NULLBEGINSELECT @Number=CASE WHEN CHARINDEX(' ',@Str) > 0 THEN LEFT(@Str,CHARINDEX(' ',@Str)-1) ELSE @Str END, @Str=CASE WHEN CHARINDEX(' ',@Str) > 0 THEN SUBSTRING(@Str,CHARINDEX(' ',@Str)+1,LEN(@Str)) ELSE NULL ENDINSERT INTO @RESULT ([Value]) SELECT CAST(@Number AS int)ENDRETURNENDuse function like this:-SELECT main.Part_ID,main.Act_ListingsFROM (SELECT a.Part_ID,a.Act_Listings,b.ValueFROM Table2 aCROSS APPLY dbo.ParseValues (Act_Listings) b)mainLEFT JOIN Table1 tmpON tmp.Code=main.ValueWHERE tmp.Act_Num IS NULL |
 |
|
|
im1dermike
Posting Yak Master
222 Posts |
Posted - 2008-06-03 : 13:11:34
|
| How would the CHARINDEX function not accomplish what I want to do, possibly more easily? |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-06-03 : 13:20:01
|
| CHARINDEX is already being used in the functions.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Database maintenance routines:http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx |
 |
|
|
im1dermike
Posting Yak Master
222 Posts |
Posted - 2008-06-03 : 13:23:35
|
| My reply was regarding your reply. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-06-03 : 13:27:41
|
| I don't understand. The functions are already using CHARINDEX, both from the link I posted and the function that visakh16 posted.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Database maintenance routines:http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx |
 |
|
|
im1dermike
Posting Yak Master
222 Posts |
Posted - 2008-06-03 : 13:28:32
|
| Oh I didn't see your link somehow. n/m |
 |
|
|
im1dermike
Posting Yak Master
222 Posts |
Posted - 2008-06-03 : 13:33:54
|
Okay. I tried implementing the code visakh16 suggested and it doesn't appear to be working. Here are the actual tables and column names:ACTIVITY_CORE_LISTINGACTV_CORE.....ACTV_NAME1...............332...............233...............224...............265...............206...............287...............428...............249...............1412..............1611..............2513..............1514..............18Activity_ListPRTCPNT_ID......Activity List1000059............33 1000086............33 38 1001881............20 38 1001906............24 1002403............24 1002539............20 38 1002828............20 38 1002870............33 1004340............24 1005639............24 1005644............33 38 And here is the query I have, altered from visakh16's code:SELECT main.PRTCPNT_ID,main.[Activity List]FROM (SELECT a.PRTCPNT_ID,a.[Activity List],b.ValueFROM Activity_List aCROSS APPLY dbo.ParseValues ([Activity List]) b)mainLEFT JOIN ACTIVITY_CORE_LISTING tmpON tmp.ACTV_NAME=main.ValueWHERE tmp.actv_core IS NULL Here is the result:PRTCPNT_ID.....Activity List1000059..........331000059..........331000086..........33 38 1000086..........33 38 1000086..........33 38 1000086..........33 38 1000086..........33 38 1000086..........33 38 1001881..........20 38 1001881..........20 38 1001881..........20 38 1001881..........20 38 1001881..........20 38 1001881..........20 38 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-03 : 13:46:11
|
Try grouping:-SELECT main.PRTCPNT_ID,main.[Activity List]FROM (SELECT a.PRTCPNT_ID,a.[Activity List],b.ValueFROM Activity_List aCROSS APPLY dbo.ParseValues (LTRIM(RTRIM([Activity List]))) b)mainLEFT JOIN ACTIVITY_CORE_LISTING tmpON tmp.ACTV_NAME=main.ValueWHERE tmp.actv_core IS NULLGROUP BY main.PRTCPNT_ID,main.[Activity List] |
 |
|
|
im1dermike
Posting Yak Master
222 Posts |
Posted - 2008-06-03 : 13:52:26
|
Awesome. That appears to work.Unfortunately, I just found out that I only need to display records where NO activities in the Activity List field exist in the ACTIVITY_CORE_LISTING table. So if a record has an Activity List value of "33 38", then it should NOT appear, but if it has a value of "35" or "38 35" then it SHOULD appear. Not sure how to alter the code I have... |
 |
|
|
im1dermike
Posting Yak Master
222 Posts |
Posted - 2008-06-03 : 14:55:47
|
| Nevermind. I found a much easier way to do it based on the view that Activity_Listing is created. Thanks for the parse info, though! I'm sure it will come in handy in the future. |
 |
|
|
|