2017-02-25 70 views
-2

花了一段時間在這仍然不能相當得到它。我有一個表中的某些行是這樣的:如何編寫此SQL Server查詢?

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,nu MBER = 1
    • 沒有出現POSň
    • POS R有唯一的編號爲1,沒有其他事件POS的R此UUID

希望我已經提供了足夠的信息,連續請讓我知道你是否需要更多。

+1

你能發佈您正在查詢的查詢英? –

+1

作爲DDL + DML的樣本數據和期望的結果將有助於回答這個(和任何其他)sql問題。另外,你正在使用哪個版本的sql server? –

回答

1

您正在尋找的UUID其中符合一定條件的記錄。所以你按照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如果你發現這個更清楚。)

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) 
0

它使用條件聚集和過度條款的一種方式做到:

創建和填充示例表(保存我們這一步在你未來的問題)

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 
) 

查詢的CTE:

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

結果:

uuId pos  Number Account  xtype 
3  R  1  vwx   h 
3  S  1  yz   i  
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