February 19, 2018 at 8:02 am
I have two Tables (In Ms Sql Server)
Table Name Column Names
MasterCities Countryy City
VisitedCities Country City
I want to delete from the MasterCities table any Country/Cities I have visited
I don't have any primary keys as the second table is coming from an outside source so need to be able to match by Coountry and City in both tables
So I want to end up , in the MasterCities table, all the Countries/Cities that originally exist in the Master Table, except those that are in the VisitedCities
Have tried various selects where not in etc. but cannot get syntax right or its deleting too much stuff
February 19, 2018 at 8:23 am
xxx-593414 - Monday, February 19, 2018 8:02 AMI have two Tables (In Ms Sql Server)
Table Name Column Names
MasterCities Countryy City
VisitedCities Country CityI want to delete from the MasterCities table any Country/Cities I have visited
I don't have any primary keys as the second table is coming from an outside source so need to be able to match by Coountry and City in both tables
So I want to end up , in the MasterCities table, all the Countries/Cities that originally exist in the Master Table, except those that are in the VisitedCities
Have tried various selects where not in etc. but cannot get syntax right or its deleting too much stuff
Something like this (untested)delete mc
from MasterCities mc
where exists (select 1 from VisitedCities vc where vc.Country = mc.Country and vc.City = mc.City)
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
February 19, 2018 at 8:25 am
It sounds as though you want the "DELETE FROM x JOIN y" type of query shown in the example D of the DELETE syntax - https://docs.microsoft.com/en-us/sql/t-sql/statements/delete-transact-sql#d-using-joins-and-subqueries-to-data-in-one-table-to-delete-rows-in-another-table
For example:
--table variables because lazy
DECLARE @MasterCities TABLE (Country varchar(20), city varchar(20))
DECLARE @VisitedCities table (Country varchar(20), city varchar(20))
--dummy data
INSERT INTO @MasterCities values('UK', 'London'), ('UK', 'Birmingham'), ('UK','Edinburgh'), ('UK','Belfast'), ('Canada', 'London')
INSERT INTO @VisitedCities VALUES ('UK', 'London'), ('UK', 'Edinburgh')
--see what we've got so far
SELECT * FROM @MasterCities
SELECT * FROM @VisitedCities
--delete matching records from Master table
DELETE m
FROM
@MasterCities AS m
INNER JOIN @VisitedCities AS v ON v.Country = m.Country
AND v.city = m.city;
--check the results
SELECT * FROM @MasterCities
SELECT * FROM @VisitedCities
Thomas Rushton
blog: https://thelonedba.wordpress.com
February 19, 2018 at 10:16 am
Phil Parkin - Monday, February 19, 2018 8:23 AMxxx-593414 - Monday, February 19, 2018 8:02 AMI have two Tables (In Ms Sql Server)
Table Name Column Names
MasterCities Countryy City
VisitedCities Country CityI want to delete from the MasterCities table any Country/Cities I have visited
I don't have any primary keys as the second table is coming from an outside source so need to be able to match by Coountry and City in both tables
So I want to end up , in the MasterCities table, all the Countries/Cities that originally exist in the Master Table, except those that are in the VisitedCities
Have tried various selects where not in etc. but cannot get syntax right or its deleting too much stuffSomething like this (untested)
delete mc
from MasterCities mc
where exists (select 1 from VisitedCities vc where vc.Country = mc.Country and vc.City = mc.City)
That's perfect and worked fine
Many thanks
February 19, 2018 at 10:17 am
ThomasRushton - Monday, February 19, 2018 8:25 AMIt sounds as though you want the "DELETE FROM x JOIN y" type of query shown in the example D of the DELETE syntax - https://docs.microsoft.com/en-us/sql/t-sql/statements/delete-transact-sql#d-using-joins-and-subqueries-to-data-in-one-table-to-delete-rows-in-another-tableFor example:
--table variables because lazy
DECLARE @MasterCities TABLE (Country varchar(20), city varchar(20))
DECLARE @VisitedCities table (Country varchar(20), city varchar(20))--dummy data
INSERT INTO @MasterCities values('UK', 'London'), ('UK', 'Birmingham'), ('UK','Edinburgh'), ('UK','Belfast'), ('Canada', 'London')
INSERT INTO @VisitedCities VALUES ('UK', 'London'), ('UK', 'Edinburgh')--see what we've got so far
SELECT * FROM @MasterCities
SELECT * FROM @VisitedCities--delete matching records from Master table
DELETE m
FROM
@MasterCities AS m
INNER JOIN @VisitedCities AS v ON v.Country = m.Country
AND v.city = m.city;--check the results
SELECT * FROM @MasterCities
SELECT * FROM @VisitedCities
Many thanks
February 21, 2018 at 2:38 pm
ThomasRushton - Monday, February 19, 2018 8:25 AM
>> I have two Tables (In Ms Sql Server) <<
Where is the DDL for these alleged tables? Why doesn't your narrative. Follow any ISO 11179 naming rules? Now we have to do everything for you, because you can bother to read even the front end of this form before posting. Are you always this rude to people that you're asking to help you for free?
Then you screwed up at a more fundamental level. The metadata term. "Master" was derived from tape filesystems in the 1950s. A master tape was a magnetic tape file that held the most current information, this terminology was then carried over in network database systems. It has absolutely no place in RDBMS. Next you confused attributes and entities. Being visited is not a whole separate kind of entity. It is a status (state of being) or attribute of the city.
CREATE TABLE Cities
(country_code CHAR(3) NOT NULL, -- look up ISO country code
city_name VARCHAR(20) NOT NULL,
PRIMARY KEY (country_code, city_name),
visitation_count INTEGER NOT NULL DEFAULT 0 CHECK (visitation_status I>= 0));
>> I want to delete from the MasterCities [sic] table any Country/Cities I have visited
I don't have any primary keys as the second table is coming from an outside source so need to be able to match by Coountry and City in both tables <<
Every table to model a distinct and totally different set of entities. You visiting a city does not change its very nature any more than your sitting in an automobile would turn it into a squid. What you're doing is mimicking the way we would have done this with decks of punch cards in the 1950s.
In some destroying information, let's count the number of times you visited a particular city. Obviously that's going to be zero or more visits. And you'll do it with an update statement
Please post DDL and follow ANSI/ISO standards when asking for help.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply