如何将 SQL 查询与不同的表达式结合起来?

2024-03-18

我的三个查询已经达到了我的 SQL 知识的顶峰(Microsoft SQL 2005,如果这很重要),现在我需要将它们组合成一个查询,并将所有值放在一行上。

我的实际查询如下,但我认为如果我在这里提供一个简单的版本会更容易:

查询一:

-- Provides School District summary based on a CountyID
SELECT DistrictID, Count(Schools) as NumberofSchools
FROM Schools
WHERE (CountyID = 207)
GROUP BY DistrictID

查询一个示例输出:

DistrictID  |  NumberofSchools
345         |  26
567         |  17
211         |  9

查询二:

-- Summarizes Activity from our Contact Manager (GoldMine)
SELECT DistrictID, Count(Contacts) as NumberofContacts, MAX(Contact) as LastActivity
FROM ContactManager JOINED WITH CONTACT MANAGER TABLES
WHERE (CountyID = 207)
GROUP BY DistrictID

查询两个示例输出:

DistrictID  |  NumberofContacts  |  LastActivity
345         |  29                |  Nov 12, 2010
567         |  31                |  Dec 5, 2010
211         |  4                 |  Oct 9, 2010

疑问三:

-- Summarizes data from our Opt-In Email Newsletter
SELECT DistrictID, Count(EmailSubscribers) AS NumberofSubscribers, MAX(Date) AS LastSent
FROM SubscribeList JOINED WITH Schools Tables
WHERE (CountyID = 207)
GROUP BY DistrictID

查询三个示例输出:

DistrictID  |  NumberofSubscribers  |  LastSent
345         |  2                    |  Sep 4, 2010
567         |  3                    |  Oct 22, 2010
211         |  1                    |  NULL

我尝试使用父 SELECT 语句对它们进行巨大的 UNION(以下详细信息来自这个网页链接 http://www.sql-server-pro.com/sql-union.html并为每个数据集引入 SELECT NULL AS MissingColumnName),但它真的很难看 - 并且不会返回一行上的所有内容。

我正在寻找这样的结果:

DistrictID  |  NumberofSchools  |  NumberofContacts  |  LastActivity  |  NumberofSubscribers  |  LastSent
345         |  26               |  29                |  Nov 12, 2010  |  2                     |  Sep 4, 2010
567         |  17               |  31                |  Dec 5, 2010   |  3                     |  Oct 22, 2010 
211         |  9                |  4                 |  Oct 9, 2010   |  1                     |  NULL

我怎样才能做到这一点? (如果您好奇,我要加入的真正查询如下)

感谢你的帮助!,

拉塞尔·舒特

尽我所能清理这些 - 抱歉,它们显示得不太好。 (这些也可能存在问题 - 它们是我 SQL 知识的顶部,但到目前为止结果似乎是准确的。):-)

查询一:

SELECT
    institutionswithzipcodesadditional_1.DistrictID, institutionswithzipcodesadditional_1.InstitutionName, institutionswithzipcodesadditional_1.Latitude, institutionswithzipcodesadditional_1.Longitude,
    SUM(CASE WHEN institutionswithzipcodesadditional.LevelID IN (4, 5, 6, 7, 8, 14, 15, 16, 20) THEN institutionswithzipcodesadditional.Enrollment ELSE 0 END) AS OthersEnrollment,
    COUNT(CASE WHEN institutionswithzipcodesadditional.LevelID IN (4, 5, 6, 7, 8, 14, 15, 16, 20) THEN institutionswithzipcodesadditional.InstitutionID ELSE NULL END) AS OthersCount, 
    SUM(CASE WHEN institutionswithzipcodesadditional.LevelID IN (13) THEN institutionswithzipcodesadditional.Enrollment ELSE 0 END) AS K12SchoolsEnrollment, 
    COUNT(CASE WHEN institutionswithzipcodesadditional.LevelID IN (13) THEN institutionswithzipcodesadditional.InstitutionID ELSE NULL END) AS K12SchoolsCount, 
    SUM(CASE WHEN institutionswithzipcodesadditional.LevelID IN (12) THEN institutionswithzipcodesadditional.Enrollment ELSE 0 END) AS HighSchoolsEnrollment, 
    COUNT(CASE WHEN institutionswithzipcodesadditional.LevelID IN (12) THEN institutionswithzipcodesadditional.InstitutionID ELSE NULL END) AS HighSchoolsCount, 
    SUM(CASE WHEN institutionswithzipcodesadditional.LevelID IN (10, 11) THEN institutionswithzipcodesadditional.Enrollment ELSE 0 END) AS MiddleSchoolsEnrollment,
    COUNT(CASE WHEN institutionswithzipcodesadditional.LevelID IN (10, 11) THEN institutionswithzipcodesadditional.InstitutionID ELSE NULL END) AS MiddleSchoolsCount,
    SUM(CASE WHEN institutionswithzipcodesadditional.LevelID IN (9) THEN institutionswithzipcodesadditional.Enrollment ELSE 0 END) AS ElementariesEnrollment,
    COUNT(CASE WHEN institutionswithzipcodesadditional.LevelID IN (9) THEN institutionswithzipcodesadditional.InstitutionID ELSE NULL END) AS ElementariesCount,
    SUM(CASE WHEN institutionswithzipcodesadditional.LevelID IN (4, 5, 6, 7, 8, 14, 15, 16, 20, 13, 12, 10, 11, 9) THEN institutionswithzipcodesadditional.Enrollment ELSE 0 END) AS AllSchoolsEnrollment, 
    COUNT(CASE WHEN institutionswithzipcodesadditional.LevelID IN (4, 5, 6, 7, 8, 14, 15, 16, 20, 13, 12, 10, 11, 9) THEN institutionswithzipcodesadditional.InstitutionID ELSE NULL END) AS AllSchoolsCount
FROM zipcodes 
    INNER JOIN users_link_territory ON zipcodes.CountyID = users_link_territory.CountyID
    INNER JOIN institutionswithzipcodesadditional ON zipcodes.ZIP = institutionswithzipcodesadditional.ZIP
    RIGHT OUTER JOIN institutionswithzipcodesadditional AS institutionswithzipcodesadditional_1 ON institutionswithzipcodesadditional.DistrictID = institutionswithzipcodesadditional_1.InstitutionID
WHERE
    (institutionswithzipcodesadditional_1.CountyID = 207)
AND (institutionswithzipcodesadditional_1.LevelID IN (1, 2, 3, 6, 7, 8))
GROUP BY institutionswithzipcodesadditional_1.DistrictID, institutionswithzipcodesadditional_1.InstitutionName, institutionswithzipcodesadditional_1.Latitude, institutionswithzipcodesadditional_1.Longitude

查询二:

SELECT
institutionswithzipcodesadditional_1.InstitutionID AS DistrictID,
COUNT(GoldMine.dbo.CONTACT1.ACCOUNTNO) AS GM,
MAX(CASE WHEN GoldMine.dbo.CONTHIST.USERID NOT IN ('DEBRA', 'TRISH', 'RUSSELL', 'GREG') THEN GoldMine.dbo.CONTHIST.OnDate ELSE NULL END) AS LastActivity
FROM institutionswithzipcodesadditional
    LEFT OUTER JOIN contacts
    LEFT OUTER JOIN GoldMine.dbo.CONTACT1
    RIGHT OUTER JOIN GoldMine_Link_Russell 
    ON GoldMine.dbo.CONTACT1.KEY3 = GoldMine_Link_Russell.GoldMineKeyThree
    ON contacts.ContactID = GoldMine_Link_Russell.ContactID 
    ON institutionswithzipcodesadditional.InstitutionID = contacts.InstitutionID
    RIGHT OUTER JOIN institutionswithzipcodesadditional AS institutionswithzipcodesadditional_1
    ON institutionswithzipcodesadditional.DistrictID = institutionswithzipcodesadditional_1.InstitutionID
    LEFT OUTER JOIN GoldMine.dbo.CONTHIST ON GoldMine.dbo.CONTHIST.ACCOUNTNO = GoldMine.dbo.CONTACT1.ACCOUNTNO
WHERE (institutionswithzipcodesadditional_1.CountyID = 207) AND (institutionswithzipcodesadditional_1.LevelID IN (1, 2, 3, 6, 7, 8))
GROUP BY institutionswithzipcodesadditional_1.InstitutionID

疑问三:

SELECT
COUNT(NewsletterContacts.Email) AS EMailableContacts,
institutionswithzipcodesadditional_1.InstitutionID AS DistrictID,
MAX(newsletterregister.Sent) AS LastSent
FROM newsletterregister
    RIGHT OUTER JOIN contacts ON newsletterregister.ContactID = contacts.ContactID
    RIGHT OUTER JOIN institutionswithzipcodesadditional ON contacts.InstitutionID = institutionswithzipcodesadditional.InstitutionID
    LEFT OUTER JOIN EmailableContacts ON institutionswithzipcodesadditional.InstitutionID = EmailableContacts.InstitutionID
    RIGHT OUTER JOIN institutionswithzipcodesadditional AS institutionswithzipcodesadditional_1 ON 
    institutionswithzipcodesadditional.DistrictID = institutionswithzipcodesadditional_1.InstitutionID
WHERE
    (institutionswithzipcodesadditional_1.CountyID = 207)
    AND (institutionswithzipcodesadditional_1.LevelID IN (1, 2, 3, 6, 7, 8))
GROUP BY institutionswithzipcodesadditional_1.InstitutionID

是的,您可以使用联接来做到这一点:

SELECT
    T1.DistrictID,
    T1.NumberofSchools,
    T2.NumberofContacts,
    T2.LastActivity,
    T3.NumberofSubscribers,
    T3.LastSent
FROM (SELECT ...) T1
JOIN (SELECT ...) T2 ON T1.DistrictID = T2.DistrictID
JOIN (SELECT ...) T3 ON T1.DistrictID = T3.DistrictID

The (SELECT ...)是您的三个原始查询的占位符。如果三个查询可能返回不同的地区,即如果一个地区出现在一个查询的结果中,但在另一个查询的结果中缺失,您可能还需要考虑使用 OUTER JOIN。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将 SQL 查询与不同的表达式结合起来? 的相关文章

随机推荐

  • 除非作为参数传入,否则无法添加快速路线

    我正在尝试创建映射其自己的路线的节点模块 我在下面提供了一个简化的示例 删除了返回的对象中映射的所有其他函数以及任何代码以简化示例 我最初的尝试是这样做 文件 web core js function initRoutes app var
  • 具有不同 uv 坐标的 OpenGL ES 1 多重纹理

    我需要使用多重纹理渲染一个对象 但两个纹理对于同一对象具有不同的 uv 坐标 一张是法线贴图 另一张是光照贴图 请提供与此相关的任何有用材料 在 OpenGL ES 2 中 无论如何你都会使用着色器 因此 您可以完全自由地使用您喜欢的任何纹
  • 为 RDBMS(MySQL 数据库)创建 SPARQL 端点的最佳方法

    我正在 想做 一些链接开放数据集的实验 特别是政府推出的实验 我有一个 RDBMS 更具体地说是 MySQL 我设计它时考虑了语义网络的想法 即我将信息存储为对象 谓词和定义对象的类 反过来 所有对象通过主语 gt 谓词 gt 宾语形式的语
  • OpenId + 记住我/保持登录状态

    我有一个问题 关于如何 什么是使用 OpenId 并提供保持登录状态的最佳方法 例如 如果我查看 Stackoverflow 我已经使用 Google 登录 如果我关闭浏览器并返回 它仍然显示我已登录 However 我没有登录谷歌 而且我
  • Rails 引擎存在外键问题

    我正在开发一个 Rails 引擎 这是我的gem gemspec s required ruby version gt 2 0 0 s add dependency rails gt 4 2 0 s add dependency enume
  • angularjs - 在范围或 ng-model 上使用“字符串”名称

    在普通的 JavaScript 中 你可以像这样声明变量 var obj obj item text obj item text 这里给出的例子 http jsbin com petafu 1 edit http jsbin com pet
  • Google Play 排行榜 UI 颜色更改

    在我刚刚开发的游戏中 直到一周前 排行榜 UI 颜色还是深半透明的绿色 这看起来感觉很好 至少对眼睛不刺激 突然之间 它变成了明亮的半透明红色 并且从那时起就一直保持这种颜色 这种颜色与我游戏的任何阶段的任何屏幕都不匹配 这是我的排行榜屏幕
  • 如何在 Dart 中创建 HTML 链接?

    我想用 Dart 创建一个 HTML 链接 在 HTML 中我会写 You can click a href url 1 here a and a href url 2 there a 我不知道如何在 Dart 中做到这一点 我尝试过类似的
  • Python xlwt 创建错误的 Excel 书

    我正在尝试使用xlwt创建具有多个选项卡的输出文件 xlsx 格式 我的Python版本号是2 7 我使用Aptana Studio 3作为IDE 我用过xlwt包之前 具有相同的环境 执行相同的任务 效果很好 但这一次 一开始运行良好 然
  • 将 gcc libs .data 放在特定部分?

    我正在尝试为我们的嵌入式系统切换到 GNU GCC 编译器 但由于我们芯片的内存布局被分割 我在链接该项目时遇到了问题 RAM section 1 0x10000 0x12FFF RAM section 2 0x18000 0x1BFFF
  • Express js中通过id删除mongodb文档

    我正在为此抓狂 尝试通过 id 删除文档 router delete api menu delete id function req res var id req params id db get collection menu funct
  • 如何在自定义验证属性中获取/注入服务

    我们使用 NET Core 3 1 5 这是一个 Blazor 服务器应用程序 我们有一个 ValidationAttribute 并需要访问外部服务来验证对象 ValidationAttribute 有 IsValid 方法 protec
  • 有没有办法在 C# 中修改进程 DACL

    我有更改进程 DACL 的遗留 C 代码 并尝试使用 NET 3 5 中的托管代码类 我在网上找到了代码 其中有人创建了一个 SetAclOnServices 类 该类扩展了服务的 NativeObjectSecurity 类 我认为我可以
  • jQuery 数据表中的列排序

    我已经了解了 jQuery 数据表插件中的列排序以及控制它的各种方法 我有一个查询是否可以通过单击上箭头图标将按升序和下箭头图标进行排序的方式控制排序会按降序排序吗 有两种方法可以做到这一点 具体取决于datatables版本 编辑数据表版
  • 如何在Python中将一个对象的所有属性复制到另一个对象?

    在Python中 是否有一个库方法可以复制同一类的两个 已经存在的 实例之间的所有属性 我的意思是 类似 Apache Commons 的东西PropertyUtilsBean copyProperties Try destination
  • ActiveRecord 查询比直接 SQL 慢得多?

    我一直致力于优化项目的数据库调用 我注意到下面两个相同的调用之间的性能存在 显着 差异 connection ActiveRecord Base connection pgresult connection execute SELECT S
  • 当我“推”到 Bitbucket 时,可以从 VPS 中“拉”一个钩子

    我正在 Bitbucket 中管理我的 WordPress 模板 每次我push主题 我必须登录我的 VPS 服务器并且pull回购协议 我想自动完成 如果我自己运行 git daemon 我找到了一个解决方案 推送到服务器后执行自动拉取请
  • 基于现有模板生成Word文档的最佳方法是什么

    TL DR 我可以使用 NET 生成 Word 文档 如 XAML ItemTemplates 吗 我发现很难找到一个满足我所有要求的解决方案 所以我想我会把它扔到 stackoverflow 希望有人能指导我 非常感谢 简单来说 我需要根
  • 如何根据条目的长度过滤 pandas 数据框

    在 pandas 数据框中 我有一个字段 amp 应由长度为 495 的列表填充 是否有一种 pandas ic 方法可以快速过滤此长度 使得具有字段 amp 的所有行不等于 495被丢弃 I tried df len df amp 495
  • 如何将 SQL 查询与不同的表达式结合起来?

    我的三个查询已经达到了我的 SQL 知识的顶峰 Microsoft SQL 2005 如果这很重要 现在我需要将它们组合成一个查询 并将所有值放在一行上 我的实际查询如下 但我认为如果我在这里提供一个简单的版本会更容易 查询一 Provid