如何查看每天出现多次的所有记录?
我有这张桌子:
ID Name Date
1 John 27.03.2010 18:17:00
2 Mike 27.03.2010 16:38:00
3 Sonny 28.03.2010 20:23:00
4 Anna 29.03.2010 13:51:00
5 Maria 29.03.2010 21:59:00
6 Penny 29.03.2010 17:25:00
7 Alba 30.03.2010 09:36:00
8 Huston 31.03.2010 10:19:00
我想得到:
1 John 27.03.2010 18:17:00
2 Mike 27.03.2010 16:38:00
4 Anna 29.03.2010 13:51:00
5 Maria 29.03.2010 21:59:00
6 Penny 29.03.2010 17:25:00
答案 0 :(得分:7)
假设您正在使用MySQL,应工作。
SELECT *
FROM `thetable`
GROUP BY DATE(`Date`)
HAVING COUNT(*) > 1
我没有对此进行过测试,这只是我能想到的第一件事。
日期函数只占用DateTime的日期部分(我假设你正在使用它,因为也显示了一个时间组件。Reference。我还在反引号中包含Date字段名,因为Date是保留的MySQL中的单词(并且为了一致性而对表名执行相同的操作)。
请记住,不同的RDBMS可能会有不同的功能来实现这一目标。
答案 1 :(得分:2)
我正在调用表Mytable,并将Date的名称更改为somedate以不使用关键字:
--create table mytable(ID int,Name varchar(32), somedate datetime)
select *
from mytable
where id in (
select id
from mytable
group by convert(varchar(10), somedate, 101), id
having count(1) > 1
)
答案 2 :(得分:1)
您的意思是“我如何选择包含多条记录的天数的所有记录?”
select *
from your_table
where trunc(date) in ( select trunc(date)
from your_table
group by trunc(date)
having count(*) > 1)
/
修改强>
哦,你在SQL Server上。我使用了ORACLE的TRUNC()
函数,该函数采用日期时间并删除时间元素。显然,SQL Server没有确切的等价物,但有some workarounds。
答案 3 :(得分:1)
更新:使用SQL Server:
CREATE TABLE t1 (id int IDENTITY PRIMARY KEY, name varchar(20), date datetime);
INSERT INTO t1 VALUES('John', '2010-03-27 18:17:00');
INSERT INTO t1 VALUES('Mike', '2010-03-27 16:38:00');
INSERT INTO t1 VALUES('Sonny', '2010-03-28 20:23:00');
INSERT INTO t1 VALUES('Anna', '2010-03-29 13:51:00');
INSERT INTO t1 VALUES('Maria', '2010-03-29 21:59:00');
INSERT INTO t1 VALUES('Penny', '2010-03-29 17:25:00');
INSERT INTO t1 VALUES('Alba', '2010-03-30 09:36:00');
INSERT INTO t1 VALUES('Huston', '2010-03-31 10:19:00');
SELECT t1.id, t1.name, sub_t.date
FROM t1
JOIN (SELECT DATEADD(dd, DATEDIFF(dd,0, date), 0) as date
FROM t1
GROUP BY DATEADD(dd, DATEDIFF(dd,0, date), 0)
HAVING COUNT(id) > 1) sub_t ON
(sub_t.date = DATEADD(dd, DATEDIFF(dd,0, t1.date), 0));
返回:
+----+-------+---------------------+
| id | name | date |
+----+-------+---------------------+
| 1 | John | 2010-03-27 00:00:00 |
| 2 | Mike | 2010-03-27 00:00:00 |
| 4 | Anna | 2010-03-29 00:00:00 |
| 5 | Maria | 2010-03-29 00:00:00 |
| 6 | Penny | 2010-03-29 00:00:00 |
+----+-------+---------------------+
以前的答案假定MySQL:
加入子查询将是一个选项:
CREATE TABLE t1 (id int AUTO_INCREMENT PRIMARY KEY,
name varchar(20),
date datetime);
INSERT INTO t1 VALUES(NULL, 'John', '2010-03-27 18:17:00');
INSERT INTO t1 VALUES(NULL, 'Mike', '2010-03-27 16:38:00');
INSERT INTO t1 VALUES(NULL, 'Sonny', '2010-03-28 20:23:00');
INSERT INTO t1 VALUES(NULL, 'Anna', '2010-03-29 13:51:00');
INSERT INTO t1 VALUES(NULL, 'Maria', '2010-03-29 21:59:00');
INSERT INTO t1 VALUES(NULL, 'Penny', '2010-03-29 17:25:00');
INSERT INTO t1 VALUES(NULL, 'Alba', '2010-03-30 09:36:00');
INSERT INTO t1 VALUES(NULL, 'Huston', '2010-03-31 10:19:00');
SELECT t1.id, t1.name, sub_t.date
FROM t1
JOIN (SELECT DATE(date) as date
FROM t1
GROUP BY DATE(date)
HAVING COUNT(id) > 1) sub_t ON (sub_t.date = DATE(t1.date));
返回:
+----+-------+------------+
| id | name | date |
+----+-------+------------+
| 1 | John | 2010-03-27 |
| 2 | Mike | 2010-03-27 |
| 4 | Anna | 2010-03-29 |
| 5 | Maria | 2010-03-29 |
| 6 | Penny | 2010-03-29 |
+----+-------+------------+
5 rows in set (0.02 sec)
答案 4 :(得分:0)
Select * From YourTable Where ADate in (Select ADate From YourTable Group By ADate Having Count(Distinct Id) > 1 )
答案 5 :(得分:0)
您可以按照以下方式执行此操作:
SELECT *
FROM theTable t1
WHERE t1.created_at IN
(SELECT t2.created_at
FROM theTable t2
GROUP BY created_at
HAVING COUNT(*) > 1)
答案 6 :(得分:0)
您可以在日期变量的日期部分进行连接,这将排除该日期只有一行的任何位置(因为它没有任何内容可以加入)。
此示例位于t-sql:
select distinct l.*
from @table l
join
@table r
on convert(varchar, l.[date], 102) = convert(varchar, r.[date],102)
and l.id != r.id
答案 7 :(得分:0)
这是最终版本:
SELECT *
FROM table
WHERE convert(varchar, table.DataInput, 102) IN
(SELECT convert(varchar, table.DataInput, 102)
FROM table
GROUP BY convert(varchar, table.DataInput, 102)
HAVING COUNT(*) > 2)
谢谢大家的投入! :)