如何使用多个select语句对新表达式进行分组

时间:2017-09-15 11:48:03

标签: sql

SELECT date_uploaded,
       (SELECT CAST(tablename.date_uploaded AS date) AS Expr1) AS newdate
  FROM tablename

我的问题是,如何GROUP BY给出新的虚拟列(Expr1)?

2 个答案:

答案 0 :(得分:1)

在大多数数据库中,您需要在group by

中重复该条件
SELECT date_uploaded, CAST(tablename.date_uploaded AS date) AS newdate
FROM tablename
GROUP BY CAST(tablename.date_uploaded AS date);

对于简单的表达式,完全没有嵌套的SELECT。我不知道人们在哪里学会做这些事情,但我在过去看过类似的例子。

答案 1 :(得分:0)

最简单的方法:

SELECT * FROM
(

        SELECT date_uploaded,
        (SELECT CAST(tablename.date_uploaded AS date) AS Expr1) AS newdate
        FROM tablename

 )AS TB  

GROUP BY TB.newdate