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 2008 Forums
 Transact-SQL (2008)
 FINDING THE MISSING RECORD

Author  Topic 

jbulldog
Starting Member

21 Posts

Posted - 2013-04-16 : 23:25:24

I am looking for the right way to determine a missing service from all similar client accounts. We have six different regions with multiple clients. Only One region has this specific service for all of its regional clients. This service is mandatory, so all client accounts must have/show this service. Each client account has a "Portfolio" containing all the offered services.

What I want to do is to run a query to find all portfolios for Region 1 where this specific service is missing for this one region. Since all Portfolios across the six regions are set up the same way a simple WHERE NOT EXIST statement will return thousands of records that are irrelevant since they don't contain this service already. The following code actually returns no results, 0 rows"


SELECT dbo.ACCOUNT.PORTFOLIO
FROM dbo.ACCOUNT
INNER JOIN dbo.SERVICES
ON dbo.ACCOUNT.PORT_ROW_ID = dbo.SERVICES.ROW_ID

WHERE EXISTS (SELECT dbo.ACCOUNT.PORTFOLIO
FROM dbo.ACCOUNT
WHERE dbo.ACCOUNT.REGION = 'R1')
AND NOT EXISTS
(SELECT dbo.ACCOUNT.PORTFOLIO
FROM dbo.ACCOUNT
WHERE dbo.DM_SERVICES.SERVICE = 'MISSING_SERVICE')


Any help or alternative suggestions would be greatly appreciated.

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-04-17 : 00:25:00
[code]--May be this?
SELECT dbo.ACCOUNT.PORTFOLIO
FROM dbo.ACCOUNT
INNER JOIN dbo.SERVICES
ON dbo.ACCOUNT.PORT_ROW_ID = dbo.SERVICES.ROW_ID
WHERE dbo.ACCOUNT.REGION = 'R1' AND dbo.SERVICES.SERVICE != 'MISSING_SERVICE'[/code]

--
Chandu
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-04-17 : 00:58:01
shouldnt this suffice?

SELECT dbo.ACCOUNT.PORTFOLIO
FROM dbo.ACCOUNT
INNER JOIN dbo.SERVICES
ON dbo.ACCOUNT.PORT_ROW_ID = dbo.SERVICES.ROW_ID
GROUP BY dbo.ACCOUNT.PORTFOLIO
HAVING SUM(CASE WHEN dbo.ACCOUNT.REGION = 'R1'
AND dbo.DM_SERVICES.SERVICE = 'MISSING_SERVICE'
THEN 1
ELSE 0
END
) = 0


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

jbulldog
Starting Member

21 Posts

Posted - 2013-04-17 : 07:28:03
Thanks for the speedy replys! Chandu When I change the query to what you submitted I still get 0 rows returned. I originally thought of != but the NOT EXISTS statement seemed a better fit. Thanks though.

Visakh when I run your code changes it returned only 5 rows. I'm sure there is more than that missing. But two of the rows where from different regions which would not have that service anyway.

The other three were from the correct region but each had the service already. In which case thousands of rows should have been returned sense most of the region's accounts already have the service in their portfolio. It seems to be maybe on the right track. Thanks for the reply!
Go to Top of Page

djj55
Constraint Violating Yak Guru

352 Posts

Posted - 2013-04-17 : 13:13:15
I am most likely not even in the same ball park but what about:
SELECT  dbo.ACCOUNT.PORTFOLIO
FROM dbo.ACCOUNT
INNER JOIN dbo.SERVICES
ON dbo.ACCOUNT.PORT_ROW_ID = dbo.SERVICES.ROW_ID
WHERE dbo.ACCOUNT.REGION = 'R1'
AND dbo.DM_SERVICES.[SERVICE] = 'MISSING_SERVICE'


djj
Go to Top of Page

jbulldog
Starting Member

21 Posts

Posted - 2013-04-17 : 13:36:07
Sory djj that would bring all acounts in the region with the service. there needs to be a criteria that looks for the difference between the other portfolios that have it and those that don't and select only those that are missing the specific service. The name MISSING_SERVICE is used to emphasise what I'm looking for. Thanks.
Go to Top of Page

MIK_2008
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2013-04-17 : 13:53:01
How about this

SELECT dbo.ACCOUNT.PORTFOLIO
FROM dbo.ACCOUNT
INNER JOIN dbo.SERVICES
ON dbo.ACCOUNT.PORT_ROW_ID = dbo.SERVICES.ROW_ID
WHERE dbo.ACCOUNT.REGION = 'R1'
AND Not Exists
(SELECT dbo.ACCOUNT.PORTFOLIO
FROM dbo.ACCOUNT X
WHERE X.SERVICE = 'MISSING_SERVICE'
AND X.PORT_ROW_ID=dbo.ACCOUNT.PORT_ROW_ID)

Cheers
MIK
Go to Top of Page

jbulldog
Starting Member

21 Posts

Posted - 2013-04-17 : 15:46:20
Hi MIK I tried your code but got this string of error messages:

"Msg 207, Level 16, State 1, Line 8
Invalid column name 'SERVICES'.
Msg 4104, Level 16, State 1, Line 8
The multi-part identifier "dbo.ASSETS.PORT_ROW_ID" could not be bound."

I should mention I have limited access to this one database and cannot create or write to it, only query.

Modifying your code cleared the errors but still returned 0 rows. What I Modified:
[CODE]
SELECT dbo.ACCOUNT.PORTFOLIO_NAME
FROM dbo.ACCOUNT INNER JOIN
dbo.SERVICES ON dbo.ACCOUNT.PORT_ROW_ID =
dbo.SERVICES.ROW_ID
WHERE dbo.ACCOUNT.REGION = 'R1'
AND NOT EXISTS
(SELECT dbo.ACCOUNT.PORTFOLIO_NAME
FROM dbo.SERVICES X
WHERE X.SERVICE_NAME = 'MISSING_SERVICE')
[/CODE]

Thanks for the effort!

Cheers

James
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-04-18 : 01:59:56
quote:
Originally posted by jbulldog

Thanks for the speedy replys! Chandu When I change the query to what you submitted I still get 0 rows returned. I originally thought of != but the NOT EXISTS statement seemed a better fit. Thanks though.

Visakh when I run your code changes it returned only 5 rows. I'm sure there is more than that missing. But two of the rows where from different regions which would not have that service anyway.

The other three were from the correct region but each had the service already. In which case thousands of rows should have been returned sense most of the region's accounts already have the service in their portfolio. It seems to be maybe on the right track. Thanks for the reply!



can you show sample data and explain what eaxct output you expect out of it?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

MIK_2008
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2013-04-18 : 06:51:31
You've not used the query I proposed. However, just looked at your actual question again and I see one thing confusing which needs explanation


SELECT dbo.ACCOUNT.PORTFOLIO
FROM dbo.ACCOUNT
INNER JOIN dbo.SERVICES
ON dbo.ACCOUNT.PORT_ROW_ID = dbo.SERVICES.ROW_ID

WHERE EXISTS (SELECT dbo.ACCOUNT.PORTFOLIO
FROM dbo.ACCOUNT
WHERE dbo.ACCOUNT.REGION = 'R1')
AND NOT EXISTS
(SELECT dbo.ACCOUNT.PORTFOLIO
FROM dbo.ACCOUNT
WHERE dbo.DM_SERVICES.SERVICE = 'MISSING_SERVICE')

I don't see any table used with the name DM_Services in your whole query. Where it comes from? Seems either you have provided just a part of your query or its a typo.


Secondly, what I suggested is

SELECT
A.PORTFOLIO
FROM dbo.ACCOUNT A
INNER JOIN dbo.[SERVICES] B
ON A.PORT_ROW_ID = B.ROW_ID
WHERE A.REGION = 'R1'
AND Not Exists
(SELECT 1
FROM dbo.ACCOUNT X
WHERE X.SERVICE = 'MISSING_SERVICE'
AND X.PORT_ROW_ID=A.PORT_ROW_ID) -- this outer reference is missed when you modified my query.
What this reference does, is that it looks for a record having "missing_service" in the account table for a particular Port_Row_ID (coming from the joining tables). If exist any, that particular Port_Row_ID will be excluded from the result set.
Replace the bold (font) column with the one that stores the information 'MISSING_SERVICE' of the Account table. And see if it is of any help? If the information is not stored in Account table then use the correct table and make an outer reference for it accordingly.


Cheers
MIK
Go to Top of Page

jbulldog
Starting Member

21 Posts

Posted - 2013-04-18 : 07:24:23
Here is the actual code I use. The joins are the minimum necesSary to return results. This code produces the results below showing what it is I have:
[CODE]
SELECT DISTINCT dbo.DM_ASSETS.PORTFOLIO_NAME, dbo.DM_SERVICES.SERVICE_NAME
FROM dbo.DM_ASSETS INNER JOIN
dbo.DM_SERVICE_JOIN ON dbo.DM_ASSETS.PORT_ROW_ID = dbo.DM_SERVICE_JOIN.PORTFOLIO_ID INNER JOIN
dbo.DM_SERVICES ON dbo.DM_SERVICE_JOIN.SERVICE_ID = dbo.DM_SERVICES.ROW_ID
WHERE dbo.DM_ASSETS.COUNTRY = 'USA'
AND dbo.DM_SERVICES.SERVICE_NAME = 'US - ALL BENEFITS NOT LISTED'
[/CODE]

This is sample results of the code above this returned over 46,000 rows:
[CODE]
PORTFOLIO_NAME SERVICE_NAME
10040119 - CREDIT CLASSIC US - ALL BENEFITS NOT LISTED
10037145 - DEBIT CLASSIC US - ALL BENEFITS NOT LISTED
10002213 - DEBIT CLASSIC US - ALL BENEFITS NOT LISTED
10030959 - DEBIT CLASSIC US - ALL BENEFITS NOT LISTED
10031067 - DEBIT CLASSIC US - ALL BENEFITS NOT LISTED
10031811 - DEBIT CLASSIC US - ALL BENEFITS NOT LISTED
[/CODE]
What I need is to adjust the code above to find similar portfolios that are missing the posted service shown in the sample. There are six regions, only one region, the USA has this specific service, hence the code at the country level.

Now using the code above if adjusted to find the service that is missing I would expect to see something like this:
[CODE]
PORTFOLIO_NAME SERVICE_NAME
10040119 - CREDIT CLASSIC NULL
10037145 - DEBIT CLASSIC NULL
10002213 - DEBIT CLASSIC NULL
10030959 - DEBIT CLASSIC NULL
10031067 - DEBIT CLASSIC NULL
10031811 - DEBIT CLASSIC NULL
[/CODE]

...and maybe only a couple hundred rows returned.

Thanks

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-04-18 : 07:26:49
so you want the rows having service also to be returned but with value of NULL ?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

MIK_2008
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2013-04-18 : 07:35:05
And a question from me too, by 'MISSING_SERVICE', you mean a value that exist in dbo.DM_SERVICES.SERVICE_NAME field?
OR you mean all portfolios that are not linked with any services?

expected result shows that you want the later one

Cheers
MIK
Go to Top of Page

jbulldog
Starting Member

21 Posts

Posted - 2013-04-18 : 08:00:58
I don't think there will be a row for the services missing returned with NULL. Since the service is not there in those particular portfolios. I'm guesing I need the code to compare the differences in the portfolios to see those that have the service and those that don't, and list those that don't have the service. Because all portfolios have services, meaning more than one type of service.
Here is a sample of the services if I ran the above code without the service criteria:

[CODE]
PORTFOLIO_NAME SERVICE_NAME
10000000 - TEST TEMPLATE COMMON PHONE NUMBERS
10000000 - TEST TEMPLATE GENERAL INFORMATION
10000000 - TEST TEMPLATE GENERAL SERVICE DESCRIPTION
10000000 - TEST TEMPLATE LOCATOR
10000000 - TEST TEMPLATE PROMOTIONS
[/CODE]

does this help?
Go to Top of Page

MIK_2008
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2013-04-18 : 08:30:33
Getting it a little...

So there are three tables

1) DM_ASSETS: stores portfolio related inforamtion.
2) DM_SERVICES: Stores services related inforamtion.
3) DM_SERVICE_JOIN: a bridge/linking table that stores the primay keys of the above two tables to make their relationship.

Your requirements are to list all portfolios that are not linked with any service? Meaning that portfolio record exist in DM_ASSETS but have no corresponding records in the relationship table DM_SERVICE_JOIN, and thus not linked to any service as well?


Cheers
MIK
Go to Top of Page

jbulldog
Starting Member

21 Posts

Posted - 2013-04-18 : 08:35:49
Hi MIK! I am re-trying your code but was getting some error messages. I also had to ammend the join to reflect the code I previously posted recently because one of the tables is like a pivot table in a manner of speaking. I am going to post your version using exact column names and tables. I got it down to one error message:

Msg 102, Level 15, State 1, Line 10
Incorrect syntax near ')'.

Here's my code:
[CODE]
SELECT dbo.DM_ASSETS.PORTFOLIO_NAME
FROM dbo.DM_ASSETS A INNER JOIN
dbo.DM_SERVICE_JOIN C ON A.PORT_ROW_ID = C.PORTFOLIO_ID INNER JOIN
dbo.DM_SERVICES B ON C.SERVICE_ID = B.ROW_ID
WHERE (dbo.DM_ASSETS.COUNTRY = 'USA'
AND NOT EXISTS
(SELECT 1
FROM dbo.DM_SERVICES X
WHERE X.SERVICE_NAME = 'US - ALL BENEFITS NOT LISTED'
AND X.ROW_ID=B.ROW_ID)
[/CODE]
Where did I go wrong?

I believe the actual service name is stored on the dbo.DM_SERVICES table in the SERVICE_NAME column.

Thanks and Cheers!
Go to Top of Page

MIK_2008
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2013-04-18 : 08:45:54
remove the paranthesis after Where clause.

--> WHERE (dbo.DM_ASSETS.COUNTRY = 'USA'

But note that this query means:
Select all portfolio_name of the US Country that are linked with any service but not with "US - ALL BENEFITS NOT LISTED".
So if a PortFolio = X is linked with Services A, B, C but not with "US - ALL BENEFITS NOT LISTED", will not come in the result set.

Cheers
MIK
Go to Top of Page

MIK_2008
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2013-04-18 : 08:50:54
Just in case if all this does not help you. Then simply provide sample data in consumable format along with desired output (based on sample data). So that the team can help you out on it.

By consumable format I mean code for:
"Create Table" for the three structures that are mentioned above.
"Insert statments" for the sample data

so that the team can straight away use it and develop a query as per your requirements.
Have fun :)

Cheers
MIK
Go to Top of Page

jbulldog
Starting Member

21 Posts

Posted - 2013-04-18 : 08:52:00
MIK,
quote:
Your requirements are to list all portfolios that are not linked with any service? Meaning that portfolio record exist in DM_ASSETS but have no corresponding records in the relationship table DM_SERVICE_JOIN, and thus not linked to any service as well?

Correct, the Portfolio Name is in the DM_ASSETS table and the Service Name is in the DM_SERVICES table but I need the DM_SERVICE_JOIN table to connect the two.

My requirement is to find the portfolios that should have the service 'US - ALL BENEFITS NOT LISTED' but don't and list them. Because all portfolios has multiple services. See my sample set above.

Cheers
James

Go to Top of Page

jbulldog
Starting Member

21 Posts

Posted - 2013-04-18 : 08:58:23
MIK, removing the ( gets me this error message:

Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "dbo.DM_ASSETS.COUNTRY" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "dbo.DM_ASSETS.PORTFOLIO_NAME" could not be bound.

And of course using the group by to bound the columns gest me this:

Msg 156, Level 15, State 1, Line 6
Incorrect syntax near the keyword 'WHERE'.

Cheers
Go to Top of Page

MIK_2008
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2013-04-18 : 09:09:52
SELECT A.PORTFOLIO_NAME
FROM dbo.DM_ASSETS A
INNER JOIN dbo.DM_SERVICE_JOIN C ON A.PORT_ROW_ID = C.PORTFOLIO_ID
INNER JOIN dbo.DM_SERVICES B ON C.SERVICE_ID = B.ROW_ID
WHERE A.COUNTRY = 'USA'
AND NOT EXISTS
(SELECT 1
FROM dbo.DM_SERVICES X
WHERE X.SERVICE_NAME = 'US - ALL BENEFITS NOT LISTED'
AND X.ROW_ID=B.ROW_ID)

Cheers
MIK
Go to Top of Page
    Next Page

- Advertisement -