SQL选择独占或/

时间:2015-10-05 13:22:13

标签: sql-server-2008-r2

我需要一个基于独占Or语句的查询,我尝试使用这个案例但是在Null的情况下我不会得到结果......

...
and b.[U_Periode] = CASE
                        when (b.U_Periode= @period) then @period
                        when (b.U_Periode is NULL ) then null
                    end 
...

如果B.U_Status为null且b.U_Periode为null,则不会捕获的Case为...... 如果var Periode与Value匹配且U_Status = 0或1

唯一让我这样做的方法是:

...
and
ISNULL( b.[U_Status],'0') = CASE 
                    when (b.U_Status= '1') then '1'
                    when (isnull( b.U_Status,'0')= '0') then '0'

                end 
and 

ISNULL (b.[U_Periode],'01.01.1901') = CASE
                    when (b.U_Periode= @period) then @period
                    when (ISNULL (b.U_Periode,'01.01.1901') = '01.01.1901' ) then '01.01.1901'
                end 

还有其他更好的解决方案吗? 最好的祝福 奥利弗

好的......这是我的问题

表1 InsID ContractID 1 1 2 1 表2

ID  insid   Period      Status  Count
1   1       null        null    100
2   1       30.09.2015  1       500

3   2       null        null    100
4   2       30.09.2015  1       500

Case '31.08.2015'

in total Value should be 200

in case of '30.09.2015'

the Value should be 1.000

XOR /OR will do the same in this case.

Value case '31.08.2015' = 200
value Case ' 30.09.2015 = 2200

所以这就像子查询一样

left join (
        [dbo].[Table2]b 
        inner join [dbo].[Table 3]K on k.DocEntry = b.DocEntry and CAST( k.U_CSetID as int) >0 
        )
 on b.[U_InsID] in(select... but here I should have an if statement...
  1. 如果有与日期相符的结果,请加入
  2. 如果不是加入结果,则NULL匹配periode ...

3 个答案:

答案 0 :(得分:0)

你必须使用“^”操作数,这是TSQL中的XOR操作数

expression ^ expression

表达式必须返回0或1 ......

愚蠢的例子:

WHERE (name like "stackoverflow") ^ (age > 10)

font:https://msdn.microsoft.com/en-us/library/ms190277(v=sql.105).aspx

更新支票

WHERE (CONVERT(VARCHAR(10), a.[U_Periode], 104) = '30.08.2015') != (a.U_Periode IS NULL)

答案 1 :(得分:0)

好的..这是完整的查询

具有日期31.08.2015的表2应该包含一个记录 B_STATUS = Null和B.Preiode = null并且U_Periode '31 .08.2015'和Staus没有可用的记录...

日期为30.09.2015 有一个记录匹配U_Period = '30 .09.2015'在这种情况下,带U_Period = null的记录不应影响结果......

Declare @period as varchar(20)= '31-08-2015 00:00:00'
declare @Customer as Varchar(15)='12345'
declare @Contract as varchar(30) = '123'
declare @test as varchar(1) =  null


select SUM(cast(K.U_Count as decimal))as counter, K.U_CounterTyp
from [dbo].[Table1] a
left join (
        [dbo].[Table2]b 
        inner join [dbo].[Table 3]K on k.DocEntry = b.DocEntry and CAST( k.U_CSetID as int) >0 
        )
 on b.[U_InsID]=a.[insID] and b.[U_ObjectType]in ('5','1') and 
 ISNULL( b.[U_Status],'0') = CASE 
                    when (b.U_Status= '1') then '1'
                    when (isnull( b.U_Status,'0')= '0') then '0'

                end 
and 

ISNULL (b.[U_Periode],'01.01.1901') = CASE
                    when (b.U_Periode= @period) then @period
                    when (ISNULL (b.U_Periode,'01.01.1901') = '01.01.1901' ) then '01.01.1901'
                end 

where a.[customer] =@Customer and a.[Status]='A' and a.[U_ContrCount]='1'
and a.[manufSN]  in( 
select c.[ManufSN] from [dbo].[Table4] c 
inner join [dbo].[OCTR]d on d.[ContractID] = c.[ContractID]
where c.[ManufSN]=a.[manufSN] 
and d.[CstmrCode] = a.[customer] and d.[ContractID]=@Contract
)
group by K.U_CounterTyp

答案 2 :(得分:-1)

Olay在这里是我的功能,可以选择是否存在日期结果。

唯一的问题是,如果我运行句子的话,表现并不是最好的... foreach记录(达到应用程序1000)我需要查询每个子表...并且有很多记录。 ..

无论如何这是函数​​

CREATE FUNCTION fn_GetInsCounters(@InsId as Varchar(30), @Date as datetime)

returns datetime
as
begin
declare @count as int = 0
declare @Retruns as Datetime
set @count =
            (
                select count( b.Docentry) 
                from [dbo].[Table2]b 
                where b.U_Insid =@InsID 
                and b.U_Status <> '2'
                and b.[U_ObjectType]in ('5','1')
                and b.U_Periode=@Date
            )

if(@count>0)
    begin


        set @Retruns = @date
        end
    else
        begin
         set @Retruns = '01.01.1901'
    end
    return @Retruns
end

如果有人有更好的主意??? 最好的祝福 奥利弗