如何将三个sql选择组合成一个查询

2024-01-02

我有下面的三个查询,我想将它们合并为一个查询,这样我就可以得到三列,其中包含按县划分的结果。我尝试对所有表进行内部联接,但得到的数据很差。如何组合这三个查询并按县分组?

     select [Total DLL Children] = SUM(cd.NumberOfLanguageSpeakers)
 from ClassroomDLL as cd
 inner join Classrooms as c on cd.Classroom_Id = c.Id
 inner join Sites as s on c.Site_Id = s.Id
 inner join Profiles as p on s.Profile_Id = p.Id
 inner join Counties as co on p.County_Id = co.Id
 group by co.Description

 select [Total Children] = (SUM(demo.NumberOfPreschoolers) + SUM(demo.NumberOfToddlers) + SUM(demo.NumberOfInfants))
 from ClassroomDemographics as demo
 inner join Classrooms as c on demo.Classroom_Id = c.Id
 inner join Sites as s on c.Site_Id = s.Id
 inner join Profiles as p on s.Profile_Id = p.Id
 inner join Counties as co on p.County_Id = co.Id
 group By co.Description

 select co.Description from Counties as co
 group by co.Description

请尝试这个。基本上,每个子查询,您也需要返回 County.Description,然后您可以将它们连接在一起。


 SELECT A.Description, B.[Total DLL Children], C.[Total Children]
 FROM (
     select co.Description from Counties as co
     group by co.Description
     ) A
 LEFT JOIN 
     (
         select co.Description, [Total DLL Children] = SUM(cd.NumberOfLanguageSpeakers)
         from ClassroomDLL as cd
         inner join Classrooms as c on cd.Classroom_Id = c.Id
         inner join Sites as s on c.Site_Id = s.Id
         inner join Profiles as p on s.Profile_Id = p.Id
         inner join Counties as co on p.County_Id = co.Id
         group by co.Description
      ) B
      ON A.DESCRIPTION = B.DESCRIPTION 
LEFT JOIN 
      (
         select co.Description, [Total Children] = (SUM(demo.NumberOfPreschoolers) + SUM(demo.NumberOfToddlers) + SUM(demo.NumberOfInfants))
         from ClassroomDemographics as demo
         inner join Classrooms as c on demo.Classroom_Id = c.Id
         inner join Sites as s on c.Site_Id = s.Id
         inner join Profiles as p on s.Profile_Id = p.Id
         inner join Counties as co on p.County_Id = co.Id
         group By co.Description
      ) C
      ON A.DESCRIPTION = C.DESCRIPTION 
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将三个sql选择组合成一个查询 的相关文章

随机推荐

  • 未定义的方法“save”使测试在测试结束之前失败

    我正在关注 ruby railstutorial org 我遇到了一些麻烦 但我解决了它们 然而 现在我在谷歌上搜索了相当长的一段时间 检查了代码 我什至知道为什么测试失败 但不知道如何让它通过 所以 问题来了 我有一个用户模型 class
  • 无法将密钥发送到定向 Paypal 登录页面

    我正在为 Paypal Checkout 编写一个自动填充机器人 paypal 登录页面是从另一个网站重定向的 因此与官方 paypal 登录页面略有不同 尽管 HTML 类似 我尝试过不同的方法 比如switch to frame scr
  • 运行 Spring MVC 测试时出现 NoSuchMethod 错误

    我正在尝试按照 Spring in Action 第四版 第 5 章中的示例来创建我自己的项目 仍然是企业级的新手 我正在使用 Windows 7 PC Java 7 Spring 4 和 Maven 当我运行 ClinicalNoteCo
  • Hbase快速统计行数

    现在我实现了行计数ResultScanner像这样 for Result rs scanner next rs null rs scanner next number 如果数据达到百万次计算量很大 我想实时计算我不想使用Mapreduce
  • Spring框架4.3.0 - 我什么时候需要@Autowired?

    我刚刚开始学习 Spring 框架 我使用版本 4 3 0 我认为我们需要 Autowired 来告诉框架何时一个类需要注入 不过 我今天尝试这样做 Component public class CDPlayer implements Me
  • 如何在 C++ 中创建动态数组的数组

    我正在尝试学习 C 并尝试为简单的哈希表编写代码 如下结构 array 0 0 array 0 1 array 0 2 key 1 value 1 value 2 array 1 0 array 1 1 key 2 value 3 arra
  • SQLAlchemy 关系加载器在表上留下锁?

    我有非常简单的代码导致我的 MySQL 数据库挂起 import sqlalchemy as sa from sqlalchemy import orm creating the engine the base etc import uti
  • 如何在 Jetty 中部署 Servlet?

    我创建了一个简单的 Servlet 我想将其部署在 Jetty 7 2 中 Jetty 正在运行并且能够为 JSP 页面提供服务http localhost 8080 jonas test jsp 我用 Jetty 启动java jar s
  • React 18 TypeScript 儿童 FC

    我升级到 React 18 并且编译得很好 如今 似乎每个使用子组件的组件都会抛出错误 Property children does not exist on type IPageProps 在儿童道具自动包含在之前FC界面 现在看来我必须
  • 将 IN 子句与 PIG FILTER 结合使用

    PIG 支持 IN 子句吗 filtered FILTER bba BY reason not in a b c d 或者我应该把它分成多个 OR Thanks 您可以使用 Apache DataFu 中的以下 udf 代替 这将帮助您避免
  • 如何在 JPA 2.0 项目中加载 Hibernate 'xxx.hbm.cfg' 文件?

    我刚刚启动了一个 Spring Roo 应用程序 使用 Hibernate 作为 JPA2 0 提供程序 我使用罐 子的方式如下 hibernate core 3 6 4 Final jar hibernate commons annota
  • 使用 C# 中的架构进行 XML 解析

    我正在编写一些在运行时加载 xml 文件的代码 目前 我们使用 XmlDocument 类型来读取 xml 文件 并在 SelectSingleNode 语句周围包装一个 try catch 这是在节点为 null 或在我们解析用户时不存在
  • iOS 从后台重新检查加载位置

    我正在构建一个应用程序 它根据您当前的位置显示结果数据 目前 我正在使用viewDidLoad的方法UIViewController开始CLLocationManager并获取当前位置 一旦我获得了与我想要的准确性相匹配的位置 我就会向我的
  • 从发布的 JSON 获取经典 ASP 变量

    我正在尝试通过 AJAX 将 JSON 发布到经典 ASP 页面 该页面检索值 检查数据库并将 JSON 返回到原始页面 我可以通过 AJAX 发布 JSON 我可以从 ASP 返回 JSON 我无法将发布的 JSON 检索到 ASP 变量
  • 在 Golang 中导入 C 错误:cc1.exe:错误:给出的文件名太多

    我正在尝试导入并使用 C 包 但我不断收到此错误 runtime cgo cc1 exe error too many filenames given Type cc1 exe help for usage cc1 exe fatal er
  • 将域指向我的远程 Node JS 应用程序?

    我正在尝试弄清楚如何在我的 Ubuntu 10 04 LTS 服务器上准确部署 Node JS 我读过许多不同的博客和文章 它们解释了多种不同的方式 大多数似乎已经过时了 或者看起来并没有真正发挥作用 看来最简单的解决办法就是使用Forev
  • 部分类文件的命名约定

    我正在生成大部分 ASP NET MVC 脚手架代码 所有生成的文件都是使用标准命名约定的部分类 例如 我的员工控制器文件名为 EmployeeController cs 如果我希望使用自定义的非生成逻辑扩展 EmployeeControl
  • Visual Studio 2019:起始页而不是起始窗口

    Visual Studio引入了阻塞Start Window而不是非阻塞Start Page 考虑到Visual Studio的可扩展性如何 有没有办法让VS2019显示非阻塞Start Page The new Start Window非
  • switch 语句:“期望一个常量值”

    目前我正在与 神奇的字符串 问题作斗争 public class MyDataField class definition exuecuted method public void SwitchMultipleDataFields var
  • 如何将三个sql选择组合成一个查询

    我有下面的三个查询 我想将它们合并为一个查询 这样我就可以得到三列 其中包含按县划分的结果 我尝试对所有表进行内部联接 但得到的数据很差 如何组合这三个查询并按县分组 select Total DLL Children SUM cd Num