How to get unique customer names those have different IDS - sql

I am working with a table that contains Account_No as unique ID, Customer_Name, Building_Name. The table below is an example:
It can be seen for few cases there are same customer name and same building however different Account_No. I need to remove duplicate names even though they have unique Account_No. Building_Name and Customer_Name are ties together. For example "William----Science City" and "William-----River Club" should be count as two customers since they are residing in different buildings. The result table should look as below;
I need to use SQL for creating the resulting table. Kindly use Customer Table as the reference for SQL query. Thanks

Select Min(Account_No) As Account_No
,Customer_Name,Building_Name
From Customer_Table
Group By Customer_Name, Building_Name

Related

Moving data between tables on a specific condition

I have two tables. In table Order, I have a column named customer_name. I am going to create a column named customer_id and customer_name is going to be dropped.
Before I drop that column, we have to look at the table Customer. Customer has one column name that matches the customer_name from Order. I want to find the SQL syntax that will let me move the id from Customer into the customer_id in Order, on the condition that Order.customer_name = Customer.name.
I am trying to use postgresql and have to learn how to use that, but even using regular SQL I have not found the solution yet.
I have tried:
ALTER TABLE Order
SELECT INTO customer_id
id from Customer
WHERE Order.customer_name = Customer.name;
and
INSERT INTO Order (customer_id)
SELECT id
FROM Customer
WHERE Order.customer_name = Customer.name;
I am getting a syntax error.
Is there another way I should write the condition? Or is it because of pgadmin and I need to write some other way?
You seem to want something like this:
ALTER TABLE Orders ADD customer_id INT;
UPDATE Orders
SET customer_id = c.id
FROM Customers c
WHERE o.customer_name = c.name;
Note: After doing this, you should check that all orders have a valid id. Then you may need to clean up records afterwards where the names do not match.

duplicated rows in select query

I am trying to run a query to get all the customers from my database. These are my tables in a diagram :
when running the query by joining the table Companies_Customers and the Customers table based on the customerId in both tables(doesn't show in the join table in the pic), I get duplicate rows, which is not the desired outcome.
This is normal from a database standpoint since a Customer can be related to different companies (Companies can share single customer).
My question is how do I get rid of the duplication via SQL.
There can be 2 approaches to your problem.
Either only select data from Customers table:
SELECT * FROM Customers
Or select from both tables joined together, but without CompanyName and with GROUP BY CompanyCustomerId - although I highly suggest the first approach.

Will the use of count(distinct depositor.account_number) make a difference here

Given Database Schema:
[The primary key for the tables is mentioned in bold)
account(account_no , branch_name,balance)
depositor(customer_name,account_number) {No key was stated here}
customer(customer_name,customer_street,customer_city)
The question required to write the SQL query for - Find average balance for each customer who lives in Harrison and has atleast 3 accounts.
I wrote the following SQL query:
select depositor.customer_name,avg(balance)
from depositor,account,customer
where depositor.account_number=account.account_number and
depositor.customer_name=customer.customer_name and
customer_city='Harrison'
group by depositor.customer_name
having count(depositor.account_number) >=3
My textbook mentions of the query as:
select depositor.customer_name,avg(balance)
from depositor,account,customer
where depositor.account_number=account.account_number and
depositor.customer_name=customer.customer_name and
customer_city='Harrison'
group by depositor.customer_name
having count( distinct depositor.account_number) >=3
Would placing distinct here lead to a change in result ?
According to my analysis, The cross-product of the resultant relation (depositoraccountcustomer) will have the candidate key as
customer_name account_number so distinct would not add any value here.
It seems that depositor table stores unique combinations of customer_name and account_number, meaning that adding DISTINCT in the count should not make a difference.
But if the table was a fact table which had repeating instances of the same account_number you wouldn't want to count the same account number twice and in that case, it would have made a difference.
But in your case, it should not make a difference since the other two tables also seem to contain unique combinations of their respective fields.
Since the depositor table doesn't have a primary key constraint it would be possible to duplicate a name-account pairing. Your textbook likely hasn't discussed composite keys so this was possibly intended as a way to catch that scenario. Unfortunately the extra row(s) would still throw off the average calculation though getting the three accounts qualification correct.

SQL query with loop

I am having trouble with writing a SQL query in Access with foreign keys. There are two tables, 'Customers'(ID, Name, Surname) and 'Orders'(ID, Customer, Date, Volume). ID fields are primary, and Orders.Customer is a foreign key linked to Customers.ID, so a customer can have many orders or none.
My goal is to do a search on customers based on many criteria, one of which being if customers have at least an order which volume is superior to a certain quantity. I tried joins with SELECT DISTINCT but it still gives me duplicate results, plus I had to create an empty order for every customer without orders if the query didn't use the above condition.
Does anyone have an idea about that? Maybe some special instruction on foreign keys or 2 separate queries?
Based on the information you give, i only can give you hints on what I think you're doing/understanding wrong :
SELECT DISTINCT does select you a unique record, not a unique value, so if your statement selects all fields (*), distinct won't help you much there.
My guess is you had to create an empty order for each customer because you used INNER JOIN, try LEFT OUTER JOIN instead
For example :
SELECT DISTINCT Customers.*
FROM Customers
LEFT OUTER JOIN Orders
ON (Orders.Customer = Customers.id)
WHERE Volume > put_your_value

SQL: check one table and enter values into another

I have a very simple MS Access User Table (USER_TABLE) consisting of 3 fields: Customer_Number, User_Name, and Email_Address. I have another table (NEW_USERS) that consist of new requests for Users. It has a User_Status field that is blank by default, and also has the Customer_Number, User_Name, and Email_Address fields.
Half of the new requests that come through are users already existing, so I want to set up a query that will check the USER_TABLE to determine if a new request exists or not, using the Email_Address field checked vs. the Customer_Number field. Complicating this is the fact that 1) Customer_Number is not unique (many Users exists for a single Customer Number) and 2) Users can have multiple accounts for different Customer Numbers. This results in 4 scenarios in the NEW_USERS table when checking vs. the USER_TABLE:
Email_Address does not exist for Customer Number in USER_TABLE (New)
Email_Address exists for Customer Number in USER_TABLE (Existing)
Email_Address does not exist for Customer Number in USER_TABLE, but exists for other Customer Numbers (New-Multi)
Email_Address does exist for Customer Number in USER_TABLE, and also exists for other Customer Numbers (Existing-Multi)
What I would like to do is run these checks and enter the corresponding result (New, Existing, New-Multi or Existing-Multi) into the User_Status field.
This seems like it would be possible. Is it possible to run 4 separate queries to make the updates to NEW_USERS.User_Status?
When you're working in Access, you really need a field that uniquely identifies each record. At the very least, some combination of field, like customerid and email.
Beyond that, since you have a few criteria to satisfy, the easiest way is probably to make a single select statement that compares data between the results of multiple select statements. Look into outer joins for picking the results from one table that are not found in another. Something like -
insert into user_table select customerid, email_address from
(select customerid, email_address from new_users inner join user_table on ...) as expr1,
(select customerid, email_address from new_users outer join user_table on ...) as expr2
where expr1.customerid = expr2.customerid and expr1.new_users = expr2.new_users
I recommend trying out the free stanford course on sql, theres a handy lesson on nesting your select statements - its a good way to get results that fit a lot of criteria. http://class2go.stanford.edu/
As an aside, they do a lot using syntax of 'specifying joins in the where claus' which is increasingly frowned upon, but much easier to understand.