SQL 语法不在列表中?

发布于 2024-09-15 23:51:00 字数 1041 浏览 3 评论 0原文

我正在尝试开发一个 T-SQL 查询来排除另一个表“B”中的所有行。另一个表“B”有 3 列组成其 PK,总共 136 行。所以我想选择表“A”中的所有列减去表“B”中的列。我该怎么做?我认为这个查询不正确,因为我仍然收到重复记录错误:

CREATE TABLE #B (STUDENTID VARCHAR(50), MEASUREDATE SMALLDATETIME, MEASUREID VARCHAR(50))
INSERT #B 
SELECT studentid, measuredate, measureid
  from [J5C_Measures_Sys] 
GROUP BY studentid, measuredate, measureid
  HAVING COUNT(*) > 1

insert into J5C_MasterMeasures (studentid, measuredate, measureid, rit)
select A.studentid, A.measuredate, B.measurename+' ' +B.LabelName, A.score_14
from [J5C_Measures_Sys] A
join [J5C_ListBoxMeasures_Sys] B on A.MeasureID = B.MeasureID 
  join sysobjects so on so.name = 'J5C_Measures_Sys' AND so.type = 'u'
join syscolumns sc on so.id = sc.id and sc.name = 'score_14' 
join [J5C_MeasureNamesV2_Sys] v on v.Score_field_id = sc.name
 where a.score_14 is not null AND B.MEASURENAME IS NOT NULL
 and (A.studentid NOT IN (SELECT studentid from #B) 
and a.measuredate NOT IN (SELECT measuredate from #B)
and a.measureid NOT IN (SELECT measureid from #B))

I am trying to develop a T-SQL query to exclude all rows from another table "B". This other table "B" has 3 columns comprising its PK for a total of 136 rows. So I want to select all columns from table "A" minus those from table "B". How do I do this? I don't think this query is correct because I am still getting a duplicate record error:

CREATE TABLE #B (STUDENTID VARCHAR(50), MEASUREDATE SMALLDATETIME, MEASUREID VARCHAR(50))
INSERT #B 
SELECT studentid, measuredate, measureid
  from [J5C_Measures_Sys] 
GROUP BY studentid, measuredate, measureid
  HAVING COUNT(*) > 1

insert into J5C_MasterMeasures (studentid, measuredate, measureid, rit)
select A.studentid, A.measuredate, B.measurename+' ' +B.LabelName, A.score_14
from [J5C_Measures_Sys] A
join [J5C_ListBoxMeasures_Sys] B on A.MeasureID = B.MeasureID 
  join sysobjects so on so.name = 'J5C_Measures_Sys' AND so.type = 'u'
join syscolumns sc on so.id = sc.id and sc.name = 'score_14' 
join [J5C_MeasureNamesV2_Sys] v on v.Score_field_id = sc.name
 where a.score_14 is not null AND B.MEASURENAME IS NOT NULL
 and (A.studentid NOT IN (SELECT studentid from #B) 
and a.measuredate NOT IN (SELECT measuredate from #B)
and a.measureid NOT IN (SELECT measureid from #B))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

难如初 2024-09-22 23:51:00

使用 NOT EXISTS...NOT IN 不会过滤掉 NULLS

insert into J5C_MasterMeasures (studentid, measuredate, measureid, rit)
select A.studentid, A.measuredate, B.measurename+' ' +B.LabelName, A.score_14
from [J5C_Measures_Sys] A
join [J5C_ListBoxMeasures_Sys] B on A.MeasureID = B.MeasureID 
  join sysobjects so on so.name = 'J5C_Measures_Sys' AND so.type = 'u'
join syscolumns sc on so.id = sc.id and sc.name = 'score_14' 
join [J5C_MeasureNamesV2_Sys] v on v.Score_field_id = sc.name
 where a.score_14 is not null AND B.MEASURENAME IS NOT NULL
 AND NOT EXISTS (select 1 from  #B where #b.studentid = A.studentid
 and a.measuredate = #B.measuredate
 and a.measureid = #B.measureid)
  and not exists (select 1 from J5C_MasterMeasures z 
                  where z.studentid = A.studentid)

只是让你知道,看看 选择一个表中不存在于另一个表中的所有行

基本上有至少有 5 种方法从 onr 表中选择不在另一个表中的所有行

  • NOT IN
  • NOT EXISTS
  • LEFT 和 RIGHT JOIN
  • OUTER APLY (2005+)
  • EXCEPT (2005+)

use NOT EXISTS...NOT IN doesn't filter out NULLS

insert into J5C_MasterMeasures (studentid, measuredate, measureid, rit)
select A.studentid, A.measuredate, B.measurename+' ' +B.LabelName, A.score_14
from [J5C_Measures_Sys] A
join [J5C_ListBoxMeasures_Sys] B on A.MeasureID = B.MeasureID 
  join sysobjects so on so.name = 'J5C_Measures_Sys' AND so.type = 'u'
join syscolumns sc on so.id = sc.id and sc.name = 'score_14' 
join [J5C_MeasureNamesV2_Sys] v on v.Score_field_id = sc.name
 where a.score_14 is not null AND B.MEASURENAME IS NOT NULL
 AND NOT EXISTS (select 1 from  #B where #b.studentid = A.studentid
 and a.measuredate = #B.measuredate
 and a.measureid = #B.measureid)
  and not exists (select 1 from J5C_MasterMeasures z 
                  where z.studentid = A.studentid)

Just so you know, take a look at Select all rows from one table that don't exist in another table

Basically there are at least 5 ways to select all rows from onr table that are not in another table

  • NOT IN
  • NOT EXISTS
  • LEFT and RIGHT JOIN
  • OUTER APLY (2005+)
  • EXCEPT (2005+)
独自唱情﹋歌 2024-09-22 23:51:00

以下是使用left join进行差分运算的通用解决方案:

select * from FirstTable
left join SecondTable on FirstTable.ID = SecondTable.ID
where SecondTable.ID is null

当然,您的join on子句可能会更复杂,但基本操作是相同的。

Here is a general solution for the difference operation using left join:

select * from FirstTable
left join SecondTable on FirstTable.ID = SecondTable.ID
where SecondTable.ID is null

Of course yours would have a more complicated join on clause, but the basic operation is the same.

伪装你 2024-09-22 23:51:00

我认为你可以在子查询中使用“NOT IN”,但你说你有一个多字段键?

我正在考虑使用左外连接,然后在右侧测试空值...

马丁。

I think you can use "NOT IN" with a subquery, but you say you have a multi-field key?

I'd be thinking about using a left outer join and then testing for null on the right...

Martin.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文