用SQL中的条件按组替换值

时间:2018-06-28 19:57:02

标签: sql sql-server

我有一个SQL数据集,如下所示:

if (get_order_info.Columns.Contains("added_time") != null)
{
    var secondsSince1970 = DateTime.Now - DateTime.Parse("01.01.1970 00:00:00");
    difference = Convert.ToInt64(secondsSince1970.TotalSeconds) - Convert.ToInt64(get_order_info.Rows[0]["added_time"]);
}
else
{
    Label1.Text = "fail";
}

如果对于特定组(例如“ A”),Col2中的值为“是”,则将空值替换为“否”。如果组中不存在“是”,则保留为NULL。

我希望我的最终输出看起来像这样:

Col1    Col2
   A     Yes
   A     Yes
   A    NULL
   B    NULL
   B    NULL
   B    NULL
   C    NULL
   C     Yes

任何帮助都会很棒,谢谢

6 个答案:

答案 0 :(得分:2)

我认为这会起作用...

update table1
set col2 = 'No'
where col2 is null
AND exists (
select *
from table1 a
where table1.col1 = a.col1
AND  col2 = 'Yes')

答案 1 :(得分:0)

使用CTE来选择“是”的组,然后相应地更新表格:

;WITH
    YesGroup AS
    (
        SELECT DISTINCT
                    Col1
        FROM        MyTable
        WHERE       Col2 = 'Yes'
    )

UPDATE T
    SET         Col2 = 'No'
    FROM        MyTable     T
    INNER JOIN  YesGroup    YES     ON T.Col1 = YES.Col1
    WHERE       T.Col2 IS NULL

答案 2 :(得分:0)

如果要避免使用TEMP TABLE,请尝试此操作

OVER(PARTITION BY将在我相信的BIG数据集中胜过

对MERGE好的任务仍然没有回复

  select Col1,
   Case when tempV > 0 and [Col2] is null  then 'No'
     else [Col2]
     end 

   from (
   SELECT   [Col1]
       ,[Col2],
       SUM(CASE when (  [Col2]='Yes') then 1
       else 0 
      end  ) OVER(PARTITION BY [Col1])   as tempV
   FROM [test].[dbo].[Table_1]
  )A

  SELECT   [Col1]
     ,[Col2],
        Case when   SUM(CASE when (  [Col2]='Yes') then 1
      else 0 
      end  ) OVER(PARTITION BY [Col1])  
    > 0 and [Col2] is null  then 'No'
      else [Col2]
   end  new_value
  FROM [test].[dbo].[Table_1]

答案 3 :(得分:0)

我想我对您的要求有所了解,但这有点开放性(不使用CTE,这是另一个可行的选择):

SELECT DISTINCT Col1 INTO #YesGroups FROM dataset WHERE Col2 = 'Yes'

UPDATE A SET Col2 = 'No'
FROM dataset A INNER JOIN #YesGroups ON A.Col1 = B.Col2
WHERE Col2 IS NULL

DROP TABLE #YesGroups

答案 4 :(得分:0)

保持简单:

library(tidyverse)
mtcars %>%
  group_by(cyl, carb) %>%
  summarize(median_mpg = median(mpg),
            avg_mpg = mean(mpg),
            count = n()) %>% 
  ungroup() %>%
  mutate_at(vars(cyl:carb), funs(as.character(.))) %>%
  bind_rows(summarise(cyl = "ttl", carb = "ttl", mtcars, median_mpg = median(mpg),
                      avg_mpg = mean(mpg),
                      count = n()))


# # A tibble: 10 x 5
#   cyl   carb  median_mpg avg_mpg count
#   <chr> <chr>      <dbl>   <dbl> <int>
# 1 4     1           27.3    27.6     5
# 2 4     2           25.2    25.9     6
# 3 6     1           19.8    19.8     2
# 4 6     4           20.1    19.8     4
# 5 6     6           19.7    19.7     1
# 6 8     2           17.1    17.2     4
# 7 8     3           16.4    16.3     3
# 8 8     4           13.8    13.2     6
# 9 8     8           15.0    15.0     1
#10 ttl   ttl         19.2    20.1    32

有关更新:

SELECT
    col1,  
    CASE 
        WHEN col1 IN(SELECT col1 FROM @test WHERE col2 = 'Yes') AND col2 IS NULL THEN 'No'
        ELSE col2
    END col2
FROM someTable

答案 5 :(得分:0)

我只会使用EXISTS

select Col1, coalesce(Col2, 'No') as Col2
from table t
where exists (select 1 from table t1 where t1.col1 = t.col1 and t1.col2 = 'Yes');

现在,您可以使用UPDATE语句了:

update t  
    set Col2 = 'No'
from table t
where exists (select 1 from table t1 where t1.col1 = t.col1 and t1.col2 = 'Yes') and
      Col2 is null;