MSSQL Select-删除重复项将不起作用

时间:2019-04-24 07:21:33

标签: sql-server select duplicates

嘿,“ GROUP BY”子句有些问题。它不会匹配“ day”列。

[数字] =>整数
[date] => char(14)

正常的日期格式如下:

20190424091650
20190424091550
20190424091450
20190424091350
20190424091250
20190424091150
20190424091050
20190424090950
20190424090850
20190424090750
20190424090650

有人知道为什么他们不会更改为这样的东西(特定日期的最高数字)吗?

31  20190424
34  20190423
20  20190422

查询:

SELECT
[number],
SUBSTRING([date], 0, 9) AS "day"

FROM
[LOG_VISITOR]

GROUP BY
[number],SUBSTRING([date], 0, 9)

ORDER BY  SUBSTRING([date], 0, 9) DESC,[number] DESC

输出:

[number] [date]
31  20190424
28  20190424
27  20190424
26  20190424
15  20190424
14  20190424
34  20190423
14  20190423
13  20190423
12  20190423
11  20190423
10  20190423
9   20190423
8   20190423
7   20190423
6   20190423
5   20190423
4   20190423
3   20190423
2   20190423
1   20190423
0   20190423
20  20190422
19  20190422

2 个答案:

答案 0 :(得分:0)

您需要这样的东西:

SELECT
Max([number]) as [number],
left([date],8) AS [day]

FROM
[LOG_VISITOR]

GROUP BY
left([date],8)

ORDER BY  left([date],8) DESC

如果正确,则您不需要[number] DESC来订购,因为left([date],8)是唯一的

但我不明白您是否需要Max([number])Count([number]) 您提到(highest number on specific day),但使用count作为列标题

答案 1 :(得分:0)

尝试一下。

    declare @table as Table
    (col nvarchar(14))
    insert into @table values (20190422091650)
    insert into @table values (20190422091550)
    insert into @table values (20190423091450)
    insert into @table values (20190423091350)
    insert into @table values (20190423091250)
    insert into @table values (20190423091150)
    insert into @table values (20190424091050)
    insert into @table values (20190424090950)
    insert into @table values (20190424090850)
    insert into @table values (20190424090750)
    insert into @table values (20190424090650)

    select count(col), cast(left(col,8) as datetime) as [date] from @table
    group by cast(left(col,8) as datetime)