Insert into table with multiple select including select distinct from two tables - sql

I have two SQL Server tables (simplified) below:
Table1
| productname | certno | expdate | company
+-------------+--------+------------+---------
| abc | 123 | 2020-01-01 | ABC
| def | 123 | 2020-01-01 | ABC
| qwe | 456 | 2020-01-02 | ABC
| asd | 999 | 2020-02-02 | DEF
| .. | .. | .. | ..
Table2
| companyid | company
+-----------+---------
| 1 | ABC
| 2 | DEF
| .. | ..
And I need to insert data from these 2 tables into another table for distinct certno, Table3 should be like this:
| certid | companyid | certno | expdate | createddate | is_null
+--------+-----------+--------+------------+-------------+---------
| 1 | 1 | 123 | 2020-01-01 | 2020-09-12 | 1
| 2 | 1 | 456 | 2020-01-02 | 2020-09-12 | 1
| 3 | 2 | 999 | 2020-02-02 | 2020-09-12 | 1
| .. | .. | .. | .. | .. | ..
I'm currently using this query, I need to create a script so that next batch of update is just in one query.
insert into Table3 (certno, expdate, is_null)
select distinct certno, expdate
from Table1
where company like '%ABC%';
--then I will update Table3 to complete it
update Table3
set companyid = '1',
createddate = getdate(),
is_null = '1'
where is_null is null; --edited**
--then repeat the above until I have all companies registered as companyid in Table3
But this will be a hassle when it involved more companies and lots of data and I was thinking of using multiple select including select distinct but I'm stuck right here:
insert into Table3 (companyid, certno, expdate, createddate)
select
(select companyid from Table2
where company in (select company from Table1)
),
(select distinct certno),
expdate,
getdate()
from Table1;
I did not run the script to see whether it works or not because I don't want to mess up the Table3 data and I feel that the script is not going to work.
Is there any way to just run the query in one script?

I think you want insert . . . select with join:
insert into Table3 (companyid, certno, expdate, createddate, is_null)
select distinct t2.companyid, t1.certno, t1.expdate, getdate(), 1
from Table1 t1 join
Table2 t2
on t1.company = t2.company
where t1.company like '%ABC%';
You can remove the where clause to insert all companies in one insert.

Looks like you just need to expand the columns in the SELECT:
INSERT INTO dbo.Table3 (certno, expdate, companyid, createdate)
SELECT DISTINCT
certno,
expdate,
company,
GETDATE()
FROM dbo.Table1;

Related

Update the column of table1 and get it on table2

How can I create a query where I can update the table1 column date that I get it on table2?
Here is some example of the tables
Table1:
| stud_id | start_date | birt_date | name | exam_date |
| s001 | 11/19/2018 | 05/20/1999 | john | 10/20/2018 |
| s003 | 01/01/2018 | 05/25/1995 | mike | 10/20/2018 |
| s005 | 12/23/2018 | 02/20/1999 | ed | 10/20/2018 |
| s005 | 12/23/2018 | 02/20/1999 | ed | 10/05/2017 |
Table2:
| stud_id | start_date | exam_date |
| s005 | 01/01/2017 | 10/20/2018 |
| s001 | 01/01/2017 | 10/20/2018 |
| s003 | 01/01/2017 | 10/20/2018 |
Basically I want to change just the start_date of the 3 so s006 will not change.
How can I accomplish that using query? I was thinking using in then select the table but I think its not gonna work.
I need to based on two or more column for the condition of my update so I want to update the table 1 where table1.stud_id = table2.stud_id and table1.exam_date = table2.exam_date
do join and update
update t1
set t1.stardate=t2.startdate,
t1.exam_date=t2.exam_date
from table1 t1 join table2 t2
on t1.stud_id=t2.stud_id
where t2.stud_id='s003' -- if you just need s003 update
You can also use CTE as well to update:
;With cte as
(
select t1.start_date as t1date,t2.start_date as t2date from table1 t1
join table2 t2
on t1.stud_id=t2.stud_id
and t1.exam_date=t2.exam_date
)
update cte set t1date=t2date
You can join the 2 tables:
UPDATE T1
SET Start_Date = T2.Start_Date
FROM Table1 AS T1
INNER JOIN Table2 AS T2
ON T1.stud_id = T2.stud_id
AND T1.exam_date = T2.exam_date

SQL get data that doesn't exist on another table

I'm trying to get a query that get me a data that exist in table1 but not on table2, but has the same id with data table2.
In the following example, I'm trying to get 'SecID-by_Bank' 12456 which shares the same ISIN as another 2 items in table2.
I've tried the following query, but it returned me every data that's not on table2, not just the one that shares the same ISIN.
Query:
SELECT tb1_isin, tb1_SecID_by_Bank
FROM table1
WHERE not EXISTS (
SELECT top 1 null
FROM table2
WHERE table1.tb1_ISIN = table2.tb2_isin
)
Table 1
--------------------------------------------
Row | SecID_by_Bank | Desc | ISIN
--------------------------------------------
1 | 421345 | BlaBla | US1354
--------------------------------------------
499 | 34345 | 2.US | XS1545
--------------------------------------------
500 | 45676 | 2/US | XS1545
--------------------------------------------
501 | 12456 | 2-US | XS1545
--------------------------------------------
Table 2
--------------------------------------------
Row | SecID_by_Bank | Desc | ISIN
--------------------------------------------
1 | 34345 | 2.US | XS1545
--------------------------------------------
2 | 45676 | 2/US | XS1545
Query result needed:
SecID_by_Bank | Desc | ISIN
-------------------------------------
| 12456 | 2-US | XS1545
-------------------------------------
What am I missing?
Thank you!
If you build query in Access try:
SELECT Table1.*
FROM Table1
WHERE (((Table1.[ISIN]) In (SELECT ISIN FROM Table2)) AND ((Table1.SecID_by_Bank) Not In (SELECT SecID_by_BANK FROM Table2)));
Or using 1 subquery:
SELECT Table1.*, Table2.ISIN
FROM Table2 RIGHT JOIN Table1 ON Table2.SecID_by_Bank = Table1.SecID_by_Bank
WHERE (((Table1.ISIN) In (SELECT ISIN FROM Table2)) AND ((Table2.ISIN) Is Null));
For query in SQLServer:
select distinct a.SecID_by_Bank, a.[desc], a.isin from #table1 a left join #table2 b
on a.SecID_by_Bank=b.SecID_by_Bank and a.isin=b.isin
join #table2 c on a.isin=c.isin
where b.isin is null

Showing only duplicate rows from table in postgres

I have table like this:
--------------------------------------
| id | name | phone_number | address |
--------------------------------------
| 1 | Ram | 9090909090 | Delhi |
| 2 | Shyam| 9865444456 | Mumbai |
| 3 | Mohan| 9756543455 | Chennai |
| 4 | Ram | 9090909090 | Delhi |
--------------------------------------
I want to return the rows having same column data. The result will be like this:
--------------------------------------
| id | name | phone_number | address |
--------------------------------------
| 1 | Ram | 9090909090 | Delhi |
| 4 | Ram | 9090909090 | Delhi |
--------------------------------------
This can be done using window functions which avoids the join on the aggregated data and is usually the faster way:
select *
from (
select id,name,phone_number,address
count(*) over (partition by name,phone_number,address) as cnt
from the_table
) t
where cnt > 1;
SELECT t2.id,t2.name,t2.phone_number,t2.address
FROM
(
SELECT name,phone_number,address
FROM tableName
GROUP BY name,phone_number,address
HAVING COUNT(*) > 1
) AS t1
INNER JOIN tableName t2
ON t1.name=t2.name AND t1.phone_number=t2.phone_number AND t1.address=t2.address
Please run the below query, (consider table name to be "data"), to get the desired result as follows:
SELECT * FROM data where name IN (SELECT name FROM data GROUP BY name HAVING COUNT(*) > 1);
SELECT T1.*
FROM
table_name T1
INNER JOIN table_name T2 ON
T1.name= T2.nam` AND
T1.phone_number= T2.phone_number AND T1.address= T2.address
WHERE T2.id <> T1.id

Ordering results by values in other table

I have two tables :
Table1
id | name | age | d_o_b
=====================================
1 | ASD | 22 | 12/01/1992
2 | QWE | 21 | 04/04/1993
3 | FRG | 24 | 04/04/1990
Table2
id | age
===============
1 | 22
2 | 21
3 | 24
Is it possible to order by two columns one from first Table1 and then by one column from Table2.
Something like ..
SELECT * FROM Table1 order by d_o_b , age in (SELECT * FROM Table2)
To order by a column in other table you may need to join them. This should work:
SELECT a.* FROM Table1 a
join table2 b
on a.id=b.id
order by d_o_b,b.age

Using SWITCH() to split data from a column into distinct columns, with associated data in reach row

I'm not quite sure how to properly phrase the question, but I am basically trying to develop an SQL query that SELECTs information from this table:
-------------------
| id | Val | Date |
|----|-----|------|
| 1 | A | 10/9 |
| 1 | B | 3/14 |
| 2 | A | 1/6 |
| 3 | A | 4/4 |
| 4 | B | 7/12 |
| 5 | A | 8/6 |
-------------------
And produces a table that looks like this:
------------------------------------------------
| id | Val_1 | Val_1_Date | Val_2 | Val_2_Date |
|----|-------|------------|-------|-------------
| 1 | A | 10/9 | B | 3/14 |
| 2 | A | 1/6 | | |
| 3 | A | 4/4 | | |
| 4 | | | B | 7/12 |
| 5 | A | 8/6 | | |
------------------------------------------------
I have already begun and developed the query to pull out the values in the Val fields into distinct columns:
SELECT * FROM
(
SELECT id, MAX(SWITCH( val='A', 'A')) as Val_1,
MAX(SWITCH( val='B', 'B')) as Val_2
FROM table1 GROUP BY id
)a
WHERE Val_1 IS NULL OR Val_2 IS NULL;
How would I expand on this to pull out their associated dates?
(I am using SWITCH() instead of CASE WHEN because I am using a driver similar to that of MS Access.)
Thanks!
I think following should work:
select id, SWITCH( val='A', 'A') as Val_1, SWITCH( val='A', Date) as Val_1_Date, SWITCH( val='B', 'B') as Val_2, SWITCH( val='B', Date) as Val_2_Date FROM table1 GROUP BY id
I do not prefer switches, so here is a query that does what you want without switches. This also answers your previous question.
Select distinct table1.ID, tableA.Val as Val_1, tableA.Date as Val_1_Date,
tableB.Val as Val_2, tableB.Date as Val_2_Date
FROM table1 left outer join
table1 as tableA on table1.id = tableA.id and tableA.Val = 'A' left outer join
table1 as tableB on table1.id = tableB.id and tableB.Val = 'B'
You can use ISNULL if that is preferred. This works because the first tables selects a distinct column of ID's, and the two joins get the A and B values. When creating selects using this method, make sure that you use tableA.Val = 'A' in the join conditions, and not in the where clause. Having tableA.Val = 'A' in the where clause will filter out all NULL's.