如果有两个以上相同值的单元格,则将单元格替换为NULL

时间:2014-01-23 13:07:16

标签: sql sql-server

我有这个查询使用CTE并连接2个表,但其中有1个ubytov多次。

   with CTE as
(
  select Count(u.z) as Reserved, u.z as Z
  from ubytov u
  inner join klient K on  u.[text] = K.ubytov
  LEFT OUTER JOIN ubytov U2 ON u.z = U2.id WHERE u.akce = 'FF1231-00'
  group by u.z

)

SELECT U2.[text] AS [Scheme], K.name AS Name, K.surname AS Surname, U1.akce AS Event,
    U1.[text]  AS [Registered under], u1.z,
   CTE.Reserved ,(U2.x) as [Capacity], (U2.x-CTE.Reserved) as [Free beds]
          FROM klient K

          INNER JOIN ubytov U1 ON U1.[text] = K.ubytov
          INNER JOIN CTE on U1.z = CTE.Z

          LEFT OUTER JOIN ubytov U2 ON U1.z = U2.id WHERE U1.akce = 'FF1231-00'
        ORDER BY U1.[text]

它给我这样的输出:

Scheme      Name    Surname  Event      Register u.  z  R.  C. FB    
1/2 SWC Jaromír Jágr    FF1231-00   Jaromír Jágr    57  3   2   -1
1/2 SWC Radim   Vrbata  FF1231-00   Jaromír Jágr    57  3   2   -1
1/3 SWC Jiří    Hudler  FF1231-00   Jiří Hudler     58  2   3   1
1/2 SWC Patrik  Eliáš   FF1231-00   Patrik Eliáš    57  3   2   -1
1+1 SWC Test    Obsazen FF1231-00   Test Obsazen    65  1   1   0
1/3 SWC Tomáš   Plekan  FF1231-00   Tomáš Plekan    58  2   3   1

注册你。列是注册的 R.是列保留 C.是列容量 FB是免费床柱

我想问你,因为如果NULL列超过1,如果有可能在方案列中显示Registered under值,我就没有尝试过甚至不需要这样做同一个registered under单元格的行?

假设已注册的欠限值为Jaromir Jagr

输出结果为:

Scheme      Name    Surname  Event      Register u.  z  R.  C. FB    
1/2 SWC Jaromír Jágr    FF1231-00   Jaromír Jágr    57  3   2   -1
NULL    Radim   Vrbata  FF1231-00   Jaromír Jágr    57  3   2   -1
对于包含特定registered under的行已经提到的每一行,

为NULL 因此,如果Jaromir Jagr将有Registered under 4行,那么它将是

Scheme      Name    Surname  Event      Register u.  z  R.  C. FB    
1/2 SWC Jaromír Jágr    FF1231-00   Jaromír Jágr    57  3   2   -1
NULL    Name Surname    FF1231-00   Jaromír Jágr    57  3   2   -1
NULL    Name Surname    FF1231-00   Jaromír Jágr    57  3   2   -1
NULL    Name Surname    FF1231-00   Jaromír Jágr    57  3   2   -1

给定表的总输出为:

    Scheme      Name    Surname  Event      Register u.  z  R.  C. FB    
1/2 SWC Jaromír Jágr    FF1231-00   Jaromír Jágr    57  3   2   -1
NULL    Radim   Vrbata  FF1231-00   Jaromír Jágr    57  3   2   -1
1/3 SWC Jiří    Hudler  FF1231-00   Jiří Hudler     58  2   3    1
1/2 SWC Patrik  Eliáš   FF1231-00   Patrik Eliáš    57  3   2   -1
1+1 SWC Test    Obsazen FF1231-00   Test Obsazen    65  1   1   0
1/3 SWC Tomáš   Plekan  FF1231-00   Tomáš Plekan    58  2   3   1

我可以知道应该如何实现这一目标吗?

我认为在registered under上应用外部申请将是方式,但我不知道如何检查是否已存在具有相同registered under的一行。

感谢大家的时间。

1 个答案:

答案 0 :(得分:1)

忽略我之前建议的对CTE的更改

在你的外部选择...

SELECT 
    CASE WHEN RowNum > 1 THEN NULL ELSE Scheme END AS Scheme,Name,Surname,Event,Registered_Under AS [Registered_Under]
    ,Capacity,Free_beds as [Free beds] 
FROM 
(
   SELECT U2.[text] AS [Scheme], K.name AS Name, K.surname AS Surname, U1.akce AS Event,
    U1.[text]  AS [Registered_Under], u1.z,
   CTE.Reserved ,(U2.x) as [Capacity], (U2.x-CTE.Reserved) as [Free_beds]
   ,ROW_NUMBER() OVER (PARTITION BY U1.[text] ORDER BY U1.[text]) RowNum
          FROM klient K

          INNER JOIN ubytov U1 ON U1.[text] = K.ubytov
          INNER JOIN CTE on U1.z = CTE.Z

          LEFT OUTER JOIN ubytov U2 ON U1.z = U2.id WHERE U1.akce = 'FF1231-00'

 )i     
 ORDER BY Registered_Under