Sql query with internal selection

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: CardNO | UserName | DateTime

I tried to execute it, but to no avail. I say this is an invalid request. Can anyone find something wrong in this query?

-3
source share
2 answers

This will not solve your potential problems with reserved keywords and column names, but should be a valid query. You used the select query as the 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]
0
source

You are grouping a DateTime, which is a reserved keyword, and you are not selecting a DateTime column. You choose aggregates compared to DateTime, which means you should not group them. @irene got the correct answer.

0

All Articles