简体   繁体   English

如何在TSQL中创建布尔计算字段并加入该计算字段?

[英]How do I make a boolean calculated field in TSQL and join on that calculated field?

First check out this code. 首先看看这段代码。 I seems like it should work for me, but it doesn't! 我觉得它应该适合我,但事实并非如此! (surprise!) (惊喜!)

Anyway, this is what I tried first: 无论如何,这是我首先尝试的:

SELECT
Status as status,
Address as ip,
PCName as pc_name,
(Numbers.Phone = 'CPU/' + PCName) as cpu_contact,
(Numbers.Phone = 'PC/' + PCName) as pc_contact,
(Numbers.Phone = 'LOGIN/' + PCName) as login_contact,
FROM IPAddress
WHERE $where  --Generated In code
JOIN Numbers
  ON ('CPU/' + PCName = Numbers.Phone) 
  OR ('PC/' + PCName = Numbers.Phone) 
  OR ('LOGIN/' + PCName = Numbers.Phone)

So what I want is some boolean calculated fields and to join on similar conditions. 所以我想要的是一些布尔计算字段并在类似条件下加入。 I would also like the result to collapse down to single rows. 我还希望结果可以折叠成单行。 For example, I think the current setup would do something like this: 例如,我认为当前的设置会做这样的事情:

status ip  cpu_contact pc_contact login_contact
-----------------------------------------------
foo    bar true        false      false
foo    bar false       true       false
foo    bar false       false      true

And obviously I'd rather 显然我宁愿

status ip  cpu_contact pc_contact login_contact
-----------------------------------------------
foo    bar true        true       true

Any ideas? 有任何想法吗? Database redesign isn't an option. 数据库重新设计不是一种选择。 If it were, I'd do that :-) 如果是的话,我会这样做:-)

You could use a GROUP BY and SUM to collapse the rows: 您可以使用GROUP BYSUM来折叠行:

SELECT
  Status as status, Address as ip, PCName as pc_name,
  cast(sum(case when (Numbers.Phone = 'CPU/' + PCName) then 1 else 0 end) as bit)
    as cpu_contact,
  cast(sum(case when (Numbers.Phone = 'PC/' + PCName) then 1 else 0 end)) as bit)
    as pc_contact,
  cast(sum(case when (Numbers.Phone = 'LOGIN/' + PCName) then 1 else 0 end) as bit)
    as login_contact,
FROM
  IPAddress
    JOIN Numbers ON 
      ('CPU/' + PCName = Numbers.Phone) OR ('PC/' + PCName = Numbers.Phone) OR 
      ('LOGIN/' + PCName = Numbers.Phone)
WHERE
  $where  --Generated In code
GROUP BY
  Status, Address, PCName

Since you are doing a logical or between the rows, a sum of zero is false, while any value greater than 0 will be true. 由于您正在执行逻辑或行之间,因此零之和为false,而大于0的任何值都为true。

You need to use Case/When for the comparisons. 您需要使用Case / When进行比较。 In this case, I am hardcoding a 1 or 0, but T-SQL will convert the hard coded numbers to int. 在这种情况下,我硬编码1或0,但T-SQL会将硬编码的数字转换为int。 If you want boolean (bit), you'll need to convert that manually, like this... 如果你想要布尔(位),你需要手动转换,就像这样......

Convert(Bit, Case When Numbers.Phone = 'CPU/' + PCName Then 1 Else 0 End) as cpu_contact,
Convert(Bit, Case When Numbers.Phone = 'PC/' + PCName Then 1 Else 0 End) as pc_contact,
Convert(Bit, Case When Numbers.Phone = 'LOGIN/' + PCName Then 1 Else 0 End) as login_contact,
SELECT
Status as status,
Address as ip,
PCName as pc_name,
case when sum(case when Numbers.Phone = 'CPU/' + PCName then 1 end) > 0 then 'true' else 'false' end as cpu_contact,
case when sum(case when Numbers.Phone = 'PC/' + PCName then 1 end) > 0 then 'true' else 'false' end as pc_contact,
case when sum(case when Numbers.Phone = 'LOGIN/' + PCName then 1 end) > 0 then 'true' else 'false' end as login_contact
FROM IPAddress
JOIN Numbers
  ON ('CPU/' + PCName = Numbers.Phone) 
  OR ('PC/' + PCName = Numbers.Phone) 
  OR ('LOGIN/' + PCName = Numbers.Phone)
WHERE -- your condition goes here
group by status, address, pc_name   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM