query after inner joining two tables - sql

I have two tables TableA and TableB....
TableA
+----+-----------------+
| Id | ColName |
+----+-----------------+
| 1 | Name1 |
| 1 | Name2 |
| 1 | Name3 |
| 1 | Name4 |
| 1 | Name5 |
| 1 | Name6 |
| 1 | Name7 |
| 1 | Name8 |
+----+-----------------+
TableB
+----+-----------------+-----------------+-----------------+
| Id | ColName | CriticalityName | RefNo |
+----+-----------------+
| 1 | Name1 | High | RE1 |
| 1 | Name1 | Low | RE1,RE2 |
| 1 | Name1 | Low | RE1 |
| 1 | Name1 | Low | RE1 |
| 1 | Name1 | Low | RE1,RE2 |
| 1 | Name6 | High | RE3 |
| 1 | Name7 | High | RE3 |
| 1 | Name8 | High | RE1,RE#,RE3 |
| 1 | Name1 | High | RE1 |
| 1 | Name1 | High | RE4,RE5 |
| 1 | Name1 | High | RE1 |
| 1 | Name1 | High | RE1 |
| 1 | Name5 | High | RE1 |
| 1 | Name6 | High | RE1 |
| 1 | Name1 | High | RE1 |
| 1 | Name1 | High | RE3 |
| 1 | Name1 | High | RE2 |
| 1 | Name1 | High | RE4 |
| 1 | Name3 | High | RE5 |
| 1 | Name1 | Low | RE1 |
| 1 | Name1 | Low | RE1,RE#,RE3 |
| 1 | Name1 | High | RE5 |
| 1 | Name1 | Low | RE1,RE#,RE3 |
| 1 | Name8 | Low | RE4 |
+----+-----------------+-----------------+-----------------+
I need to filter using RefNo column, while inner joining two table TableA,TableB
I need a output as below:
+----+--------------+--------------+
| Id | ColName | RefNo |
+----+--------------+--------------+
| 1 | Name1 | RE1 |
| 1 | Name5 | RE1 |
| 1 | Name6 | RE1 |
| 1 | Name8 | RE1 |
+----+--------------+--------------+
here i'm inner joining TableA and TableB and filter with RefNo (passing RE1 as parameter).
In which ever row contains RE1, is displayed as output..
I dnt know to filter it after inner joining..
Please help me out..

Is this what you want?
select a.id, a.colname, 'RE1' as b.refno
from tableA a join
tableB b
on a.colname = b.colname and
',' + RefNo + ',' like '%,' + 'RE1' + ',%' ;
As a note: it is a really, really bad idea to store lists of things in a column. SQL offers a great data structure for storing lists of things, it is called a table and not a string column. In this case, you want a junction table, that has a separate row for each individual id (and/or ColName) and RefNo.

Related

SQL Query - Add column data from another table adding nulls

I have 2 tables, tableStock and tableParts:
tableStock
+----+----------+-------------+
| ID | Num_Part | Description |
+----+----------+-------------+
| 1 | sr37 | plate |
+----+----------+-------------+
| 2 | sr56 | punch |
+----+----------+-------------+
| 3 | sl30 | crimper |
+----+----------+-------------+
| 4 | mp11 | holder |
+----+----------+-------------+
tableParts
+----+----------+-------+
| ID | Location | Stock |
+----+----------+-------+
| 1 | A | 2 |
+----+----------+-------+
| 3 | B | 5 |
+----+----------+-------+
| 5 | C | 2 |
+----+----------+-------+
| 7 | A | 1 |
+----+----------+-------+
And I just want to do this:
+----+----------+-------------+----------+-------+
| ID | Num_Part | Description | Location | Stock |
+----+----------+-------------+----------+-------+
| 1 | sr37 | plate | A | 2 |
+----+----------+-------------+----------+-------+
| 2 | sr56 | punch | NULL | NULL |
+----+----------+-------------+----------+-------+
| 3 | sl30 | crimper | B | 5 |
+----+----------+-------------+----------+-------+
| 4 | mp11 | holder | NULL | NULL |
+----+----------+-------------+----------+-------+
List ALL the rows of the first table and if the second table has the info, in this case 'location' and 'stock', add to the column, if not, just null.
I have been using inner and left join but some rows of the first table disappear because the lack of data in the second one:
select tableStock.ID, tableStock.Num_Part, tableStock.Description, tableParts.Location, tableParts.Stock from tableStock inner join tableParts on tableStock.ID = tableParts.ID;
What can I do?
You can use left join. Here is the demo.
select
s.ID,
Num_Part,
Description,
Location,
Stock
from Stock s
left join Parts p
on s.ID = p.ID
order by
s.ID
output:
| id | num_part | description | location | stock |
| --- | -------- | ----------- | -------- | ----- |
| 1 | sr37 | plate | A | 2 |
| 2 | sr56 | punch | NULL | NULL |
| 3 | sl30 | crimper | B | 5 |
| 4 | mp11 | holder | NULL | NULL |

How to get non-existing rows in many to many relationship

I am having following three tables
products
------------------
| id | name |
------------------
| 1 | Ariel |
| 2 | Milk |
------------------
price_list
-----------------------
| id | name |
-----------------------
| 1 | Trade Price |
| 2 | Net Price |
| 3 | Retail Price |
-----------------------
product_prices (it has only two records for product 'Ariel')
----------------------------------------------
| id | product_id | price_list_id | price |
----------------------------------------------
| 1 | 1 | 1 | 100 |
| 2 | 1 | 2 | 110 |
----------------------------------------------
Desired Result:
------------------------------------------------------------
| id | product_name | prices |
------------------------------------------------------------
| | | | id | price_list_name | price | |
| 1 | Ariel | -------------------------------- |
| | | | 1 | Trade Price | 100 | |
| | | | 2 | Net Price | 110 | |
| | | | 3 | Retail Price | null | |
| | | -------------------------------- |
| | | | id | price_list_name | price | |
| 2 | Milk | -------------------------------- |
| | | | 1 | Trade Price | null | |
| | | | 2 | Net Price | null | |
| | | | 3 | Retail Price | null | |
-------------------------------- |
------------------------------------------------------------
I tried following query to get a cross between products and price_list
SELECT p.id,
p.NAME,
pl.id,
pl.NAME
FROM products p
CROSS JOIN price_list pl
WHERE pl.id NOT IN (SELECT product_id
FROM product_prices)
Any idea how to achieve the desired result?
I think something like this. I changed the names of the fields and tables for short. I also display all the fields.
select * from t1 cross join t2 left join t3 on t1.id = t3.t1_id and t2.id = t3.t2_id order by t1.id, t2.id;
+----+-------+----+--------------+------+-------+-------+-------+
| id | name | id | name | id | t1_id | t2_id | price |
+----+-------+----+--------------+------+-------+-------+-------+
| 1 | Ariel | 1 | Trade Price | 1 | 1 | 1 | 100 |
| 1 | Ariel | 2 | Net Price | 2 | 1 | 2 | 110 |
| 1 | Ariel | 3 | Retail Price | NULL | NULL | NULL | NULL |
| 2 | Milk | 1 | Trade Price | NULL | NULL | NULL | NULL |
| 2 | Milk | 2 | Net Price | NULL | NULL | NULL | NULL |
| 2 | Milk | 3 | Retail Price | NULL | NULL | NULL | NULL |
+----+-------+----+--------------+------+-------+-------+-------+
products -> t1
price_list -> t2
product_prices -> t3

Need to shift the data to next column, unfortunately added data in wrong column

I have a table test
+----+--+------+--+--+--------------+--+--------------+
| ID | | Name1 | | | Name2 |
+----+--+------+--+--+--------------+--+--------------+
| 1 | | Andy | | | NULL |
| 2 | | Kevin | | | NULL |
| 3 | | Phil | | | NULL |
| 4 | | Maria | | | NULL |
| 5 | | Jackson | | | NULL |
+----+--+------+--+--+----------+--+--
I am expecting output like
+----+--+------+--+--+----------+--
| ID | | Name1 | | | Name2 |
+----+--+------+--+--+----------+--
| 1 | | NULL | | | Andy |
| 2 | | NULL | | | Kevin |
| 3 | | NULL | | | Phil |
| 4 | | NULL | | | Maria |
| 5 | | NULL | | | Jackson |
+----+--+------+--+--+----------+--
I unfortunately inserted data in wrong column and now I want to shift the data to the next column.
You can use an UPDATE statement with no WHERE condition, to cover the entire table.
UPDATE test
SET Name2 = Name1,
Name1 = NULL

How to reorder column values in a table?

When I delete some rows from a table it looks like the following table after removing:
+-----+--------+----------+
| ID | Name | Address |
+-----+--------+----------+
| 1 | name1 | address1 |
| 2 | name2 | address2 |
| 8 | name8 | address8 |
| 9 | name9 | address9 |
+-----+--------+----------+
The problem is the ID isn't ordered properly.
I need it to be reordered like this:
+----+---------+----------+
| ID | Name | Address |
+----+---------+----------+
| 1 | name1 | address1 |
| 2 | name2 | address2 |
| 3 | name8 | address8 |
| 4 | name9 | address9 |
+-----+--------+----------+
Is there a command to reorder the column like shown?
I'm using sqlite
I think this type of query should do that:
UPDATE yourTable
SET ID = (SELECT COUNT(*) FROM yourTable ti WHERE ti.ID <= yourTable.ID)

SQL Join setting value from table2 to table1 where id is the same

i've the following table
+-----+------+
| sID | name |
+-----+------+
| 2 | MXX |
| 3 | ISS |
| 4 | FSS |
| 5 | SSA |
| 6 | PSA |
+-----+------+
and the following table
+-----+-------+
| sID | pname |
+-----+-------+
| 1 | qqq |
| 1 | yyy |
| 2 | zzz |
| 1 | lll |
| 2 | mmm |
| 3 | ttt |
| 3 | sss |
| 5 | xxx |
| 5 | iii |
+-----+-------+
and the join result should look like
+-----+-------+----------+
| sID | pname | supplier |
+-----+-------+----------+
| 1 | qqq | |
| 1 | yyy | |
| 2 | zzz | MXX |
| 1 | lll | |
| 2 | mmm | MXX |
| 3 | ttt | ISS |
| 3 | sss | ISS |
| 5 | xxx | SSA |
| 5 | iii | SSA |
+-----+-------+----------+
idea is to put the values of column name from first table in the second table where sID is the same
i tried Select * From TABLE1 c LEFT join TABLE2 T on c.sID=T.sID
Your left join is the wrong way around.
select table2.sid, pname, table1.name as supplier
from table2
left join table1 on table2.sid = table1.sid
or change your left join to a right join