不在Date SQL中选择

时间:2018-04-27 07:36:16

标签: sql sql-server sql-server-2014

我有下表:

sDateTime                sItem        sValue
-------------------------------------------
2018-01-01 00:00:00      A            10
2018-01-01 00:00:01      A            15
2018-01-01 00:00:02      A            8
ff.

数据位于second基础上。我想知道的是,我怎么知道桌子上是否有“错过”的第二个?

请指教。
谢谢。

4 个答案:

答案 0 :(得分:2)

使用LAG功能,您可以将每个日期与之前的日期进行比较,看看差异是否超过1秒。

IF OBJECT_ID('tempdb..#Dates') IS NOT NULL
    DROP TABLE #Dates

CREATE TABLE #Dates (
    sDateTime DATETIME)

INSERT INTO #Dates (
    sDateTime)
VALUES
    ('1908-01-01 00:00:09'),
    ('2018-01-01 00:00:00'),
    ('2018-01-01 00:00:01'),
    ('2018-01-01 00:00:04'),
    ('2018-01-01 00:00:05')

;WITH Lag AS
(
    SELECT
        sDateTime = D.sDateTime,
        PreviousAvailableDateTime = LAG(D.sDateTime, 1, NULL) OVER (ORDER BY D.sDateTime ASC)
    FROM
        #Dates AS D
)
SELECT
    L.sDateTime,
    L.PreviousAvailableDateTime,
    IsThereAGap = CASE WHEN DATEADD(SECOND, 1, L.PreviousAvailableDateTime) <> L.sDateTime THEN 'Yes' END
FROM
    Lag AS L

结果:

sDateTime               PreviousAvailableDateTime IsThereAGap
----------------------- ------------------------- -----------
1908-01-01 00:00:09.000 NULL                      NULL
2018-01-01 00:00:00.000 1908-01-01 00:00:09.000   Yes
2018-01-01 00:00:01.000 2018-01-01 00:00:00.000   NULL
2018-01-01 00:00:04.000 2018-01-01 00:00:01.000   Yes
2018-01-01 00:00:05.000 2018-01-01 00:00:04.000   NULL

答案 1 :(得分:1)

  

我怎么知道桌子上是否有'错过'秒?

由于每秒应有一条记录,从第一条记录中减去最后一秒,看看你的记录是否与秒数一样多。

import React from 'react';
import { Scene, Router, ActionConst, Stack, Modal, Tabs } from 'react-native-router-flux';
import { View, Text, Icon } from 'react-native-elements';
//Splash Component
import Splash from '../components/Splash/Splash';

//Authentication Scenes
//removed import screens to simplify

import { StackNavigator } from 'react-navigation';

//Import Store, actions
import store from '../redux/store'
import { checkLoginStatus } from "../modules/auth/actions";

import { color, navTitleStyle } from "../styles/theme";

function TabIcon(props) {
    return (
      <View style={styles.container}>
        <Icon
          color={props.tintColor}
          name={props.iconName}
          size={26}
          />
      </View>
    )
  }

  const styles = {
    container: {
      width: 48,
      height: 42,
      padding: 5,
      justifyContent: 'center',
      alignItems: 'center',
    }
  };

export default class extends React.Component {
    constructor() {
        super();
        this.state = {
            isReady: false,
            isLoggedIn: false
        }
    }

    componentDidMount() {
        let _this = this;
        store.dispatch(checkLoginStatus((isLoggedIn) => {
            _this.setState({isReady: true, isLoggedIn});
        }));
    }

    render() {
        if (!this.state.isReady)
            return <Splash/>

        return (
            <Router>
                <Scene key="root" hideNavBar
                        navigationBarStyle={{backgroundColor: "#fff"}}
                        titleStyle={navTitleStyle}
                        backButtonTintColor={color.black}
                >
                    <Stack key="Main" tabs={true} initial={this.state.isLoggedIn}>
                        //here's the scene that uses the TabIcon function//////////////////
                        <Scene key="Submit" component={Submit} title="Submit" icon={TabIcon} iconName='timer' />
                    </Stack>
                </Scene>
            </Router>
        )
    }
}

答案 2 :(得分:1)

如果要查看所有缺失的秒数,请在递归查询中从最小值到最大值创建它们,并从此集合中删除表格中的值。

with seconds(second, last) as
(
  select min(sdatetime), max(sdatetime) from mytable
  union all
  select dateadd(second, 1, second), last from seconds where second < last
)
select second from seconds
except
select sdatetime from mytable
option (maxrecursion 0);

Rextester演示:http://rextester.com/CEL20691

答案 3 :(得分:0)

你需要这样的查询

SELECT * FROM
(SELECT ISNULL(LAG(sDateTime) OVER (ORDER BY sDateTime),sDateTime) as PrevTime,sDateTime,sItem,sValue FROM [yourTable])t
 WHERE DATEDIFF(s,PrevTime,sDateTime)>1

这将为您提供错过第二个错误之前的记录时间以及错过第二个之后记录的详细信息。