View new count column - sql

I have 2 tables. One who is like a transactional table
userId category
Second table also has
userId (as primary key)
and other columns (not important)
I wanted to create a view based on these 2 tables. Also, I want to have one column which puts into the cells the count from the transactional table who has the same id and eventually matches a special categorie f.e "car".
I thought of maybe using nested select statement, but since i'm a beginner i'm a little bit confused.

Try something like this.
select count(b.userId) as ID, b.category from table2 a inner join transactional b
on a.userId = b.userId where b.category= 'car' group by b.category;

Related

Inserting values from another table into a column where values are null

I have a table A with ID's only and another table B with two columns, ID and Product Number. The ID column in table B has nulls and Product Number has Product Numbers. I would like to update table B with the ID's in column in no specific order just so that the Product Number has ID's.
I have tried to use update but that has not worked, have tried insert but it just adds the ID's in A to the bottom of the list in B. Would like to do this in Microsoft SQL.
SQL code tried:
IF OBJECT_ID('tempdb..#ProductNum') IS NOT NULL DROP TABLE #ProductNum
SELECT ID
INTO #ProductNum
FROM Products
UPDATE [ProductCatalogue] PC
SET
PC.ID = Pn.ID
FROM #ProductNum Pn
INNER JOIN
[ProductCatalogue] PC
ON Pc.ID = Pn.ID
WHERE Pc.ID IS NULL
It sounds a lot like you would be better off having the ID-Column Autoincrement, instead of giving it the IDs from table A. This is already explained in this answer.
In case you actually need the specific IDs from table A, this SO thread might help you.
Solved the issue by creating auto increment columns on each table and called it Row_ID. Then I used Row_ID to join the tables together with some logic provided by #Chris above.

SQL: Put rows at top of query if a column exists elsewhere

I have a rows in a table that multiple users will view. Users can "favorite" a row, which adds the username/row key to a separate table.
I want to make a query that orders "favorites" to the top. I assume this would require a subquery, but I'm not sure on the best way to proceed.
One idea I had was to query my table where it exists in favorites, and then union all to a query where it doesn't exist in favorites. But I'm wondering if there's a better way.
You would want something like this:
select t.*
from t left join
favorites f
on t.pk = f.pk and f.userid = #userid
order by (case when f.pk is not null then 1 else 2 end);
pk is the primary key of the original table used to uniquely identify each row.

Update a field from one table to another, involving a 3 table join

I have a table I need to update the price field in. I need to update this field from a different price field from a different table. The only way I can get to the required table for the update is by joining another table into this query.
So in all I need to join 3 tables in the update.
Table A has the price field that needs to be updated. In this table I have the foreign key of the product.
Table A structure
-PK_TABLE_A,
-FK_TABLE_B,
-ITEM_COST,
-ITEM_PRICE (needs to be updated from Table C)
Table B is the product table which has the PK of the product. This table is used to access Table C.
I also need to filter Table B to only update a certain stock type.
Table B structure
-PK_TABLE_B,
-FK_TABLE_C,
-DESCRIPTION,
-QUANTITY,
-ITEM_TYPE (a string that needs to be added in where clause to only update records with certain type).
Table C has a foreign key back to Table B. It also contains the price field that I need to use to update the price field in table A
Table C structure
-PK_TABLE_C,
-FK_TABLE_B,
-PRICE (this is the field that I need to use to update the price field in table A)
-USED_DATE,
-ID
The DBMS I am using is Firebird.
I have tried to use sub queries to no avail. I regularly use a sub-select when using two tables to update, so something like
UPDATE table1 AS t1
SET t1.FK = (select table2.PK
FROM table2 INNER JOIN
table1
ON table2.FK = table2.PK
WHERE table2.name = t1.name)
I'm just struggling to use the same technique with a 3rd table incorporated.
I am not even sure if this is the correct way to go about this situation. I have looked on google, but most examples I have come across don't utilise the 3rd table.
Any help would be appreciated.
**edited to included more detail on table structure.
are you able to show us the table structures in more detail?
if both tableA and tableC have a foreign key that points back to tableB I don't think you need to include a three table join. you just need to
update tableA set ITEM_PRICE = SELECT(PRICE FROM TableC WHERE
TableA.FK_TABLE_B = TableC.FK_TABLE_B;
unless I'm missing something?
edited to reflect a better understanding of the problem
alright, I think I've got it this time:
update tableA set price =
(select price from tableC where tableA.fk_tableB = tableC.fk_tableB) where
(Select item_type from tableB where tableB.pk_tableB = tableA.fk_tableB) =
'$itemTypeVariable';
edited again with a better understanding of the problem

Returning results from a one-to-many relationship

I have Table A:
ID Name
Table B:
ID A_ID COLORS
A_ID is the FK to Table A
Table A to Table B is one to many.
I want to return all of Table B for a given Table A ID, in one SQL call.
I'm assuming this is the best idea, to return one result set in this scenario(?), then I'm going to scroll through the resultset to set all of the colors for a given person so I can set the color list.
Person(int id, String name, List<String>colors){
}
I trying to figure out the SQL, and I'm wondering in general if what I am doing is ok.
SELECT TableA.Name, TableB.COLORS FROM TableA INNER JOIN TableB ON TableA.ID=TableB.A_ID WHERE TableA.ID = "Given TableA ID"
Depending on how many times you're running this query and how many results it is returning most likely returning them all in one shot is the right way to do it.

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).