简体   繁体   English

在T-Sql的公用表表达式中使用IF ELSE语句

[英]Use IF ELSE statement in Common Table Expression in T-Sql

I have a CTE created and I am trying to delete tables using the following. 我创建了CTE,并尝试使用以下方法删除表。 Note that CTE expressions work fine but IF ELSE statements does not. 请注意,CTE表达式可以正常工作,但IF ELSE语句则不能。

IF 
with FinalSales (time, terminal_id, count) as
(
    select time, terminal_id, count(*)
    from Final_Sales
    group by time, terminal_id
    having count(*) = 1             -- condition here
)

delete Sales
from FinalSales
inner join Sales
    on Sales.[time] = FinalSales.[time] 
    and Sales.terminal_id = FinalSales.terminal_id

ELSE IF 

  with FinalSales (time, terminal_id, count) as
  (
    select time, terminal_id, count(*)
    from Final_Sales
    group by time, terminal_id
    having count(*) > 1              -- Condition here
  )
  delete Sales
  from FinalSales
  inner join Sales
      on Sales.[time] = FinalSales.[time] 
      and Sales.terminal_id = FinalSales.terminal_id

   delete #temp1
   from FinalSales
   inner join #temp1
        on #temp1.[time] = FinalSales.[time] 
        and #temp1.terminal_id = FinalSales.terminal_id

In the first IF statement, when count(*) = 1 then I delete rows in one table, and then in ELSEIF statement when count > 1 I want to delete rows in two tables. 在第一个IF语句中,当count(*) = 1我删除一个表中的行,然后在ELSEIF语句中,当count > 1我要删除两个表中的行。 However, my approach does not work. 但是,我的方法行不通。

How can I wrap it into a IF ELSE statement? 如何将其包装到IF ELSE语句中? OR is there any other ways to do this? 还是有其他方法可以做到这一点?

Whole query in simple structure, 整个查询结构简单,

if count(*) == 1:
   delete Sales
else if count(*) > 1:
   delete Sales, #temp1

something like this. 这样的事情。 How can I do it in t-sql? 如何在t-sql中执行此操作?

Any ideas would be appreciated. 任何想法,将不胜感激。

I think this might work: 我认为这可能有效:

declare @count_of_duplicates int

set @count_of_duplicates = 

(select max(duplicate_count)
from
(       select time, terminal_id, count(*) as duplicate_count
        from Final_Sales
        group by time, terminal_id)  t1)

We'll first set the parameter equal to the greatest value of count(*) . 我们首先将参数设置为等于count(*)最大值。 If there is a duplicate it will be a value greater than 1, else if there is no duplicate it will be 1. 如果存在重复项,则其值将大于1;否则,如果存在重复项,则其值为1。

We can then proceed to start our condition: 然后我们可以开始我们的条件:

if @count_of_duplicates = 1

begin
with FinalSales (time, terminal_id, count) as
(
    select time, terminal_id, count(*)
    from Final_Sales
    group by time, terminal_id
)

delete Sales
from FinalSales
inner join Sales
    on Sales.[time] = FinalSales.[time] 
    and Sales.terminal_id = FinalSales.terminal_id
end

else if @count_of_duplicates > 1
begin
  with FinalSales (time, terminal_id, count) as
  (
    select time, terminal_id, count(*)
    from Final_Sales
    group by time, terminal_id
  )
  delete Sales
  from FinalSales
  inner join Sales
      on Sales.[time] = FinalSales.[time] 
      and Sales.terminal_id = FinalSales.terminal_id

   delete #temp1
   from FinalSales
   inner join #temp1
        on #temp1.[time] = FinalSales.[time] 
        and #temp1.terminal_id = FinalSales.terminal_id
end

I haven't had the chance to parse it... let me know if it works or not. 我还没有机会解析它……让我知道它是否有效。

Why not use if exists(query)? 为什么不使用if exist(query)?

IF exists (
    select time, terminal_id 
    from Final_Sales 
    group by time, terminal_id 
    having count(*) = 1
) begin
    --query
end else begin
    --query
end 

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

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