Need HELP! with - SQL Plus Select multiple columns - sql

Hi I am using sql plus and I need to select 5 columns from 4 tables and show the result. This is the code that i used and the error I get.
SELECT CustomerID, OrderID, AircraftID, Quantity, TotalCost
FROM Customer_Table, Order_Table, Aircraft_Table, Orderline;
ERROR at line 1: ORA-00918: column ambiguously defined
What is the code to get these columns and display them from multiple tables. Please help with this.

The reason is that you tried to execute an SQL statement that joined two or more tables, where a column with the same name exists in both tables.
Exemple with this SQL query
SELECT suppliers.supplier_id, quantity
FROM suppliers, orders
WHERE suppliers.supplier_id = orders.supplier_id;
Since the supplier_id column exists in both the suppliers and orders table, you need to prefix the column with the table name as follows:
Moreover you need to link your tables between them. If I take the example above you should do this kind of thing
WHERE suppliers.supplier_id = orders.supplier_id;
Means you link the table suppliers with the table orders by the column supplier_id
This article may help you to understand about your issue:
http://www.sitepoint.com/understanding-sql-joins-mysql-database/

I don't know SQL plus specifically but in most SQL variants you should specify a table name then a column name. Such as SELECT Customer_Table.CustomerID ...... this will disambiguate which column should be used in the result projection.

The error means one or more of those columns exist with the same name in those tables. Thats why it does not know which column to select from what table. If you could specify the table name and then the column, I think this should go away.

Related

How to see pull up all relevant info from a table given a list of values SQL

Currently I have a list of manufacturing numbers in a table called "Mfr_Numbers_Table".
I also have a master table called Billings
I would like to generate a query where I see all line items that matches with what is on Mfr_Numbers_Tables from the Billings Table.
How do I do this? I can just write 50 queries with a where clause being the Mfr_numbers, but that would take too long.
You need put the tables in a relation in your query.
This is done by a join on the tables on a field which is in both of them.
For example if this field is named MfrNumber:
Select Billings.*, Mfr_Numbers_Table.*
From Mfr_Numbers_Table Inner Join Billings
On Mfr_Numbers_Table.MfrNumber = Billings.MfrNumber
Usually you don't select all fields from all joined tables, but select what you really need.
Also you can still filter (Where) and/or order (Order By) the result.
The name of the joined field isn't relevant, but its datatype and for sure the content.
For more please read this article: Join tables and queries

SQL Query to get distinct results from one table and match those results to the another column in the table

I have an SQL table with product names in one column and a product category in another column. So each product belongs to a specific category. I am trying to figure out an sql command that will return distinct values from the product name column but will also display the product category each product belongs to. Any ideas?
You can try like this,
SELECT Distinct([ProductName]),[ProductCategeory] FROM [DB].[dbo].[tblProduct]

(SQL) How do I differentiate 2 columns from different tables with the same name when selecting?

I'm using an Oracle 12c SQL server. The goal is to create a view containing each company and the drugs it produces.
How can I differentiate two columns with the exact same name but located in different tables using SELECT?
All relevant code below, including results with error.
I understand why I might be getting a duplicate name error as they both have the same header "name", but thought I handled it by identifying the table beforehand (i.e. pc.name and dg.name). Help!
SQL Tables Being Joined:
SQL Column Naming Error:
You have ambiguous column names in output from your view:
pc.name, dg.name
Adding alias for columns should solve this:
pc.name as pc_name, dg.name as dg_name

Assistance with part of a join

I'm a college student and the database I'm working with is purely fictional but part of it requires me to make a query that is a join.
What I have are 3 tables each with part of the data but also needing to use two of those tables as conditions based off the main table. What I mean is I have an employee table, order table and customer table and the only thing any two of them have in common is the ID of either the employee or the customer is part of the order table.
Now what I am trying to do is create a join statement that will get certain information from the employee and customer tables and only those that both the employee and the customer are also on the same line in the order table. How should i make this type of conditional statement?
Any example using the same basic scenario will work I can use that to help me build my own query.
This is what I have right now:
SELECT [Customer/Vendor_Info_local].Name_of_customer,
Employee_Info_local.Employee_Name
FROM Employee_Info_local,
[Customer/Vendor_Info_local],
Order_Information_local
WHERE (([Customer/Vendor_Info_local].[Customer/VendorID] =
[Order_Information_local].[Cu‌​srtomer/VendorID])
AND
([Employee_Info_local].[EmployeeID] = [Order_Information_local].[EmployeeID]));
I keep getting a type mismatch error when i try to use it and honestly not even sure what that means.

SQL Query to count multiple values from one table into specific view

I like to request your help. I can get the results seperated but now i want to create a query which has it perfect for a external person. my explanation:
I have a statistics database with in this database a table when some records comes in and each records has several columns with values etc...
Now one of these columns is called "MT"
MT Column can have only one of the following values per records: A,B,C,D,E
The records also have a columne called TotalAmount which indicate a size of a value outside the database. This TotalAmount column is numeric without decimals and can have a value between 1 and 10.000.
And the last part is the records it self, the table has X amount of records.
So Basicly i need to create a query which seperates each MT value and calculates the amount of records per MT and the sum of TotalAmount.
This is on SQL Server 2005.
Many thanks for your assistance!
Very hard to guess without a full db schema. But I think you need.
SELECT MT, Count(*), SUM (TotalAmout)
FROM YourTable
GROUP BY MT