如何编写此SQL Server查询?

时间:2017-02-25 09:40:47

标签: sql-server tsql

花了一段时间仍然无法得到它。我在这样的表中有一些行:

uuid, pos (R|S|N), number (1-10), account (string), xtype (string)

唯一行由uuid, pos,数字

标识

我正在尝试编写一个可以找到以下内容的查询:

  • 同样的uuid在哪里
    • account = x,pos = R,number = 1
    • account = x,pos = S,number = 1
    • 没有发生过pos N
    • pos R只有一行数字1,没有其他出现过的这个uuid

希望我提供了足够的信息,如果您需要,请告诉我。

4 个答案:

答案 0 :(得分:1)

您正在寻找符合某些条件的记录的uuids。所以你按每个uuid聚合(即GROUP BY)。

select uuid
from mytable
group by uuid
having count(case when account = 'x' and pos = 'R' and number = 1 then 1 end) > 0
   and count(case when account = 'x' and pos = 'S' and number = 1 then 1 end) > 0
   and count(case when pos = 'N' then 1 end) = 0
   and count(case when pos = 'R' and number <> 1 then 1 end) = 0;

(实际上,uuid + pos是唯一的,您可以删除最后一个条件,因为它包含在第一个条件中,您可以将> 0更改为{{1}如果你觉得这个更清楚。)

答案 1 :(得分:0)

您是否在查询后查找满足所有四个条件的表中包含各行的uuid值的实例?

您可以使用CTE:

with c1 (uuid)
    as (select uuid from myTable where account = 'x' and pos = 'R' and number = 1),
    c2 (uuid)
    as (select uuid from myTable where account = 'x' and pos = 'S' and number = 1),
    c3 (uuid)
    as (select uuid from myTable where pos = 'N'),
    c4 (uuid)
    as (select uuid from myTable where pos = 'R' and number <> 1)

select distinct myTable.uuid 
from myTable
    inner join c1 on myTable.uuid = c1.uuid
    inner join c2 on myTable.uuid = c2.uuid
    left join c3 on myTable.uuid = c3.uuid
    left join c4 on myTable.uuid = c4.uuid
where c3.uuid IS NULL and c4.uuid IS NULL

或者您可以使用exists

select distinct uuid from myTable t
where exists (select 1 from myTable where uuid = t.uuid and account = 'x' and pos = 'R' and number = 1)
  and exists (select 1 from myTable where uuid = t.uuid and account = 'x' and pos = 'S' and number = 1)
  and not exists (select 1 from myTable where uuid = t.uuid and pos = 'N')
  and not exists (select 1 from myTable where uuid = t.uuid and pos = 'R' and number <> 1)

答案 2 :(得分:0)

一种方法是使用条件聚合和over子句:

创建并填充样本表(在将来的问题中保存此步骤)

'//a[contains(@href,"/Best-Sellers-Health-Personal-Care")]'

CTE将包含告诉我们是否满足条件的列:

DECLARE @T AS TABLE 
(
    uuid int,
    pos char(1),
    Number tinyint,
    Account varchar(3),
    xtype char(1)
)

INSERT INTO @T VALUES
(1, 'R', 1, 'abc', 'a'),
(1, 'S', 1, 'def', 'b'),
(1, 'N', 1, 'ghi', 'c'),
(1, 'R', 2, 'jkl', 'd'),
(2, 'R', 1, 'mno', 'e'),
(2, 'S', 1, 'pqr', 'f'),
(2, 'N', 1, 'stu', 'g'),
(3, 'R', 1, 'vwx', 'h'),
(3, 'S', 1, 'yz', 'i')

查询cte:

;WITH CTE AS
(
    SELECT  uuId, 
            pos,
            Number,
            Account,
            xtype,
            SUM(CASE WHEN pos = 'R' AND Number > 1 THEN 1 ELSE 0 END) OVER(PARTITION BY uuId) As WrongR,
            SUM(CASE WHEN pos = 'N' THEN 1 ELSE 0 END) OVER(PARTITION BY uuId) As NExists,
            SUM(CASE WHEN pos = 'R' AND Number = 1 THEN 1 ELSE 0 END) OVER(PARTITION BY uuId) As RightR,
            SUM(CASE WHEN pos = 'S' AND Number = 1 THEN 1 ELSE 0 END) OVER(PARTITION BY uuId) As RightS
    FROM @T
)

结果:

SELECT  uuId, 
        pos,
        Number,
        Account,
        xtype
FROM CTE
WHERE WrongR = 0
AND NExists = 0
AND RightR = 1
AND RightS = 1

答案 3 :(得分:0)

select uuid from table where account = x and pos = R and number  = 1
intersect 
select uuid from table where account = x and pos = S and number  = 1
except 
select uuid from table where                 pos = N 
except 
select uuid from table where                 pos = R and number <> 1