Solved

sql help

Posted on 2014-11-12
8
141 Views
Last Modified: 2014-11-12
please help me with the following sql scenario.

table 1:  names ( nameid pk int, firstname varchar )
table 2:  name.locations ( nlid pk int, nameid int, locid int )
table 3:  locations ( locid pk int, town varchar )

values:

table 1:
1, joe
2, mary
3, susan

table 2:
1, 1, 50
2, 1, 51
3, 2, 52

table 3:
50, jonestown
51, seattle
52, chicago

i need the following results:
(nameid, locid)

1,52
2,50
2,51
3,50
3,51
3,52

names not in name.locations and names not in name.locations for all locations
0
Comment
Question by:freezingHot
8 Comments
 
LVL 39

Expert Comment

by:lcohan
ID: 40437619
I would use EXCEPT to build a select SQL statement - example at link below:

http://blog.sqlauthority.com/2008/08/07/sql-server-except-clause-in-sql-server-is-similar-to-minus-clause-in-oracle/
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40437623
Give this a whirl, renaming the obvious
-- names not in name.locations 
SELECT t1.name
FROM Table1 t1
   LEFT JOIN Table2 t2 ON t1.name_id = t2.name_id
WHERE t2.location_id IS NULL

-- names not in name.locations for all locations
SELECT t3.name_id, t3.location_id,
FROM Table3 t3
   LEFT JOIN Table2 t2 ON t3.name_id = t2.name_id AND t3.location_id = t2.location_id
WHERE t2.location_id IS NULL

Open in new window

SQL Server expert chapmandew has an excellent article called SQL Server: Table Joins Explained! that spells out joins.  Also, search images.google.com for 'SQL table join' to get a wompload of pictures that describe JOIN behavior very nicely.
0
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 500 total points
ID: 40437625
Here is your exception report:

select names.nameid, locations.locid
from names
CROSS JOIN locations
EXCEPT
SELECT nameid, locid
FROM name.locations
ORDER BY nameid, locid

Open in new window

0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 47

Expert Comment

by:Vitor Montalvão
ID: 40437649
Here you go:
SELECT n.nameid, l.locid
FROM names n
	CROSS JOIN locations l 
WHERE NOT EXISTS (SELECT 1 
		FROM name_locations nl
		WHERE n.nameid = nl.nameid AND l.locid = nl.locid)
ORDER BY n.nameid, l.locid

Open in new window

0
 
LVL 1

Author Closing Comment

by:freezingHot
ID: 40437657
thank you - worked perfectly... i appreciate all of the responses.
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40437670
> i appreciate all of the responses.
Curiosity overwlems me .. did you at least try mine, which was not only a working solution but references an existing article and pictoral views of table joins?
0
 
LVL 1

Author Comment

by:freezingHot
ID: 40437692
yes i did - however, your join for t3 fails as it doesn't have the column for name_id.  when i used phillip's response, it worked the first time... therefore, i went with his response.
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40437701
Ok.   btw Nice job laying out the sample data and expected set.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

822 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question