如何根据Where条件选择MAX值

时间:2019-02-13 11:45:06

标签: sql sql-server tsql

我有示例数据集,如下所示:

   gg1 <- ggplot(data = df1 , aes( x = x , y = y ) ) +
   geom_point( aes(x = x , y = y - 1 , color = cov1 ))  +
   geom_point( aes(x = x , y = y + 1 , color =  cov2  )) +
   scale_y_continuous(limits = c(-3,3)) 

  gg2 <- ggplot(data = df1 , aes( x = x , y = y ) ) +
  geom_point( aes(x = x , y = y - 1 , color = cov1 ))  +
  geom_point( aes(x = x , y = y + 1 , fill =  cov2  ), pch = 21 ) +
  scale_y_continuous(limits = c(-3,3)) 

grid.arrange( gg1 , gg2 , ncol = 2 )

样本数据

Declare @tbl Table (IP VARCHAR(20),IP_Name VARCHAR(30),IP_ID VARCHAR(50))
INSERT INTO  @tbl(IP,IP_Name,IP_ID)VALUES('168.0.0.0','MOHAN-1','AAAAXDXYM')
INSERT INTO  @tbl(IP,IP_Name,IP_ID)VALUES('168.0.0.0','MOHAN-2','BBBYYDDMM')
INSERT INTO  @tbl(IP,IP_Name,IP_ID)VALUES('168.0.1.2','MANASA-1','YYYYMMMD')
INSERT INTO  @tbl(IP,IP_Name,IP_ID)VALUES('168.0.1.2','MANASA-2','JJJKKKLL')
INSERT INTO  @tbl(IP,IP_Name,IP_ID)VALUES('168.0.1.3','Vijay-1','XXLLLLXXX')
INSERT INTO  @tbl(IP,IP_Name,IP_ID)VALUES('168.0.1.3','Vijay-2','SSKSKSLL')

Select * from  @tbl

我想获得基于IP的WHERE条件。

例如,如果我给

IP         IP_Name      IP_ID
168.0.0.0   MOHAN-1   AAAAXDXYM
168.0.0.0   MOHAN-2   BBBYYDDMM
168.0.1.2   MANASA-1  YYYYMMMD
168.0.1.2   MANASA-2  JJJKKKLL
168.0.1.3   Vijay-1   XXLLLLXXX
168.0.1.3   Vijay-2   SSKSKSLL

输出:

WHERE IP IN ('168.0.0.0','168.0.1.2','168.0.1.2','168.0.1.3')

脚本

  IP         IP_Name       IP_ID
168.0.0.0   MOHAN-2     BBBYYDDMM
168.0.1.2   MANASA-1    YYYYMMMD
168.0.1.2   MANASA-2    JJJKKKLL
168.0.1.3   Vijay-2     SSKSKSLL

2 个答案:

答案 0 :(得分:0)

您要根据传入的值选择许多行。WHERE不能完全做到这一点,尤其是对于IN。相反,您可以枚举每个来源中的值并使用JOIN

with t as (
      select t.*,
             row_number() over (partition by ip order by ip_name) as seqnum
      from ?
     ),
     ips as (
      select ip, count(*) as cnt
      from (values ('168.0.0.0'), ('168.0.1.2'), ('168.0.1.2'), ('168.0.1.3')
           ) v(ip)
      group by ip
     )
select
from t join
     ips
     on t.ip = ips.ip and t.seqnum <= ips.cnt;

答案 1 :(得分:0)

我认为,通过像('168.0.0.0','168.0.1.2','168.0.1.2','168.0.1.3')这样两次指定IP,您想要获得该特定IP的两条记录。

在这种情况下,您可以尝试以下操作。

SELECT ip,IP_NAME,IP_ID 
FROM   (SELECT t.*, 
               i.ct, 
               Row_number() 
                 OVER ( 
                   partition BY t.ip 
                   ORDER BY ip_name DESC) AS seqnu 
        FROM   @tbl t 
               INNER JOIN ( VALUES ('168.0.0.0',1), 
                                   ('168.0.1.2',2), 
                                   ('168.0.1.3',1) )i(ip, ct) 
                       ON t.ip = i.ip) t 
WHERE  t.seqnu <= t.ct 
ORDER BY IP, IP_NAME

Online Demo

输出

+-----------+----------+-----------+
| IP        | IP_NAME  | IP_ID     |
+-----------+----------+-----------+
| 168.0.0.0 | MOHAN-2  | BBBYYDDMM |
+-----------+----------+-----------+
| 168.0.1.2 | MANASA-1 | YYYYMMMD  |
+-----------+----------+-----------+
| 168.0.1.2 | MANASA-2 | JJJKKKLL  |
+-----------+----------+-----------+
| 168.0.1.3 | Vijay-2  | SSKSKSLL  |
+-----------+----------+-----------+