SQL查询:如果所有列的值都不等于一个值,则返回true。

huangapple go评论55阅读模式
英文:

Sql query: return true if all column values don't equal a value

问题

以下是翻译好的部分:

要查询所有的引擎并检查是否所有的engine_type都不等于-9,然后返回true(或使用位1),如果所有的engine_type都等于-9,则返回false(或使用位0)。

这是我试图查询的结果:

id  engine_name   approved_engine_types
---------------------------------------
1       abc                1
2       def	               0

以下是用于创建表格和插入数据的SQL:

CREATE DATABASE test;
USE [test]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[engine](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[engine_name] [varchar](50) NOT NULL,
CONSTRAINT [PK_engine] PRIMARY KEY CLUSTERED
(
	[id] ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [UQ_engine] UNIQUE NONCLUSTERED
(
	[engine_name] ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO


SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[engine_mode](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[engine_name] [varchar](50) NOT NULL,
	[engine_type] [varchar](50) NOT NULL,
CONSTRAINT [PK_engine_mode] PRIMARY KEY CLUSTERED
(
	[id] ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO


ALTER TABLE [dbo].[engine] ADD CONSTRAINT [DF_engine_engine_name] DEFAULT ('') FOR [engine_name]
GO
ALTER TABLE [dbo].[engine_mode] ADD CONSTRAINT [DF_engine_mode_engine_name] DEFAULT ('') FOR [engine_name]
GO
ALTER TABLE [dbo].[engine_mode] ADD CONSTRAINT [DF_engine_mode_engine_type] DEFAULT ('') FOR [engine_type]
GO
ALTER TABLE [dbo].[engine_mode] WITH CHECK ADD CONSTRAINT [FK_engine_mode_engine] FOREIGN KEY([engine_name]) REFERENCES [dbo].[engine] ([engine_name])
GO
ALTER TABLE [dbo].[engine_mode] CHECK CONSTRAINT [FK_engine_mode_engine] 
GO

USE [test]
GO
INSERT INTO [dbo].[engine] ([engine_name])
VALUES
('abc'), ('def')
GO

USE [test]
GO
INSERT INTO [dbo].[engine_mode] ([engine_name], [engine_type])
VALUES
('abc', 'R'), ('abc', '-9'), ('abc', 'S'), ('abc', '-9'), ('abc', 'T'), ('def', '-9'), ('def', '-9'), ('def', '-9')
GO

我已经尝试创建一个视图来执行此查询,但是没有成功。非常感谢您的帮助。

英文:

Say I have a table called Engine, with id as the primary key

id  engine_name
----------------
1       abc
2       def

and another table called EngineMode, with id as primary key and engine_name is a foreign key from the Engine table

id  engine_name   engine_type
------------------------------
1       abc        R
2       abc	       -9
3       abc	       S
4       abc	       -9
5       abc	       T
6       def	       -9
7       def	       -9
8       def	       -9

I want to query all Engines and check if all the engine_types do not equal -9, then I want to return true (or 1 using a bit), and if all engine_types equal -9 then return false (or 0 using a bit)

This is the result I'm trying to query

id  engine_name   approved_engine_types
---------------------------------------
1       abc                1
2       def	               0

Here is the SQL used to create the tables and insert the data

CREATE DATABASE test;
USE [test]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[engine](
[id] [int] IDENTITY(1,1) NOT NULL,
[engine_name] [varchar](50) NOT NULL,
CONSTRAINT [PK_engine] PRIMARY KEY CLUSTERED
(
[id] ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [UQ_engine] UNIQUE NONCLUSTERED
(
[engine_name] ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[engine_mode](
[id] [int] IDENTITY(1,1) NOT NULL,
[engine_name] [varchar](50) NOT NULL,
[engine_type] [varchar](50) NOT NULL,
CONSTRAINT [PK_engine_mode] PRIMARY KEY CLUSTERED
(
[id] ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[engine] ADD CONSTRAINT [DF_engine_engine_name] DEFAULT ('') FOR [engine_name]
GO
ALTER TABLE [dbo].[engine_mode] ADD CONSTRAINT [DF_engine_mode_engine_name] DEFAULT ('') FOR [engine_name]
GO
ALTER TABLE [dbo].[engine_mode] ADD CONSTRAINT [DF_engine_mode_engine_type] DEFAULT ('') FOR [engine_type]
GO
ALTER TABLE [dbo].[engine_mode] WITH CHECK ADD CONSTRAINT [FK_engine_mode_engine] FOREIGN KEY([engine_name]) REFERENCES [dbo].[engine] ([engine_name])
GO
ALTER TABLE [dbo].[engine_mode] CHECK CONSTRAINT [FK_engine_mode_engine] 
GO
USE [test]
GO
INSERT INTO [dbo].[engine] ([engine_name])
VALUES
('abc'), ('def')
GO
USE [test]
GO
INSERT INTO [dbo].[engine_mode] ([engine_name], [engine_type])
VALUES
('abc', 'R'), ('abc', '-9'), ('abc', 'S'), ('abc', '-9'), ('abc', 'T'), ('def', '-9'), ('def', '-9'), ('def', '-9')
GO

I have tried creating a view for this query but I wasn't able to figure it out. Any help is much appreciated.

答案1

得分: 2

你可以使用 group by

select engine_name,
    max(case when engine_type = '-9' then 0 else 1 end) as approved_engine_types
from EngineMode
group by engine_name

如果你想要从参考表格中获取相应的行,我们可以使用 joinapply

select e.*, em.*
from engine e
cross apply (
    select max(case when engine_type = '-9' then 0 else 1 end) as approved_engine_types
    from EngineMode em 
    where em.engine_name = e.engine_name
) em
英文:

You can use group by:

select engine_name,
max(case when engine_type = '-9' then 0 else 1 end) as approved_engine_types
from EngineMode
group by engine_name

If you wanted the corresponding row from the reference table, we can join or apply:

select e.*, em.*
from engine e
cross apply (
select max(case when engine_type = '-9' then 0 else 1 end) as approved_engine_types
from EngineMode em 
where em.engine_name = e.engine_name
) em

答案2

得分: 1

您可以使用分组和子查询
select  
			 id
			,engine_name
			,max(approved_engine_types) as approved_engine_types
from (

			select 
						 a.id  
						,a.engine_name  ,
			            case 
						    when [engine_type]='-9' then 0 else 1 
					    end  approved_engine_types

			from  [engine]  a
			inner join [engine_mode] b on a.engine_name=b.engine_name
)d
group by id,engine_name

演示

英文:

You can use group by and subquery

select  
id
,engine_name
,max(approved_engine_types) as approved_engine_types
from (
select 
a.id  
,a.engine_name  ,
case 
when [engine_type]='-9' then 0 else 1 
end  approved_engine_types
from  [engine]  a
inner join [engine_mode] b on a.engine_name=b.engine_name
)d
group by id,engine_name

Demo

huangapple
  • 本文由 发表于 2023年6月22日 02:17:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76526104.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定