简体   繁体   English

我想要有条件的第一个和第二个插入值

[英]I want first and second inserted value with condition

Table abc : abc

Consgno     Name      Entrydatetime

 111          A     01/03/2017 10:10:15
 111          A     01/03/2017 10:20:15
 111          A     01/03/2017 11:10:20
 222          B     02/03/2017 10:10:25
 333          C     06/03/2017 10:10:25
 333          C     07/03/2017 10:10:12
 444          D     04/03/2017 10:10:41
 444          D     04/03/2017 01:10:20
 444          D     06/03/2017 10:10:32
 555          E     05/04/2017 10:10:15

One Consgno has entered ONE more than one time. 一个Consgno已多次输入ONE。

When one Consgno is only once, then the first value should come, otherwise the second entered value should come. 当一个Consgno仅一次时,则第一个值应到,否则第二个输入值应到。

I want to output like this: 我想这样输出:

   Consgno  Name     Entrydatetime
    111      A      01/03/2017 10:20:15
    222      B      02/03/2017 11:10:36
    333      C      07/03/2017 10:10:12
    444      D      04/03/2017 01:10:20
    555      E      05/04/2017 10:10:15

You can use a query like the following: 您可以使用如下查询:

;WITH MyWindowedTable AS (
   SELECT Consgno, Name, Entrydatetime,
          ROW_NUMBER() OVER (PARTITION BY Consgno
                             ORDER BY Entrydatetime) AS rn,
          COUNT(*) OVER (PARTITION BY Consgno) AS cnt
FROM mytable
)
SELECT Consgno, Name, Entrydatetime
FROM MyWindowedTable
WHERE (cnt = 1 AND rn = 1) OR (cnt > 1 AND rn = 2)

Using windowed version of COUNT : 使用COUNT窗口版本:

COUNT(*) OVER (PARTITION BY Consgno)

returns the population, cnt , of each Consgno partition. 返回每个Consgno分区的填充cnt We can use cnt to properly filter the records returned: in partitions with a population of 1 we get the single record of the partition, whereas in the rest of the cases we get the one having rn = 2 . 我们可以使用cnt正确过滤返回的记录:在人口为1的分区中,将获得该分区的单个记录,而在其余情况下,我们将获得rn = 2

Use ROW_NUMBER and COUNT built in functions : 使用ROW_NUMBER和COUNT个内置函数:

 CREATE TABLE #table1 ( Consgno INT, Name VARCHAR(1), Entrydatetime 
                         DATETIME)

 INSERT INTO #table1 ( Consgno , Name , Entrydatetime )
 SELECT 111,'A','01/03/2017 10:10:15' UNION ALL
 SELECT 111,'A','01/03/2017 10:20:15' UNION ALL
 SELECT 111,'A','01/03/2017 11:10:20' UNION ALL
 SELECT 222,'B','02/03/2017 10:10:25' UNION ALL
 SELECT 333,'C','06/03/2017 10:10:25' UNION ALL
 SELECT 333,'C','07/03/2017 10:10:12' UNION ALL
 SELECT 444,'D','04/03/2017 10:10:41' UNION ALL
 SELECT 444,'D','04/03/2017 01:10:20' UNION ALL
 SELECT 444,'D','06/03/2017 10:10:32' UNION ALL
 SELECT 555,'E','05/04/2017 10:10:15' 

 SELECT Consgno , Name , Entrydatetime 
 FROM 
 (
    SELECT Consgno , Name , Entrydatetime , ROW_NUMBER() OVER (PARTITION BY 
           Consgno ORDER BY Entrydatetime) RNo , COUNT(*) OVER (PARTITION BY 
           Consgno) AS _Count
    FROM #table1
 ) A WHERE ( RNo = 1 AND _Count = 1) OR (_Count > 1 AND RNo = 2 ) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 专家您好! 我想使用触发器将值插入第二个表而不是第一个表 - Hello Experts! I want to insert value to second table instead of first table using trigger 我想查询第一和第二高工资之间的差异 - I want to query difference between first and second highest salary 我想在第一张表中获取不在第二张表中的行 - I want to fetch rows in a first table that are not in a second table 获取第一个表插入的值到第二个表 - Get First Tables Inserted Values to Second table 检查第一个或第二个条件是否存在 - Check if the first or second condition exists 我想将第一个SQL表中的数据插入第二个SQL表中,而第二个表中不存在额外的列 - I want to insert data from first SQL table into second one also with an extra column not present in the second one 我想为SQL Server中的某些情况返回默认值NULL - I want to return a default value as NULL for some condition in SQL Server SQL 服务器:从第二个表插入第一个表中具有匹配条件的每个唯一值 - SQL Server: Insert from second table for every unique value in first table with matching condition 数据已插入我想更新数据 - Data already Inserted I want to update the data 如果第一个表已经存在,则不会插入第二个表 - Second table does not get inserted if first table already exist
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM