问题
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