首页 \ 问答 \ SQL Server 2005 - 查询帮助(SQL Server 2005 - Query Help)

SQL Server 2005 - 查询帮助(SQL Server 2005 - Query Help)

我试图使用以下示例表编写查询:

公司

  • CompanyID
  • 名称

CompanyRelation

  • ParentCompanyID
  • ChildCompanyID

CompanySpecialty

  • CompanyID
  • 专业

使用CompanyRelation表是因为每个公司下面都有许多子公司,每个子公司可以有很多公司。

理想情况下,我希望能找到的是某个县和/或某个专业的所有公司。 现在,通常情况下,我会加入CompanyCompanySpecialty并过滤这两个字段,但这里是棘手的:子公司没有分配给他们的县,所以如果我过滤县,所有的子公司将是排除。

如果我将公司过滤到一个县,我想显示与该公司关联的所有子公司,无论子公司没有地址。

为了解决这个问题,我必须引入CompanyRelation表。

示例数据:

CompanyID---Name---County
1-----------ABC----King
2-----------BCD----Pierce
3-----------DEF----NULL
4-----------EFG----NULL

ParentCompanyID---ChildCompanyID
1-----------------1
1-----------------3
2-----------------2
2-----------------4

CompanyID---Specialty
1-----------Vehicles
2-----------Vehicles
3-----------Vehicles
4-----------Vehicles

使用这些数据,说我想找到金县所有处理车辆的公司。 在我的结果中,我希望看到公司1和公司3。

如何编写查询来完成此任务?


I am trying to write a query using the following example tables:

Company

  • CompanyID
  • Name
  • County

CompanyRelation

  • ParentCompanyID
  • ChildCompanyID

CompanySpecialty

  • CompanyID
  • Specialty

The CompanyRelation table is used because each company can have many subcompanies under it and each subcompany can have many companies over it.

Ideally, what I want to be able to find is all companies by a certain county and/or by a certain specialty. Now, normally, I would just join Company to CompanySpecialty and filter on those two fields, but here is where it gets tricky: the subcompanies don't have counties assigned to them, so if I filter on the county, all the subcompanies will be excluded.

If I filter the companies down to a county, I would like to display all subcompanies that are associated to that company, regardless of the fact that the subcompany does not have an address.

To get around this, I have to bring in the CompanyRelation table.

Example data:

CompanyID---Name---County
1-----------ABC----King
2-----------BCD----Pierce
3-----------DEF----NULL
4-----------EFG----NULL

ParentCompanyID---ChildCompanyID
1-----------------1
1-----------------3
2-----------------2
2-----------------4

CompanyID---Specialty
1-----------Vehicles
2-----------Vehicles
3-----------Vehicles
4-----------Vehicles

Using this data, say I want to find all companies in King county that deal with vehicles. In my results, I would expect to see Company 1 and Company 3.

How can I write a query to accomplish this?


原文:https://stackoverflow.com/questions/6710199
更新时间:2023-12-04 16:12

最满意答案

联合加入的两个查询将给出所需的结果:

( SELECT team AS team,SUM(goal) AS goals 
  FROM table
  GROUP BY 1 
  LIMIT 4 )
UNION
( SELECT 'Other',SUM(other.goals) FROM 
  ( SELECT team AS team,SUM(goal) AS goals 
    FROM table 
    GROUP BY 1 
    LIMIT 5,999999 ) other
)

Two queries joined by a union will give the desired result:

( SELECT team AS team,SUM(goal) AS goals 
  FROM table
  GROUP BY 1 
  LIMIT 4 )
UNION
( SELECT 'Other',SUM(other.goals) FROM 
  ( SELECT team AS team,SUM(goal) AS goals 
    FROM table 
    GROUP BY 1 
    LIMIT 5,999999 ) other
)

相关问答

更多
  • 以上SELECT * FROM table LIMIT 15,15; 检索的为16-30行的数据 具体的limit的使用详解如下: SELECT * FROM table LIMIT [offset,] rows | rows OFFSET offset LIMIT 子句可以被用于强制 SELECT 语句返回指定的记录数。LIMIT 接受一个或两个数字参数。参数必须是一个整数常量。如果给定两个参数,第一个参数指定第一个返回记录行的偏移量,第二个参数指定返回记录行的最大数目。初始记录行的偏移量是 0(而不是 ...
  • 返加所有记录就不用limit了,limit正是用来限制返回数据条数的。 直接用 select * from table 就可以了
  • 楼主 limit用法很简单 帮你举个例子吧 select * from table limit m,n 其中m是指记录开始的index,从0开始,表示第一条记录 n是指从第m+1条开始,取n条。 select * from tablename limit 2,4 即取出第3条至第6条,4条记录 如果你只有一条记录 但你要查询10条记录 这样就会重复取数了 如果楼主满意 请采纳
  • limit是mysql的语法 select * from table limit m,n 其中m是指记录开始的index,从0开始,表示第一条记录 n是指从第m+1条开始,取n条。 select * from tablename limit 15,15 即取出第16条至第30条,15条记录
  • SELECT id FROM ips WHERE ip IN (SELECT DISTINCT ip FROM ips ORDER BY id LIMIT 3) 由于MySQL 5.5还没有支持ANY内部的LIMIT ,这里有一个简单的解决方法: SELECT id FROM ips WHERE ip IN (SELECT * FROM (SELECT DISTINCT ip FROM ips ORDER BY id LIMIT 3) alias) SELECT id FROM ips WHERE ip ...
  • 来自https://dev.mysql.com/doc/refman/5.0/en/select.html LIMIT子句可用于约束SELECT语句返回的行数。 LIMIT需要一个或两个数字参数,它们都必须是非负整数常量(使用预准备语句时除外)。 要从特定偏移量检索所有行直到结果集的末尾,可以使用一些大数字作为第二个参数。 此语句检索从第96行到最后一行的所有行: SELECT * FROM tbl LIMIT 95,18446744073709551615; 所以在你的情况下 $sql="SELECT ...
  • 这个怎么样(未经测试): SELECT * FROM table WHERE id = 0 AND NOT EXISTS (SELECT * FROM table WHERE active = 1) UNION SELECT * FROM table WHERE ACTIVE = 1; 这可能不是很有效,但它取决于记录的数量。 对于活动的索引可能有所帮助,以防万一。 它应该适合您的问题:如果有active = 1行,它们将从union的第二个查询返回,而第一个部分解析为空集(因为EXISTS子查询将失败) ...
  • 如果您可以处理大约 30%,您可以简单地做: where brand = 'TBrand' and rand() <= 0.3 如果你想要一个更好的近似值30%,那么你可以这样做: SELECT x.* FROM (SELECT rdw.merchant_id, hash_id, transaction_ts, (@rn := @rn + 1) as rn FROM table_1 rdw JOIN table_2 bl O ...
  • 我想如果你在MySQL客户端运行查询,限制为10,你会发现它实际上在结果集中返回了10行。 我怀疑@mcmodlist_tag_server中有多行,其server_id与@mcmodlist_servers中的行匹配。 当存在多个匹配行时,您将从@mcmodlist_servers获取“重复”行。 鉴于没有从@mcmodlist_tag_server表返回的列,并且这是一个OUTER连接,所以根本不清楚为什么这个表将被包含在查询中。 不,LEFT JOIN并不意味着你所说的意思。 问: 左外连接只有在匹配 ...
  • 联合加入的两个查询将给出所需的结果: ( SELECT team AS team,SUM(goal) AS goals FROM table GROUP BY 1 LIMIT 4 ) UNION ( SELECT 'Other',SUM(other.goals) FROM ( SELECT team AS team,SUM(goal) AS goals FROM table GROUP BY 1 LIMIT 5,999999 ) other ) Two ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)