Can we use group by and where condition with same fieldname(我们可以使用具有相同字段名的 group by 和 where 条件吗)
问题描述
我有一个要求,比如必须提取用户选择的日期范围内的所有记录,选择从 2011 年 1 月 15 日到 2011 年 8 月 20 日开始的所有员工,并按日期分组.
I have a requirement like have to pull all records in the date range the user selected, selecting all employees who started from 15-Jan-2011 to 20-Aug-2011 and group by date.
我应该如何为此编写 SQL 查询:
How should I write SQL query for this:
SELECT *
FROM employees
WHERE startdate >= '15-jan-2011'
AND startdate <= '20-aug-2011'
GROUP BY startdate
推荐答案
绝对可以.这将导致过滤日期范围内的记录,然后按有数据的每一天对其进行分组.
Absolutely. It will result in filtering the records on your date range and then grouping it by each day where there is data.
应该注意的是,您只能选择开始日期,然后选择您正在计算的任何聚合.否则,它应该可以正常工作.
It should be noted that you will only be able to select the startdate and then whatever aggregates you're calculating. Otherwise, it should work perfectly fine.
例如,此查询将为您提供每个开始日期的员工数:
For example, this query will give you a count of employees for each startdate:
SELECT startdate, count(*)
FROM employees
WHERE startdate >= '15-jan-2011'
AND startdate <= '20-aug-2011'
GROUP BY startdate
这篇关于我们可以使用具有相同字段名的 group by 和 where 条件吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:我们可以使用具有相同字段名的 group by 和 where
基础教程推荐
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- 无法解决整理冲突 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- 在 SQL 中连接多个表 2021-01-01
