Sql Query with inner select [duplicate]

自古美人都是妖i 提交于 2019-12-27 02:37:16

问题


I have this sql query:

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

Table Schema: 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?


回答1:


This would not resolve your possible Problems with reserved keywords and column names, but should be a valid query. You were using a select query as column Name.

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



回答2:


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.



来源:https://stackoverflow.com/questions/25929155/sql-query-with-inner-select

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!