AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 207488
Accepted
Martin Surasky
Martin Surasky
Asked: 2018-05-23 06:00:30 +0800 CST2018-05-23 06:00:30 +0800 CST 2018-05-23 06:00:30 +0800 CST

SQL 查询问题(列中没有某些值的项目)

  • 772

这是我需要用一些 T-SQL 魔法解决的难题,我尝试过但失败了。

这是其中之一,在我想到实际解决方案之前,我认为我可以使用一些 T-SQL 在 3 分钟内解决,但是当“橡胶遇到道路”时,我只是无法在脑海中描绘出正确的运算符实现这个逻辑。

我想知道 Stack Exchange 社区中是否有人可以找到一个优雅的解决方案。我想结合使用临时表或游标的 3 或 4 个查询,我可以找到解决方案,但它不会基于 SET ...

我有一张看起来像这样的桌子(或多或少......)

CREATE TABLE Computers (
   ID               int IDENTITY(1,1),
   ComputerName     varchar(255),
   OS               varchar(255),
   AppName          varchar(255),
   AppVersion       varchar(10)
)

因此,每一行都有一个计算机名称、一个操作系统值(可以是 Windows 7 或 Windows 10),然后是应用程序的描述(例如 AppName = "Notepad++", AppVersion = "7.5.6")

ComputerName   | OS           | AppName   | AppVersion
Computer 1     | Windows 7    | App 1     | 1.0
Computer 1     | Windows 7    | App 2     | 1.0
Computer 1     | Windows 7    | App 3     | 1.0
Computer 2     | Windows 10   | App 4     | 1.0
Computer 2     | Windows 10   | App 5     | 1.0
Computer 3     | Windows 10   | App 4     | 1.0
Computer 4     | Windows 7    | App 4     | 1.0
Computer 4     | Windows 7    | App 5     | 1.0
Computer 5     | Windows 7    | App 1     | 1.0
Computer 5     | Windows 7    | App 4     | 1.0
Computer 5     | Windows 7    | App 5     | 1.0

它的工作方式是这样的:如果一台计算机安装了 10 个应用程序,那么此表中将有 10 行(每个应用程序一行)。计算机名和操作系统将重复(请不要开始讨论第二范式......这完全是关于解决问题的查询的问题)。

所以,我需要解决的问题是:我需要找到操作系统为 Windows 7(即 EAAAASY)的计算机列表,但是......

只有在该表中针对该 PC 报告的所有应用程序都报告为在其他 Windows 10 计算机上使用时,这些计算机才会出现在我的列表中。

如果 Windows 7 计算机具有未在 Windows 10 计算机中列出的应用程序,则它们不应显示在此列表中。计算机的每个应用程序都必须遵守此规则,计算机才能符合我的列表。

我的想法是要知道我在 SELECT 中获得的计算机具有我知道适用于 Windows 10 的所有应用程序,因此迁移它们应该更安全。

样本:

对于那些在家尝试此操作的人,这里有一个 CREATE TABLE 和一些 INSERT,您对我的数据是什么样子有正确的想法

-- Create the table
 CREATE TABLE Computers (
    ComputerName    varchar(255),
    OS              varchar(255),
    AppName         varchar(255),
    AppVersion      varchar(10)
 )

 -- Insert some values...
 INSERT INTO Computers
 VALUES 
 ('Computer 1', 'Windows 7', 'App 1', '1.0'),
 ('Computer 1', 'Windows 7', 'App 2', '1.0'),
 ('Computer 1', 'Windows 7', 'App 3', '1.0'),
 ('Computer 2', 'Windows 10', 'App 4', '1.0'),
 ('Computer 2', 'Windows 10', 'App 5', '1.0'),
 ('Computer 3', 'Windows 10', 'App 4', '1.0'),
 ('Computer 4', 'Windows 7', 'App 4', '1.0'),
 ('Computer 4', 'Windows 7', 'App 5', '1.0'),
 ('Computer 5', 'Windows 7', 'App 1', '1.0'),
 ('Computer 5', 'Windows 7', 'App 4', '1.0'),
 ('Computer 5', 'Windows 7', 'App 5', '1.0')
  • 在这个例子中,唯一符合我的查询条件的计算机是计算机 4,这是唯一一台 Windows 7 计算机,其中所有与之关联的应用程序(应用程序 4 和 5)也至少存在于 Windows 10 PC 的一行中。

  • 计算机 5 不符合条件,因为与之关联的其中一个应用程序(“应用程序 1”)仅存在于另一台 Windows 7 计算机(计算机 1)上的此集合中,因此我不能说此应用程序已经在 Windows 10 PC 上运行.

  • 计算机 2 和 3 是 Windows 10。所以这应该足以丢弃它们....

  • 计算机 1 也不应该符合条件,他们的所有应用程序都没有在 Windows 10 PC 上运行。

希望有道理...

select t-sql
  • 2 2 个回答
  • 84 Views

2 个回答

  • Voted
  1. Best Answer
    jyao
    2018-05-23T07:35:21+08:002018-05-23T07:35:21+08:00

    我相信以下查询将为您提供正确的计算机名称

     select ComputerName from dbo.computers
     where os= 'windows 7'
     except
     select ComputerName from dbo.computers
     where os= 'windows 7'
     and appname not in (select appname from dbo.computers where os='windows 10')
    

    结果是:

    在此处输入图像描述

    • 2
  2. paparazzo
    2018-05-23T10:10:32+08:002018-05-23T10:10:32+08:00

    另一种方法

    DECLARE @t TABLE (ComputerName varchar(255), OS varchar(255), AppName varchar(255), AppVersion varchar(10))
    
     INSERT INTO @t VALUES 
     ('Computer 1', 'Windows 7',  'App 1', '1.0'),
     ('Computer 1', 'Windows 7',  'App 2', '1.0'),
     ('Computer 1', 'Windows 7',  'App 3', '1.0'),
     ('Computer 2', 'Windows 10', 'App 4', '1.0'),
     ('Computer 2', 'Windows 10', 'App 5', '1.0'),
     ('Computer 3', 'Windows 10', 'App 4', '1.0'),
     ('Computer 4', 'Windows 7',  'App 4', '1.0'),
     ('Computer 4', 'Windows 7',  'App 5', '1.0'),
     ('Computer 5', 'Windows 7',  'App 1', '1.0'),
     ('Computer 5', 'Windows 7',  'App 4', '1.0'),
     ('Computer 5', 'Windows 7',  'App 5', '1.0') 
    
     declare @a table (AppName varchar(255) primary key);
     insert into @a 
     select distinct AppName
       from @t 
      where OS = 'Windows 10' ;
     select * from @a;
    
     select t7.ComputerName
       from @t t7 
       left join @a a 
         on t7.AppName = a.AppName 
      where t7.OS = 'Windows 7'
      group by t7.ComputerName 
      having count(*) = count(a.AppName);
    
    • 0

相关问题

  • 如何使用 TSQL 更改 SQL 服务器配置管理器设置?

  • 如何从结果集中获取列名和类型的列表?

  • MS SQL:使用计算值计算其他值

  • 如何判断 SQL Server 数据库是否仍在使用?

  • 实施 PIVOT 查询

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve