【问题标题】:Can I inner join a previous SQL query then perform counts我可以内部加入以前的 SQL 查询然后执行计数吗
【发布时间】:2019-09-24 15:29:51
【问题描述】:

我从一个相当大的数据库创建了一个查询。 目前,员工执行的每一个程序都显示为 3 个相同的时间行,每行都通知程序发生的地点、使用的设备部件以及白天还是晚上。 我想将具有匹配名称和时间的行组合在一起,以创建包含所有其他字段的单行。 然后,我希望能够为员工创建一个日志,以显示每个程序的执行次数以及使用的站点和技术。

我认为内部联接可能是执行此操作的最佳方式,但如果您提供有关如何在子查询上进行设置的进一步帮助,将不胜感激。

当前查询:

SELECT procedure, employee, chart_time, form,  
FROM cust.records 
WHERE employeeID IN () AND procedurelabel LIKE 'rad1'

非常感谢您的帮助

【问题讨论】:

  • 完全不清楚您要做什么。我建议阅读此内容并再次尝试您的问题:spaghettidba.com/2015/04/24/…
  • 请添加带有示例输入和预期输出的 DDL、DML 脚本。

标签: sql-server join count


【解决方案1】:

您的问题需要稍微澄清一下......尤其是您的表格的 DDL 以及关于每列是什么的一些注释。尽管如此,假设这些行都在cust.records 表中,并且每组 3 行都有一个唯一的名称和时间组合,你可以做这样的事情......

SELECT -- first select fields common to all rows... may as well take these from the first table
       records1.procedure, records1.employee, records1.chart_time, 
       -- ... then select records from your joins
       records2.some_column,  
       records3.some_column
  FROM cust.records records1 
 INNER JOIN WHERE cust.records records2 on records1.chart_time = records2.chart_time
                                       and records1.procedure = record2.procedure
                                       and records1.employee = records2.employee
                                       -- Possible condition required here to not join this row to itself
                                       -- or to explicitly join to a specific type of row
 INNER JOIN WHERE cust.records records3 on records1.chart_time = records3.chart_time
                                       and records1.procedure = record3.procedure
                                       and records1.employee = records3.employee
                                       -- Possible condition required here to not join this row to itself
                                       -- or to explicitly join to a specific type of row

 WHERE employeeID IN () 
   AND procedurelabel LIKE 'rad1'
   -- Possible condition required here specify the row to select for records1.

可能还值得考虑重新设计表格,因为您所描述的内容听起来并不规范。

【讨论】:

    猜你喜欢
    • 2011-05-13
    • 2017-06-26
    • 1970-01-01
    • 2016-09-24
    • 2021-06-08
    • 1970-01-01
    • 2019-06-23
    • 2014-08-19
    • 1970-01-01
    相关资源
    最近更新 更多