SQL通过将列舍入到最接近的值

时间:2017-12-05 04:44:26

标签: mysql sql sql-server group-by distinct

以下是表:

ID  C1  C2  C3  Date    C5  C6  Name
1   ### ### ### 9.53    ### ### ron
1   ### ### ### 9.57    ### ### ron
1   ### ### ### 9.87    ### ### ron
1   ### ### ### 10.2    ### ### jess
1   ### ### ### 10.2    ### ### jess
1   ### ### ### 11.3    ### ### mike
1   ### ### ### 11.3    ### ### mike
2   ### ### ### 16.58   ### ### ron
2   ### ### ### 16.33   ### ### ron
2   ### ### ### 16.45   ### ### ron
2   ### ### ### 22.43   ### ### jess
2   ### ### ### 22.88   ### ### jess
2   ### ### ### 19.2    ### ### mike
2   ### ### ### 19.51   ### ### mike

以下是我所期待的:

根据ID和名称获取不同的行,按日期分组到最接近的整数。

ID  C1  C2  C3  Date    C5  C6  Name
1   ### ### ### 9      ###  ### ron
1   ### ### ### 10     ###  ### jess
1   ### ### ### 11     ###  ### mike
2   ### ### ### 16     ###  ### ron
2   ### ### ### 22     ###  ### jess
2   ### ### ### 19     ###  ### mike

有人可以帮我吗?

6 个答案:

答案 0 :(得分:3)

这是有用的

    create table #tmp (Date float, Name varchar(10))

    insert into #tmp values(9.53,'ron')
    insert into #tmp values(9.57,'ron')
    insert into #tmp values(9.87,'ron')

    insert into #tmp values(10.2,'jess')
    insert into #tmp values(10.2,'jess')

    insert into #tmp values(11.3,'mike')
    insert into #tmp values(11.3,'mike')

    insert into #tmp values(16.58,'ron')
    insert into #tmp values(16.33,'ron')
    insert into #tmp values(16.45,'ron')

    insert into #tmp values(22.43,'jess')
    insert into #tmp values(22.88,'jess')

    insert into #tmp values(19.2,'mike')
    insert into #tmp values(19.51,'mike')



    SELECT DISTINCT floor(Date) AS Date,Name from #tmp

    drop table #tmp

答案 1 :(得分:3)

试试这个:

SELECT DISTINCT ID, Rounded_Date, Name
FROM
(SELECT ID, FLOOR(Date) Rounded_Date, Name FROM Your_Table);

答案 2 :(得分:2)

CREATE TABLE #Table1
    ([ID] int, [C1] varchar(3), [C2] varchar(3), [C3] varchar(3), [Date] numeric(10,4), [C5] varchar(3), [C6] varchar(3), [Name] varchar(4))
;

INSERT INTO #Table1
    ([ID], [C1], [C2], [C3], [Date], [C5], [C6], [Name])
VALUES
    (1, '###', '###', '###', 9.53, '###', '###', 'ron'),
    (1, '###', '###', '###', 9.57, '###', '###', 'ron'),
    (1, '###', '###', '###', 9.87, '###', '###', 'ron'),
    (1, '###', '###', '###', 10.2, '###', '###', 'jess'),
    (1, '###', '###', '###', 10.2, '###', '###', 'jess'),
    (1, '###', '###', '###', 11.3, '###', '###', 'mike'),
    (1, '###', '###', '###', 11.3, '###', '###', 'mike'),
    (2, '###', '###', '###', 16.58, '###', '###', 'ron'),
    (2, '###', '###', '###', 16.33, '###', '###', 'ron'),
    (2, '###', '###', '###', 16.45, '###', '###', 'ron'),
    (2, '###', '###', '###', 22.43, '###', '###', 'jess'),
    (2, '###', '###', '###', 22.88, '###', '###', 'jess'),
    (2, '###', '###', '###', 19.2, '###', '###', 'mike'),
    (2, '###', '###', '###', 19.51, '###', '###', 'mike')
;
select  distinct [ID], [C1], [C2], [C3], cast(date as int), [C5], [C6], [Name]  from #Table1

答案 3 :(得分:1)

看看你的要求,我认为这可能会给你预期的结果

temp_id

检查Demo

答案 4 :(得分:0)

试试这个:

flask.send_file()

答案 5 :(得分:0)

您可以使用此代码

select ID, C1, C2, C3, floor(Date) as RDate, C5, C6, Name
from your_table_name
group by ID, Name, RDate