SQLTeam.com | Weblogs | Forums

Problem with eqval and not eqval on sql

Good day today come across of a intrsting problem in end of my curi when i say
A1.Month = b2.date its working but when i am saying
A1.Month = b2.date or A1.Month != b2.date or
where not A1.Month = b2.date its not working what can be a problem ?

my idea

these sort of things are very common in SQL

everyone runs into these issues ALL THE TIME

debugging .. is very common

there are ways to find out what's the problem
( that's another PHD subject by itself )

create Sample Data

do the testing

drop table #sampledata
create table #sampledata
month date, 
Date date

insert into #sampledata values

select 'sample data', * from #sampledata 

select 'a.month= b.Date',* from #sampledata a join #sampledata b on a.month= b.Date

select 'A.Month = b.date or A.Month != b.date',* from #sampledata a join #sampledata b on A.Month = b.date or A.Month != b.date

Is there more filters in the where clause you are not showing us. When you introduce OR in the where clause you have to remember to use () parenthesis in appropriate places

What do you mean by "not working"?

Also, be aware that a NULL value will fail all = or != tests.

the value is not nul its nvarcahr on both sides
and by not working i mean tat when i apply = parameter is does the calcuklation
and when i am tryin anything ot of <> =! or not it just ignore and show evrything

We need to see the full query. And some sample data rows via CREATE TABLE and INSERT statements. Sounds like there is something wrong in your join.

Step one 1

create table olap.sampledata1
[Date] date ,TC1 varchar(100),Amount_New decimal(10,2)
Step 2

INSERT INTO [OLAP].[sampledata1]
VALUES ('2020-11-01','Houses:N-Home',70),
('2020-11-01','Default Category: Default Option',90),
('2020-12-01','Default Category: Default Option',250),
('2020-10-01','Default Category: Default Option',70)

Step 3

[Name] nvarchar NULL,
[Source] nvarchar NULL,
[AllocateToTc] nvarchar NULL,
[Percent] nvarchar NULL,
[date] nvarchar NULL,
[AllocateFromTc] nvarchar NULL

Step 4

('1','User','Houses: NS-Home','25','2020-11-01','Default Category: Default Option'),
('2','User','Houses: N-KL',50', '2020-11-01','Default Category: Default Option'),
('3','User','Houses: N-GR','25', '2020-11-01','Default Category: Default Option'),
('4','User','Houses: N-Home','20', '2020-12-01','Default Category: Default Option'),
('5','User','Houses: N-KL','10', '2020-12-01','Default Category: Default Option'),
('6','User','Houses: N-GR','50', '2020-12-01','Default Category: Default Option'),
('7','User','Houses: N-BR','20', '2020-12-01','Default Category: Default Option')

Step 5

CREATE VIEW [OLAP].[VW_Deimos_view]
select distinct
when A1.Date = B2.Date
and a1.TC1 = b2.AllocateFromTc
then AllocateToTc
else a1.TC1
end as Tracking_Category_d,
when A1.TC1 = b2.AllocateFromTc
and A1.Date = B2.Date
then A1.Amount_New*[Percent]/100
else A1.Amount_New
end as Amountd

from olap.sampledata1 A1 , OLAP.Test B2