Duplicate column name in SQL - sql

I've wanted to create a view in my sql but I've been getting this error:
Duplicate column name 'product_id'
Here's my query:
CREATE VIEW product_view AS
SELECT
*
FROM
products
JOIN storeitems on products.product_id = storeitems.product_id;
I believe creating an alias would solve the problem but then I don't know how to do it. I hope you could help me with this. Thanks!

You get the error because both tables in your query has a column named product_name and selected column names must be unique.
You make your aliases like this:
CREATE VIEW product_view AS SELECT p.product_name, si.product_name StoreItemsProductName -- continue with what you want to select...
FROM products p JOIN storeitems si
on p.product_id=si.product_id;

yes, creating an column alias would solve the problem and you do it like
select column1 as test_column
But if this your original query then I doubt you won't get this error since you are selecting from single table called products and obviously same table won't have multiple column with same name.
Anyway you can modify your query to be like
CREATE VIEW product_view AS
SELECT p.product_id as product_product_id,
si.product_id as storeItem_product_id
FROM
products p
JOIN storeitems si on p.product_id = si.product_id;

Probably both the products table and the storeitems table have a column named product_name.
CREATE VIEW product_view AS SELECT * FROM products p JOIN storeitems si on p.product_id=si.product_id;
here p will be the alias for the products table and si will be the alias for storeitems table

If your database supports the using clause and you only have one duplicate column name, then you could do:
select *
from products p join
storeitems si
using (product_id);
In general, though, I think it is better to list out the columns in a view. If one of the underlying tables changes the order or names of the columns, then this is safer.

Related

Is there a way to add a column to a view in ORACLE SQL?

I have a two tables
1.TABLE_STOCK with columns
Product_ID(primary key) and Product_unit_price
2.TABLE_SALES with columns Product_ID(foreign key) and Sales_unit_price
Now I wanted to create a view joining the two tables based on Product_ID and add a column PROFIT(which should be the difference between Sales_unit_price and Product_unit_price) to the view.
Is there a way to add a column(PROFIT) to a view?
You can do it as the way you've already described.
CREATE OR REPLACE VIEW V_PRODUCT
AS
SELECT K.PRODUCT_ID,
K.PRODUCT_UNIT_PRICE,
S.SALES_UNIT_PRICE,
S.SALES_UNIT_PRICE - K.PRODUCT_UNIT_PRICE AS PROFIT
FROM TABLE_STOCK K
INNER JOIN TABLE_SALES S ON S.PRODUCT_ID = K.PRODUCT_ID
You can have stock with no sales (but presumably not sales without some stock). Hence, I think you need an outer join:
CREATE OR REPLACE VIEW V_PRODUCT AS
SELECT st.PRODUCT_ID, st.PRODUCT_UNIT_PRICE,
s.SALES_UNIT_PRICE,
(s.SALES_UNIT_PRICE - K.PRODUCT_UNIT_PRICE) AS PROFIT
FROM TABLE_STOCK st LEFT JOIN
TABLE_SALES s
ON S.PRODUCT_ID = st.PRODUCT_ID;
You may need a FULL JOIN, if you can have sales with no stock.

In SQL Server, how can I change a column values depending on whether it contains a certain value?

Sorry if the title is a bit confusing... I'm trying to fix some data. Here goes the explanation:
Say I have two tables.
The first is a lookup table that is no longer in use.
The second has one varchar(50) column that sometimes has product names and
sometimes has product ids from the old lookup table.
The idea is to convert all the PID values in the Product table into the product names. Here's a pic i made up to help illustrate it:
The lookup table is much larger, that's just an example. So i'd guess it would be an update statement that would use the ProductsLookup.Name value if the Product value was found to be in the ProductsLookup.PID set? How would this look in SQL?
Thanks much for the help,
Carlos
You need to put inner join with both these tables "ProductsLookup" and "Products" on PID and Product column. But Product column in "Products" table is varchar so you have to apply ISNUMERIC function on this column to make sure that join works fine. below is example
UPDATE
P
SET
P.Product=PL.Name
FROM
Products P
INNER JOIN ProductsLookup PL ON PL.PID=CASE WHEN ISNUMERIC(P.Product)=1 THEN P.Product ELSE -1 END
You can do this with an update and join:
update p
set product = pl.name
from products p join
productslookup pl
on pl.pid = p.id;
A left join is not needed, because you only need to update the values that match in the lookup table.
I will caution you that performance might be an issue. The problem is the types between the two tables -- presumably pid is a number not a string. If so, you can create a computed column to change the type and an index on that column.

Join SQL tables, but with additional info from joined table

Here is a basic illustration of the issue
I have two tables I want to join.
One table contains information about a product sale. (product_sales)
The other contains keys for the names of locations. (states_keys)
Both contain the same column 'state_key'.
The states_keys table has an additional column called 'state_names'.
I would like to join the table so that I can view the accurate state name that coordinates to each product sale.
So, I want to join where each table's state_key are = but display the additional column state_names from table states_keys.
SELECT product_sales.*, state_names
FROM product_sales
INNER JOIN states_keys
ON product_sales.state_key = states_keys.state_key
Try with something like this:
Select prod.*, state.state_names
from product_sales prod
inner join states_keys state on state.state_key = prod.state_key

Retrieve different row from same table

i hava a set of following tables
customer(cus_id,cus_name);
jointAccount(cus_id,acc_number,relationship);
account(acc_number,cus_id)
now i want to create a select statement to list all the jointAccounts,
it should included the both customer name, and relationship.
I have no idea how to retrieve both different user name, is that possible to do this?
Generally speaking, yes. I'm assuming you mean you want to get customer info for both sides of the joint account per your jointAccount table. Not sure what database you're using so this answer is assuming MySQL.
You can join on the same table twice in a single SQL query. I'm assuming you have not yet created your tables, as you have cus_id listed twice in the jointAccount table. Typically these would be something like cus_id1 and cus_id2, which I've used in my sample query below.
Example:
SELECT c1.cus_id AS cust1_id, c1.cus_name AS cust1_name
, c2.cus_id AS cust2_id, c2.cus_name AS cust2_name, j.relationship
FROM customer c1
INNER JOIN jointAccount j
ON c1.cus_id = j.cus_id1
, customer c2
INNER JOIN jointAccount j
ON c2.cus_id = j.cus_id2
I haven't tested this but that's the general idea.
try this query:
SELECT * FROM jointAccount a LEFT JOIN customer c ON a.cus_id = c.cus_id;
just replace the * with the name of the columns you need.

how to select rows from table

my table contains category_name and parent_category_Id column
My parent_category data contains, the same table primary key id.
i need to select the all rows and insted my parent_category_Id i need to select the catogry_name
I think this is what you're after, though it's hard to discern from the question:
Select c.*
From category c
Join parent_category pc ON c.parent_category_id = pc.id
Where pc.category_name = 'Some Name'
Try something like:
SELECT c.category_name, p.category_name
FROM categories c LEFT JOIN parent_categories p
ON c.parent_id = p.id
PS: you may think about restructuring your database, it would make more sense to store all the categories in the same table. See for instance: http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/
You should restructure your database to have one table with:
id, name, and parent columns, with the parent column referencing the same table's id column. Your current database is not normalized and will likely cause issues in the future.
At a minimum you should have an auto_increment id column in the categories table.
The other answers here are correct (depending on the SQL server you are using).