Sql Query with inner select [英] Sql Query with inner select

查看:97
本文介绍了Sql Query with inner select的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个sql查询:

SELECT DISTINCT
  [Card NO], 
  [User Name],  
  (
    SELECT
      MIN(DateTime) AS [Enter Time], 
      MAX(DateTime) AS [Exit Time], 
      MAX(DateTime) - MIN(DateTime) AS [Inside Hours] 
    FROM
      ExcelData
  ) 
FROM
  ExcelData
GROUP BY
  [Card NO], [User Name], DateTime

表模式: CardNO | UserName | DateTime

我试图执行它,但没有成功。我说这是一个无效的查询。
任何人都可以在这个查询中发现错误?

I tried to execute it but with no success. I says that it is an invalid query. Can anyone find what is wrong in this query?

推荐答案

,并且不选择列DateTime。您可以通过DateTime选择聚合,这意味着您不应该对其进行分组。 @irene得到了正确的答案。

You are grouping by DateTime, which is a reserved keyword, and you don't select a column DateTime. You do select aggregates over DateTime, which means you shouldn't group on it. @irene got the right answer.

这篇关于Sql Query with inner select的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆