SQL Server:如何编写正确的查询?

时间:2018-12-23 12:28:40

标签: sql sql-server tsql

这是我的查询

SELECT 
    ROW_NUMBER() OVER (ORDER BY [OtpInfoId] ASC ) AS RowNumber, 
    [OtpInfoId] , 
    [OtpStatusId]  
INTO
    #TempTable 
FROM  
    RequestOTP.Main.TbOtpStatusHistory

这是结果的一部分:

RowNumber   OtpInfoId  OtpStatusId
----------------------------------
1           1          2
2           1          1
3           1          9
4           1          5
1           2          2
2           2          1
3           2          9
4           2          5
1           3          2
2           3          1
3           3          9
4           3          5
5           3          7

我想编写一个查询以返回OtpInfoId具有OtpStatusId且至少具有一个状态7的内容

在此示例中,查询应返回以下结果:

RowNumber   OtpInfoId  OtpStatusId
----------------------------------
1           3          2
2           3          1
3           3          9
4           3          5
5           3          7

但是我不知道怎么写。

5 个答案:

答案 0 :(得分:2)

您可以将关联的子查询与WHERE EXIST子句一起使用。

SELECT 
    ROW_NUMBER() OVER (ORDER BY [OtpInfoId] ASC ) AS RowNumber , 
    [OtpInfoId] , 
    [OtpStatusId]  
INTO #TempTable 
FROM  RequestOTP.Main.TbOtpStatusHistory osh 
WHERE EXISTS (
    SELECT 1 
    FROM RequestOTP.Main.TbOtpStatusHistory 
    WHERE OtpStatusId = 7 AND OtpInfoId = osh.OtpInfoId
)

答案 1 :(得分:1)

尝试一下:

SELECT * FROM RequestOTP.Main.TbOtpStatusHistory
WHERE OtpInfoId IN (
  SELECT OtpInfoId FROM RequestOTP.Main.TbOtpStatusHistory WHERE OtpStatusId = 7
)
INTO #TempTable;

此子查询返回所有具有OtpInfoId的{​​{1}}

答案 2 :(得分:1)

一种方法是使用EXISTS

SELECT *
       FROM (SELECT row_number() OVER (ORDER BY [OtpInfoId] ASC) RowNumber,
                    [OtpInfoId],
                    [OtpStatusId]  
                    FROM  [RequestOTP].[Main].[TbOtpStatusHistory]) x
                    WHERE EXISTS (SELECT *
                                         FROM [RequestOTP].[Main].[TbOtpStatusHistory] y
                                         WHERE y.[OtpInfoId] =  x.[OtpInfoId]
                                               AND y.[OtpStatusId] = 7)
       INTO #TempTable;

答案 3 :(得分:0)

这是一种方法:

SELECT
    RowNumber,
    OtpInfoId,
    OtpStatusId
FROM
(
    SELECT
        ROW_NUMBER() OVER (PARTITION BY [OtpInfoId] ORDER BY OtpInfoId) AS RowNumber,
        COUNT(CASE WHEN OtpStatusId = 7 THEN 1 END) OVER (PARTITION BY OtpInfoId) cnt
        [OtpInfoId], 
        [OtpStatusId]  
    FROM RequestOTP.Main.TbOtpStatusHistory
) t
WHERE cnt > 0
INTO #TempTable;

请注意,您期望的行编号不清楚,因为没有明显的列提供该顺序。缺少的是OtpInfoId上的分区,因此我添加了一个分区。

答案 4 :(得分:0)

请尝试使用以下脚本查看其是否满足您的要求。

create table RequestOTP.Main.TbOtpStatusHistory
(   
OtpInfoId  int,
OtpStatusId int
)

insert into RequestOTP.Main.TbOtpStatusHistory values
(1,2),(1,1),(1,9),(1,5),(2,2),(2,1),
(2,9),(2,5),(3,2),(3,1),(3,9),(3,5),
(3,7)

;with cte as 
(
SELECT 
ROW_NUMBER() OVER (partition by OtpInfoId  ORDER BY [OtpInfoId] ASC ) AS RowNumber, 
[OtpInfoId] , 
[OtpStatusId]  
FROM  RequestOTP.Main.TbOtpStatusHistory
)
select * from cte 
where OtpInfoId in 
(select OtpInfoId from RequestOTP.Main.TbOtpStatusHistory where OtpStatusId=7)
/*
RowNumber            OtpInfoId   OtpStatusId
-------------------- ----------- -----------
1                    3           2
2                    3           1
3                    3           9
4                    3           5
5                    3           7
*/

最好的问候

瑞秋