从分组依据中获取最短日期时间

时间:2018-06-27 10:56:04

标签: sql sql-server tsql gaps-and-islands

我有以下查询

SELECT tag, time, value FROM picomp
WHERE tag = test.PV' AND time >= '6/19/2017'

结果:

test.PV,6/25/2017 12:10:59 AM,23.9164886
test.PV,6/25/2017 12:11:59 AM,23.8978481
test.PV,6/25/2017 12:12:59 AM,23.888525
test.PV,6/25/2017 12:14:00 AM,23.8698845
test.PV,6/25/2017 12:15:00 AM,23.8605652
test.PV,6/25/2017 12:16:00 AM,23.8512459
test.PV,6/25/2017 12:17:00 AM,23.8326035
test.PV,6/25/2017 12:18:00 AM,23.8139629
test.PV,6/25/2017 12:19:00 AM,23.7953224
test.PV,6/25/2017 12:20:00 AM,23.77668
test.PV,6/25/2017 12:21:00 AM,23.7673588
test.PV,6/25/2017 12:22:00 AM,23.7487202
test.PV,6/25/2017 12:23:00 AM,23.7300777
test.PV,6/25/2017 12:24:00 AM,23.7207565
test.PV,6/25/2017 12:25:00 AM,23.7114372
test.PV,6/25/2017 12:26:00 AM,23.6927948
test.PV,6/25/2017 12:27:00 AM,23.6741543
test.PV,6/25/2017 12:28:00 AM,23.6648331
test.PV,6/25/2017 12:29:00 AM,23.6461945
test.PV,6/25/2017 12:29:59 AM,23.6368713
test.PV,6/25/2017 12:30:59 AM,23.6182308

我需要四舍五入,如下所示:

test.PV,6/25/2017 12:10:59 AM,23.9
test.PV,6/25/2017 12:11:59 AM,23.9
test.PV,6/25/2017 12:12:59 AM,23.9
test.PV,6/25/2017 12:14:00 AM,23.9
test.PV,6/25/2017 12:15:00 AM,23.9
test.PV,6/25/2017 12:16:00 AM,23.8
test.PV,6/25/2017 12:17:00 AM,23.8

并仅选择重复值的第一行

test.PV,6/25/2017 12:10:59 AM,23.9
test.PV,6/25/2017 12:16:00 AM,23.8

我可以与MIN (time) OVER (PARTITION BY tag)一起使用round,但是我需要分组依据。

谢谢, S

3 个答案:

答案 0 :(得分:0)

嗯。这就是你想要的吗?

boost::optional<T> get(int index, T& obj)
{
    if(not_exists(index))
        boost::none;
    else
        return get_base(index);
}

答案 1 :(得分:0)

-- Q: I need to round it, like below:
-- A: Convert the value to decimal(3,1) for example
select convert(decimal(3,1), 23.6182308)

-- Q: and select only first row of duplicated value
-- A option 1:
-- you can use ROW_Number function (supported even in sql server 2008)
-- with partition by all the duplicates columns. 
-- Next you simply filter (using where) the rows with ROW_NUMBER column = 1
-- A option 2: Use DISTINCT in the SELECT

**注意!如果您不执行我写的内容,请发布DDL + DML(查询以创建表并插入一些示例数据以供使用),我将为您提供确切的查询

---更新:添加基于DhruvJoshi发送的DDL + DML的确切示例----

请选中此选项:

;With MyCTE as (
    select
        tag, 
        time, 
        convert(decimal(3,1), [value]) as [value],
        RN = ROW_NUMBER() 
            OVER (partition by tag, convert(decimal(3,1), [value]) order by [time])
    from picomp
)
select tag, [time], [value]
from MyCTE
where RN = 1
GO

我使用的DDL + DML来自DhruvJoshi响应:

create table picomp (tag varchar(10), [time] datetime, [value] decimal(18,8));
insert into picomp values
 ('test.PV','6/25/2017 12:10:59 AM',23.9164886)
,('test.PV','6/25/2017 12:11:59 AM',23.8978481)
,('test.PV','6/25/2017 12:12:59 AM',23.888525)
,('test.PV','6/25/2017 12:14:00 AM',23.8698845)
,('test.PV','6/25/2017 12:15:00 AM',23.8605652)
,('test.PV','6/25/2017 12:16:00 AM',23.8512459)
,('test.PV','6/25/2017 12:17:00 AM',23.8326035)
,('test.PV','6/25/2017 12:18:00 AM',23.8139629)
,('test.PV','6/25/2017 12:19:00 AM',23.7953224)
,('test.PV','6/25/2017 12:20:00 AM',23.77668)
,('test.PV','6/25/2017 12:21:00 AM',23.7673588)
,('test.PV','6/25/2017 12:22:00 AM',23.7487202)
,('test.PV','6/25/2017 12:23:00 AM',23.7300777)
,('test.PV','6/25/2017 12:24:00 AM',23.7207565)
,('test.PV','6/25/2017 12:25:00 AM',23.7114372)
,('test.PV','6/25/2017 12:26:00 AM',23.6927948)
,('test.PV','6/25/2017 12:27:00 AM',23.6741543)
,('test.PV','6/25/2017 12:28:00 AM',23.6648331)
,('test.PV','6/25/2017 12:29:00 AM',23.6461945)
,('test.PV','6/25/2017 12:29:59 AM',23.6368713)
,('test.PV','6/25/2017 12:30:59 AM',23.6182308);
GO
select * from picomp
GO

答案 2 :(得分:0)

使用分组依据可以使用以下查询来获取结果。这是 working demo

select tag, time=min(time), value=min(value)
from (
    SELECT tag, [time], value= cast(value  as decimal(18,1)),
    r=row_number() over( partition by tag order by time asc, cast(value  as decimal(18,1)) asc),
    r2=row_number() over(partition by tag order by cast(value  as decimal(18,1)) asc)
FROM picomp
WHERE tag = 'test.PV' AND [time] >= '6/19/2017'
    )t
group by tag, r-r2