Suppose in table A,there are two columns say Agent and policy_number where agent=1101 has 20 policies under it.In table B,that same agent has 21 policies under it.I need a solution where it will check if the agent has the number of policies in Table B.If the records matches it will insert into a new table otherwise the non-matched records will go into an error table.
Related
I have a massive and messy database of facilities where there are many duplicates. Addresses have been entered in such a haphazard way that I will be making many queries to identify possible duplicates. My objective is for each query to identify the possible duplicates, and then a person actually goes through the list and marks each pairing as either "not a duplicate" or "possible duplicate."
When someone marks a facility pair as not a duplicate, I want to record that data pair in a table so when that when one of the queries would otherwise return that pairing, it is instead excluded. I am at a loss for how to do this. I'm currently using MS Access for SQL queries, and have rudimentary visual basic knowledge.
Sample of how it should work
Query 1 is run to find duplicates based on city and company name. It brings back that facilities 1 and 2, 3 and 4, 5 and 6 are possible duplicates. The first two pairings are duplicates I need to go fix, but that 5 and 6 are indeed separate facilities. I click to record that facilities 5 and 6 are not duplicates, which records the data in a table. When query 1 is run again it does not return that 5 and 6 are possible duplicates.
For reference, the address duplicates look something like this, which is why there need to be multiple queries
Frank's Garage, 123 2nd St
Frank's Garage LLC, LLC, 123 Second st
Frank's Garage and muffler, 123 2nd Street
Frank's, 12 2nd st
The only way I know to fix this is to create a master table of company names and associate this table PK with records in original table. It will be a difficult and tedious process to review records and eliminate duplicates from master and associate remaining PK of a duplicate group to the original records (as you have discovered).
Create a master table of DISTINCT company and address data from original table. Include autonumber field to generate key. Join tables on company/address fields and UPDATE a field in original table with this key. Have another field in original table to receive a replacement foreign key.
Have a number field (ReplacementPK) in master table. Sort and review records and enter the key you want to retain for company/address duplicates group. Build a query joining tables on original key fields, update NewFK field in original table with selected ReplacementPK from master.
When all looks good:
Delete company and address and original FK fields from original table.
Delete records from master where PK does not match ReplacementPK.
I have two schemas and a table in each schema. Table is called "concept". One Schema is "cdm" schema and other is "vocab" schema.
I would like to copy records from "concept" table of "vocab" schema to "concept" table of "cdm" schema based on certain conditions
concept table has a primary key called concept_id and several other non primary columns.
Ex: vocab.concept has 200 records. I would like to copy only records that satisfy a condition like concept_id > 150
Currently the concept table in CDM schema (cdm.concept) is filled with 150 records. So, I would like to add/insert the remaining 50 records (which is concept_id > 150)
Can you let me know how can I do this copy or kind of ignore duplicates and copy only new records (ie newly added 50 records)
INSERT INTO cdm.concept(concept_id,Name,Age,Info)
SELECT concept_id,Name,Age,Info
FROM vocab.concept
where concept_id > 150;
Questions
Is this above sql correct?
Is it the only way because my table might have more than 25 columns and I don't wish to type in column names every time I wish to copy.
Please note that table already exists in cdm schema. So, I don't wish to use "Create Table" approach
I have multiple tables that I need to delete rows from and they are all contingent on data from one table. For instance, I have a Users Table and in the Users Table the Users have a UserId. I am trying to get rid of all the data that is in relation to that one user in all the other tables.
For instance, there is a Members Table and in the Members table, there is a Foreign Key relationship to the Users table so it's tied by the UserId. The Members table can have multiple members associated with the UserId (A User can be members to more than one thing so it's a one to many relationship). Then there is also a Permissions table and in the Permissions table it has a foreign key relationship to the Members table. And in that table the Members can have multiple permissions (A Member can have permissions to more than one thing is that's a one to many relationship).
What I am needing to do is delete all the rows in the Permissions table that are in relation to the all the MemberId's that are in relation to the UserId from the User's Table (i.e. - Bob has a UserId of 7 and in the Members Table he is a Member of 3 things so he has 3 MemberId's associated to his name and in the Permissions Table those 3 MemberIds also have 3 Permissions associated to those 3 MemberIds. I am needing to delete all 9 Permission rows according to those 3 MemberIds, then delete all the 3 Member rows according to the 1 UserId, and then delete the one UserId according to a UserName).
I've tried to Inner Join multiple tables and also connect them with Unions but I am having difficulty of tying all those tables down to that one UserId and carrying that data through the flow of logic.
User has a UserId of 7
User is is associated with 3 MemberId's
The MemberId is associated with multiple PermissionId's
So I need to delete all those PermissionId's (I did a WHERE statement just on the User's MemberId's), all 3 instances of the MemberId's, and the User.
If you are aware of UserID that you want to delete from different table, then this is easy to handle from table Users and Members. You need to use a Sub Query when you will delete records from Permissions table. But if there are relation (PK/FK) established between tables, you need to maintain some sequence as below-
Important Note: Delete is a risky operation unless you have proper Backup. So please try to execute scripts on test data first.
At first you have to delete records from Permission table with following script-
DELETE FROM Permissions
WHERE MemberID IN
(
SELECT MemberID FROM Members
WHERE UserID = 7
)
In second step, you have to delete records from Members table as below-
DELEET FROM Member WHERE UserID = 7
In third step, you have to delete users from Users table as below-
DELETE from Users WHERE UserID = 7
In my application I need to perform searching on Employees Names,Customer Names etc. consider schema where there is an
Employees specific table EMPLOYEES ,
Customer specific table CUSTOMERS,
Core Table which contains Case Details related to an Employee
ALL these tables contain 10+million rows
now If a user enters a text in search box I am not aware of whether its customer name or Employee name etc.. now the general approach would be to search for customers in customers table, employee names in employee table and fetch those Ids and return rows with matching Ids from Core table but this would required joins of tables containing millions of rows how do I overcome this problem.
PS:
Using a FULL text search or using a HASH Table would be on my least priorities.
I am running Microsoft SQL Server 2008 R2, and pulling information from two tables to create one new table.
Table A has leads with a unique lead number and other information.
Table B has sales with a unique sales number, and the lead number associated with it.
Data from both tables are pulled into temp tables in SQL Server so I can change and update whatever I need, and the output of this will go into a new table.
One lead from Table A can have multiple sales associated with it in table B.
I want to update the Number of Sales column in Table A (Leads) based on how many times that lead number appears in Table B (sales). So if Table B (sales) has a lead number tied to seven (7) sales, the Number of Sales column in Table A (leads) will be updated to 7.
I have tried a few variations using the COUNT function but with no success. Any help would be appreciated.
This should work for you assuming the field name is leadNo:
update tablea
set sales = (select count(*)
from tableb
where tableb.leadNo = tablea.leadNo)
SQL Fiddle Demo