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
 General SQL Server Forums
 New to SQL Server Programming
 determine whether an entry is valid\present??

Author  Topic 

SQLJunior
Starting Member

34 Posts

Posted - 2007-03-02 : 07:32:37

I am writing a select statement to retrieve all data for a particular region. I need to select only the callerid's which are valid for that region.

I have two tables:

tblone contains PbX data, eg regionalid, callerid, donglearea and regionaldialup, tbltwo contains regionalid, and two columns which when concatenated make up a substring of the tblone.callerid...

i need to do a check of the callerid in tblone against tbltwo to make sure it is a valid callerid. (ie. it is present in tbltwo) N.B. the concatenation of the two columns in tbltwo is only a substring of tblone.callerid.

It is easy to concatenate the two rows and place them in a temp table, how do i then search through this table and if it is present, allow my select statement to print this entire row as part of the result set?

if the callerid in tblone is not valid i have to do something similar against a dongle table using the donglearea field and if the dongle area is not valid i have to use the regionaldialup field and by using a predefined value (eg. 0800003554) to determine if the row is valid and should be selected.

I then have to delete everything which was not valid from the table.

I am a junior sql administrator and your help would be much appreciated!

thanks!

SQLJunior
Starting Member

34 Posts

Posted - 2007-03-02 : 07:32:12
SELECT lnkPBXUser.*
FROM lnkPBXUser INNER JOIN
tblPBX ON lnkPBXUser.pbxID = tblPBX.PBXID INNER JOIN
tblCodes ON tblPBX.RegionalID = tblCodes.RegionalID
WHERE (lnkPBXUser.RegionalID = 0)


RegionalID pbxID userID
0 15882 17
0 10961 2
0 10961 6
0 16499 26
0 14061 36
0 15882 2
0 15101 6
0 15101 26
0 16499 2
0 16012 26


i am sorting out this particular table atm... lnkPBXUser, i have to do the same to about 20 tables.

i have to join to tblPBX first(has callerid field) and then to the Codes table which contains the two columns for comparison:
here is a sample of the codes table

CodeID RegionalID ExtName SubsNDCD LocCD UpdateStatus RegionDesc
5485 4 BENMORE GARDENS 011 523 0 GAUTENG CENTRAL REGION
5486 4 EASTLEIGH 011 524 0 GAUTENG CENTRAL REGION
5487 4 NEW DOORNFONTEIN 011 525 0 GAUTENG CENTRAL REGION
5488 4 ROSEBANK 011 526 0 GAUTENG CENTRAL REGION
5489 4 MOROKA 011 527 0 GAUTENG CENTRAL REGION
5490 4 DIEPKLOOF 011 655 0 GAUTENG CENTRAL REGION
7973 1 PRETORIA 011 3011 0 GAUTENG CENTRAL REGION
7974 1 HARTEBEESHOEK 011 3012 0 GAUTENG CENTRAL REGION

SubsNCD and LocCD need to be concatenated

if you look below at the CallerId you will see that it will only be a substring of the callerid and the length is not always the same


SELECT tblPBX.RegionalID, tblPBX.CallerID,
tblPBX.RegionalDialup, tblDongleArea.DongleAreaCode
FROM tblPBX INNER JOIN
tblDongleArea ON
tblPBX.RegionalID = tblDongleArea.RegionalID

RegionalID CallerID RegionalDialup DongleAreaCode
4 0116559000 0800005027 MZ
4 0116559000 0800005027 MD
4 0116559000 0800005027 ME
4 0116559000 0800005027 UL
4 0116559000 0800005027 MA
4 0116559000 0800005027 MB
4 0116559000 0800005027 MC

if it is not found i have to join to the dongle table using dongle area code and the appropriate regional id:


AreaID RegionalID DongleAreaCode AreaDesc UpdateStatus
1 6 AA Parrow 0
2 6 AB Malmesbury 0
3 6 AC Worcester 0
4 6 AD Paarl 0
5 6 AE Somerset West 0
6 4 MA Bloemfontein 0
7 4 MB Welkom 0
8 4 MC Kroonstad 0

if not i will use the RegionalDialup number for that area... ie. 0800005027

expected result:


RegionalID pbxID userID
0 15882 17
0 10961 2
0 10961 6
0 16499 26
0 14061 36
0 15882 2
0 15101 6
0 15101 26
0 16499 2
0 16012 26

but only for that particular region based on my findings!
Shooooo!!!

hope this is better! you help would really be great!
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-03-02 : 08:00:36
Please post some sample data and your expected result.


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

SQLJunior
Starting Member

34 Posts

Posted - 2007-03-02 : 08:35:42
there we go it is posted.
hope it is now clearer.
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2007-03-02 : 12:59:27
I'm not sure I understand the first part, getting the user. But I think this will work for getting the rest of the data you are after:

-- Set Up data
DECLARE @tblCodes TABLE
(
CodeID INT,
RegionalID INT,
ExtName VARCHAR(100),
SubsNDCD VARCHAR(4),
LocCD VARCHAR(4),
UpdateStatus INT,
RegionDesc VARCHAR(100)
)

INSERT @tblCodes
SELECT 5485, 4, 'BENMORE GARDENS', '011', '523', 0, 'GAUTENG CENTRAL REGION' UNION ALL
SELECT 5486, 4, 'EASTLEIGH', '011', '524', 0, 'GAUTENG CENTRAL REGION' UNION ALL
SELECT 5487, 4, 'NEW DOORNFONTEIN', '011', '525', 0, 'GAUTENG CENTRAL REGION' UNION ALL
SELECT 5488, 4, 'ROSEBANK', '011', '526', 0, 'GAUTENG CENTRAL REGION' UNION ALL
SELECT 5489, 4, 'MOROKA', '011', '527', 0, 'GAUTENG CENTRAL REGION' UNION ALL
SELECT 5490, 4, 'DIEPKLOOF', '011', '655', 0, 'GAUTENG CENTRAL REGION' UNION ALL
SELECT 7973, 1, 'PRETORIA', '011', '3011', 0, 'GAUTENG CENTRAL REGION' UNION ALL
SELECT 7974, 1, 'HARTEBEESHOEK', '011', '3012', 0, 'GAUTENG CENTRAL REGION'


DECLARE @tblDongleArea TABLE
(
RegionalID INT,
CallerID VARCHAR(10),
RegionalDialup VARCHAR(10),
DongleAreaCode VARCHAR(2)
)

INSERT @tblDongleArea
SELECT 4, '0116559000', '0800005027', 'MZ' UNION ALL
SELECT 4, '0116559000', '0800005027', 'MD' UNION ALL
SELECT 4, '0116559000', '0800005027', 'ME' UNION ALL
SELECT 4, '0116559000', '0800005027', 'UL' UNION ALL
SELECT 4, '0116559000', '0800005027', 'MA' UNION ALL
SELECT 4, '0116559000', '0800005027', 'MB' UNION ALL
SELECT 4, '0116559000', '0800005027', 'MC'

DECLARE @tblDongleAreaCode TABLE
(
AreaID INT,
RegionalID INT,
DongleAreaCode VARCHAR(2),
AreaDesc VARCHAR(50),
UpdateStatus INT
)

INSERT @tblDongleAreaCode
SELECT 1, 6, 'AA', 'Parrow', 0 UNION ALL
SELECT 2, 6, 'AB', 'Malmesbury', 0 UNION ALL
SELECT 3, 6, 'AC', 'Worcester', 0 UNION ALL
SELECT 4, 6, 'AD', 'Paarl', 0 UNION ALL
SELECT 5, 6, 'AE', 'Somerset West', 0 UNION ALL
SELECT 6, 4, 'MA', 'Bloemfontein', 0 UNION ALL
SELECT 7, 4, 'MB', 'Welkom', 0 UNION ALL
SELECT 8, 4, 'MC', 'Kroonstad', 0

-- I provided two queries so you can try
-- and see which performs better on your actual data.

-- Query 1
SELECT
dac.AreaID,
dac.RegionalID,
dac.DongleAreaCode,
dac.AreaDesc,
dac.UpdateStatus
FROM
@tblCodes c
INNER JOIN
@tblDongleArea da
ON c.RegionalID = da.RegionalID
INNER JOIN
@tblDongleAreaCode dac
ON da.DongleAreaCode = dac.DongleAreaCode
WHERE
CHARINDEX(c.SubsNDCD + c.LocCD, da.CallerID) > 0

-- Query 2
SELECT
dac.AreaID,
dac.RegionalID,
dac.DongleAreaCode,
dac.AreaDesc,
dac.UpdateStatus
FROM
@tblCodes c
INNER JOIN
@tblDongleArea da
ON CHARINDEX(c.SubsNDCD + c.LocCD, da.CallerID) > 0
INNER JOIN
@tblDongleAreaCode dac
ON da.DongleAreaCode = dac.DongleAreaCode


Cheers,

-Ryan
Go to Top of Page

SQLJunior
Starting Member

34 Posts

Posted - 2007-03-02 : 16:09:17
Hi Ryan,

Thanks so so much for your reply.
I will see what i can do with you have posted here.

Perhaps i am being a bit unclear in my explanation, but any help you guys can offer is very much appreciated!!!

Ciao ciao,
SQLJunior;o)
Go to Top of Page
   

- Advertisement -