简体   繁体   中英

SQL adding a new column to check whether ID appears in another table

I work with student data and have two tables with the same structure but different data sets. I would like to add a new column with a record of '0' or '1' to determine whether or not the student appears in the other table. Example:

Table 1:
s_id    s_year  s_term  s_crs       NewColumn(was student enrolled in 2016?)
123456  2017    Fall    Math 1010   1
654321  2017    Fall    Eng 1010    0

Table 2:
s_id    s_year  s_term  s_crs 
123456  2016    Fall    Math 2010
432516  2016    Fall    Eng 2010

How would you go about doing this?

SELECT s_id, s_year, s_term, s_crs 
(CASE 
    WHEN S_ID IN (select s_id from table2)
        THEN '1'

or something of that nature? I am unsure how to write this with a join

You could left join with the second table, and see if it the resulting column is not null:

SELECT    t1.s_id, t1.s_year, t1.s_term, t1.s_crs, 
          CASE WHEN t2.s_id IS NOT NULL THEN 1 ELSE 0 END AS newcolum
FROM      table1 t1
LEFT JOIN table2 t2 ON t1.s_id = t2.s_id

assuming table 1 is the result ... using subquery solely based on the s_id and s_year... if there is another requirement please update OP.

SELECT
  s_id,
  s_year,
  s_term,
  s_crs,
  ISNULL((SELECT
    1
  FROM table2 t2
  WHERE t2.s_id = t1.s_id
  AND t2.s_year = 2016), 0) [NewCol 2016]
FROM table1 t1

Assuming the s_id is a common identificator

SELECT t1.s_id, t1.s_year, t1.s_term, t1.s_crs, 1 as NewColumn
FROM table1 t1
WHERE EXISTS (SELECT 1 FROM Table2 t2 WHERE t1.s_id = t2.s_id)
UNION 
SELECT t1.s_id, t1.s_year, t1.s_term, t1.s_crs, 0 as NewColumn
FROM table1 t1
WHERE NOT EXISTS (SELECT 1 FROM Table2 t2 WHERE t1.s_id = t2.s_id)

You can do this

ALTER TABLE Table1 ADD NewColumn BIT NOT NULL DEFAULT (0)

At this point all your items in the Table1 will be 0, now you just update the needed items with 1

UPDATE Table1
SET NewColumn = 1
WHERE ID IN (SELECT ID FROM Table2)

You can use Left join

SELECT    t1.s_id, t1.s_year, t1.s_term, t1.s_crs, 
          CASE WHEN t2.s_Year = '2016' THEN 1 ELSE 0 END AS [NewColumn(was student enrolled in 2016?)]
FROM      table1 t1
LEFT JOIN table2 t2 ON t1.s_id = t2.s_id
  create table #tbl1
  (
    s_id int,
    s_year varchar(4),
    s_term varchar(4),
    s_crs varchar (10)
  )

  insert into #tbl1 values(123456,'2017','Fall','Math 1010')
  insert into #tbl1 values(654321,'2017','Fall','Eng 1010')

  create table #tbl2
  (
    s_id int,
    s_year varchar(4),
    s_term varchar(4),
    s_crs varchar (10)
  )

  insert into #tbl2 values(123456,'2016','Fall','Math 2010');
  insert into #tbl2 values(432516,'2016','Fall','Eng 2010')


  select a.s_id,a.s_year,a.s_term,a.s_crs,case when b.s_id is null then '0' else '1' end as NewColumn
  from #tbl1 a left outer join #tbl2 b on a.s_id = b.s_id



  drop table #tbl1
  drop table #tbl2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM