How case in SELECT clause can affect the resultset in SQL Server? - sql-server

I'm facing a strange behavior in SQL Server. I have a query how return 233 rows and it's fine.
The problem is if I add a condition in a CASE, well, she returns 48 rows. How is it possible ? CASE shouldn't change my results : it's a CASE in a SELECT clause and there is no WHERE on it.
See the query below. The part between * ... * is the condition who change the result of the entire query.
SELECT * FROM (
SELECT
CASE
WHEN dq.type_of_cover = 'ndd' AND ah.type_of_cover = 'ndaa' THEN ah.type_of_cover
ELSE dq.type_of_cover
END AS type_of_cover,
CASE
WHEN (dq.type_of_cover = 'CCD' AND dq.result_code = 'CCP') THEN NULL --a.mntgex
WHEN (ah.type_of_cover = 'ndaa' *** AND ah.date = GETDATE() ***) THEN ah.first_amount
ELSE ISNULL(dq.first_amount, 0)
END AS problem,
dq.active
FROM (
SELECT du.coddeb, du.coduti
FROM cacheDebiteurConsolideByUtilisateur AS du
WHERE du.coduti = 4102
) AS debi
INNER JOIN DecisionQueue AS dq ON dq.coddeb = debi.coddeb
OUTER APPLY (
SELECT TOP 1 type_of_cover, MIN(date) AS date, first_amount, coddeb, codadh
FROM AtradiusCLHistory
WHERE codadh = 1003
AND dq.coddeb = coddeb
GROUP BY type_of_cover, first_amount, coddeb, codadh
) AS ah
WHERE dq.codadh = 1003
AND dq.cancelled != 1
) AS test
WHERE type_of_cover = 'ndaa' AND active = 1
So when I got AND ah.date = GETDATE() in the "problem" CASE, I have 48rows, when not, 233.

Related

Sql query with fetch by condition

Good day to you all. Faced with a problem and I am in a stupor, please help. You need to write an sql query according to the condition: where code = 2 and num = 2 then change the value, if code = 3 then take the record whose value code = 1
, a as (
select numberfn, street
from tbl
where code = 2
and nume = 2)
, b as (
select N from tbl where code = '3'
)
, actual as (
select b.value, a.N
from b
join a
on b.N = a.N
where b.code = '1'
union all
select st.value, st.N
from rreg st
join tbl b
on st.N = b.N
)
I tried to simplify it using case as well, but the second condition leads me to a dead end.
, actual as (
select N
, case when (code= 2 and num= 2)
then value
else .... (condition: select an entry for N where code = 1)
end value
from tbl
)
You have 2 requirements here, one is to update, the other is to select a value.
Separately they are:
UPDATE table
SET value = ??
WHERE code = 2 AND num = 2
And
SELECT * FROM table
WHERE code = 3 AND value = 1
(Where ?? = value you want to change the column value to.)
If you want both in one function, then they can be moved into a stored procedure.

Get value from the select statement on the row above in T-SQL

I want to get the value the previous select statement returned (similar to the ##IDENTITY function)
Code:
IF EXISTS (SELECT MyCriticalValue
FROM Units u
JOIN pUnit pu
ON pu.UnitID = u.UnitID
WHERE u.ChildUnitID = ( SELECT ps.UnitID
FROM psUnit ps
WHERE ps.Code = #CurrentCode)
AND pu.Type = 117
AND u.TypeID = 1 )
BEGIN
SET #CriticalValue = ##identity
END
I know that ##IDENTITY doesn't work since that's only for inserts. Is there a similar function to achieve what I want (which is to get "MyCriticalValue" from the select statement into the variable #CriticalValue without typing the entire SELECT statement twice).
EDIT:
Clarification: When I come to this point in my SQL script #CriticalValue is already set and I should ONLY overwrite it if the select-statement returns anything, so it should not be overwritten in any other case.
SELECT #CriticalValue = ISNULL(MyCriticalValue, #CriticalValue)
FROM Units u
JOIN pUnit pu
ON pu.UnitID = u.UnitID
WHERE u.ChildUnitID = (
SELECT ps.UnitID
FROM psUnit ps
WHERE ps.Code = #CurrentCode
)
AND pu.Type = 117
AND u.TypeID = 1;
IF #CriticalValue IS NOT NULL
...
I assumed that your query return one value.

Update row value only once when multiple results returned in CASE statement

I need to run an UPDATE script with a value returned from a CASE WHEN statement. The condition used might lead to have the same order (table ORDER) affetcted by different erorrs.
As example, order.id = 20 can be affected by Error 1 and Error 2, but I need to update it only once with the value Error 1 (the order in the CASE WHEN shows the priority). The goal would be to have only Error 1 to be set in the field STATUS. Is this possible to be achieved in TSQL? Or should I better implement the logic with a console application?
Thanks.
UPDATE O
SET STATUS = (
CASE
WHEN E.EXPID = 'E1' THEN 'Error1'
WHEN E.EXPID = 'E3' THEN 'Error2'
WHEN E.EXPID = 'E2' THEN 'Error3'
ELSE 'EX'
END )
FROM ORDER O
INNER JOIN EXCP E ON O.ID = E.ORDERID
I would use a CTE to grab the top "error" and join that result to your order table.
;WITH TopError
AS
(
SELECT ROW_NUMBER() OVER(PARTITION BY OrderID ORDER BY ExpID ASC) AS ROWID, *
FROM Excp
)
SELECT *
-- UPDATE O SET STATUS = CASE WHEN T.EXPID = 'E1' THEN 'Error1' WHEN T.EXPID = 'E2' THEN 'Error2' WHEN T.EXPID = 'E3' THEN 'Error3' ELSE 'EX' END
FROM TopError T
JOIN Order O
ON T.ORDERID = O.ID
WHERE T.ROWID=1
Try it as a SELECT, if you're satisfied with the results, switch to an UPDATE.

Differences between equal sign(=) and IN with subquery

I have a query it takes 20 seconds to execute, follow my query:
SELECT MATLIGA.COD_MAT_FAMILIA
FROM
ORCAMENTOS.dbo.OR_1INSUMOS INSUMOS
INNER JOIN ORCAMENTOS.dbo.OR_MAT_GRUPOS GRUPOS ON (GRUPOS.EMPRESA='01' AND GRUPOS.FILIAL='01' AND GRUPOS.CODIGO_INTERNO = 'HOT' )
INNER JOIN ORCAMENTOS.dbo.OR_MATERIAIS MATER ON (MATER.EMPRESA='01' AND MATER.FILIAL='01' AND MATER.CODIGO_GRUPO=GRUPOS.ID AND MATER.ID = INSUMOS.COD_INSUMO_MATER )
INNER JOIN ORCAMENTOS.dbo.OR_MAT_LIGACAO MATLIGA ON (MATLIGA.EMPRESA='01' AND MATLIGA.FILIAL='01' AND MATLIGA.CODIGO_MATERIAL = INSUMOS.COD_INSUMO_MATER)
WHERE INSUMOS.EMPRESA='01' AND INSUMOS.FILIAL='01'
AND INSUMOS.COD_INSUMO_MATER IS NOT NULL
AND INSUMOS.NUMERO=10865812
AND INSUMOS.OPCAO_SIMULACAO=1
AND INSUMOS.CODIGO_MAQUINA = (SELECT COD_MAQ_PROPOSTA FROM ORCAMENTOS.dbo.OR_1SIMULACOES AS ORC WHERE ORC.NUMERO=10865812 AND ORC.OPCAO_SIMULACAO = 1 AND ORC.EMPRESA='01' AND ORC.FILIAL='01' )
AND INSUMOS.OPCAO_MAQUINA = (SELECT OPCAO_MAQUINA FROM ORCAMENTOS.dbo.OR_1SIMULACOES AS ORC WHERE ORC.NUMERO=10865812 AND ORC.OPCAO_SIMULACAO = 1 AND ORC.EMPRESA='01' AND ORC.FILIAL='01' )
GROUP BY MATLIGA.COD_MAT_FAMILIA
ORDER BY 1
In these two lines bellow, if I change the equal signal by (IN), ( = ALL ) or ( = ANY ) it reduces the costs to 1 second.
AND INSUMOS.CODIGO_MAQUINA IN (SELECT COD_MAQ_PROPOSTA FROM ORCAMENTOS.dbo.OR_1SIMULACOES AS ORC WHERE ORC.NUMERO=10865812 AND ORC.OPCAO_SIMULACAO = 1 AND ORC.EMPRESA='01' AND ORC.FILIAL='01' )
AND INSUMOS.OPCAO_MAQUINA IN (SELECT OPCAO_MAQUINA FROM ORCAMENTOS.dbo.OR_1SIMULACOES AS ORC WHERE ORC.NUMERO=10865812 AND ORC.OPCAO_SIMULACAO = 1 AND ORC.EMPRESA='01' AND ORC.FILIAL='01' )
Whats the difference between them?
Tks.
There is a small semantic difference. The first query must fail if the subquery matches more than one record. So it has to finish the subquery until the end:
where col1 = (select col1 from table2)
The second query can stop once it encounters a match:
where col1 in (select col1 from table2)

SQL query - need to exclude if Requirement NOT met, and exclude if Disqualifier IS met

I have a feeling once i see the solution i'll slap my forehead, but right now I'm not seeing it.
I have a lookup table, say TableB, which looks like this. All fields are INT except the last two which are BOOL.
ID, TableA_ID, Value, Required, Disqualifies
I have a list of TableA_Id values (1, 2, 3 ) etc
For each record in this table, either Required can be true or disqualified can be true - they cant both be true at the same time. They can both be false or null though. There can be duplicate values of TableA_Id but there should never be duplicates of TableA_Id and Value
If required is true for any of those TableA_ID values, and none of those values are in my list, return no records. If none of the values are marked as required (required = 0 or null) then return records UNLESS any of the values are marked as Disqualifies and are in the list, in which case i want to return no records.
So - if a field is required and i dont have it, dont return any records. If a field is marked as disqualified and i have it, don't return any records. Only return a record if either i have a required value or don't have a disqualified value or there are no required values.
I hope I explained myself clearly.
Thanks in advance for pointing me in the right direction.
As an example of what my records might look like:
ID TableA_ID Value Required Disqualifies
-- --------- ----- -------- ------------
1 123 1 True False
2 123 2 True False
3 123 3 False False
4 123 4 False True
5 456 1 False True
6 456 2 False False
Given this set of sample data, if we're working with TableA_Id 123 and my list of values is lets say 1 and 3, i would get data returned because i have a required value and dont have any disqualified values. If my list of values were just 3, i'd get no records since i'm missing of the Required values. If my list of values were 1 and 4, i'd get no records because 4 is marked as disqualified.
Now if we're working with TableA_Id 456, the only list of values that would return any records is 2.
Maybe i should post the whole SQL query - i was trying to keep this short to make it easier for everyone, but it looks like maybe that's not working so well.
Here is the full dynamically generated query. The bit i am working on now is the 2nd line from the bottom. To equate this to my example, t.id would be TableA_ID, Value would be PDT_ID.
SELECT DISTINCT t.ID, t.BriefTitle, stat.Status, lstat.Status AS LocationStatus, st.SType, t.LAgency, l.City, state.StateCode
,( SELECT TOP 1 UserID
FROM TRecruiter
WHERE TrialID = t.ID AND Lead = 1 ), l.ID as LocationID
, l.WebBased
FROM Trial t
INNER JOIN Location l ON t.ID = l.TrialID
FULL JOIN pdt on t.ID = pdt.trialid
FULL JOIN pdm on t.ID = pdm.TrialID
FULL JOIN s on t.ID = s.TrialID
FULL JOIN hy on t.ID = hy.TrialID
FULL JOIN ta on t.ID = ta.TrialID
FULL JOIN stt on t.ID = stt.TrialID
FULL JOIN [Status] stat ON t.StatusID = stat.ID
FULL JOIN st ON t.StudyTypeID = st.ID
FULL JOIN State state ON l.StateID = state.ID
FULL JOIN [Status] lstat ON l.StatusID = lstat.ID
FULL JOIN ts ON t.ID = ts.TrialID
FULL JOIN tpdm ON t.ID = tpdm.TrialID
WHERE ((t.ID IS NOT NULL)
AND (EligibleHealthyVolunteers IS NULL OR EligibleHealthyVolunteers = 1 OR (0 = 0 AND EligibleHealthyVolunteers = 0))
AND (eligiblegenderid is null OR eligiblegenderid = 1 OR eligiblegenderid = 3)
AND ((EligibleMinAge <= 28 AND EligibleMaxAge >= 28) OR (EligibleMinAge <= 28 AND EligibleMaxAge is null) OR (EligibleMinAge IS NULL AND EligibleMaxAge >= 28))
AND (HYID = 6 AND (hy.Disqualify = 0 OR hy.Disqualify IS NULL AND NOT EXISTS (SELECT * FROM hy WHERE t.id = hy.TrialID AND hy.Req =1)) OR HYID = 6 AND hy.req = 1)
AND (PDT_ID IN (1) AND ( pdt.Disqualify = 0 OR pdt.Disqualify IS NULL AND NOT EXISTS (select * from pdt where t.id = pdt.TrialID AND pdt.Req = 1)) OR PDT_ID IN (1) AND (pdt.Req = 1 AND (pdt.Disqualify = 0 or pdt.Disqualify is null )))
) AND ((3959 * acos(cos(radians(34.18)) * cos(radians(l.Latitude)) * cos(radians(l.Longitude) - radians(-118.46)) + sin(radians(34.18)) * sin(radians(l.Latitude)))) <= 300 OR l.Latitude IS NULL) AND t.IsPublished = 1 AND (t.StatusID = 1 OR t.StatusID = 2)
I've changed/shortened some table names just for security/privacy reasons.
Edit:
I think i am close to getting this working, but I'm getting tripped up on the logic again.
I have the following bit of sql:
AND ( exists (SELECT * FROM pdt WHERE Req = 1 AND trialid = t.id AND pdT_ID IN (2) ) AND EXISTS (SELECT * FROM pdt WHERE Req = 1 AND trialid = t.id ) )
I'm not sure how to structure this. Those two exists statement should make the whole thing true in the following combination:
True & False
True & True
False & False
If it's False & True, then the whole thing is false. In other words if there is a Req =1 AND the PDT_ID that is marked as Req=1 is not in our list (in the example above the list just contains '2') then return false.
EDIT:
I think i finally got it.
AND NOT EXISTS (SELECT * FROM pdt WHERE Disqualify = 1 AND trialid = t.id AND PDT_ID IN (2) )
AND NOT ( NOT exists (SELECT * FROM pdt WHERE Req = 1 AND trialid = t.id AND PDT_ID IN (2) ) AND EXISTS (SELECT * FROM pdt WHERE Req = 1 AND trialid = t.id ) )
So far this seems to work in testing. Although I'm only working with two values of PDT_ID. If this does resolve my problem, i will come back and give someone the credit for helping me.
SELECT *
FROM TABLEB B
WHERE
(
B.REQUIRED = 1
AND EXISTS
(
SELECT 1
FROM TABLEA A
WHERE A.ID =B.TABLEA_ID
)
)
OR
(
B.REQUIRED != 1
AND B.DISQUALIFIES <> 1
)
OR
(
B.REQUIRED != 1
AND B.DISQUALIFIES = 1
AND EXISTS
(
SELECT 1
FROM TABLEA A
WHERE A.ID =B.TABLEA_ID
)
)
UPDATE - after the EDIT and explanation from OP:
Change the line
FULL JOIN pdt on t.ID = pdt.trialid
To
FULL JOIN (SELECT * FROM pdt BB WHERE
BB.TrialID IN (SELECT AA.ID FROM Trial AA WHERE AA.ID = BB.TrialID) AND
1 > (SELECT COUNT(*) FROM Trial A
LEFT OUTER JOIN pdt B ON B.Req != 1 AND B.Disqualify != 1 AND B.TrialID = A.ID
WHERE B.TrialID IS NULL)) pdt ON t.ID = pdt.TiralID
AND change the line before last from
AND (PDT_ID IN (1) AND ( pdt.Disqualify = 0 OR pdt.Disqualify IS NULL AND NOT EXISTS (select * from pdt where t.id = pdt.TrialID AND pdt.Req = 1)) OR PDT_ID IN (1) AND (pdt.Req = 1 AND (pdt.Disqualify = 0 or pdt.Disqualify is null )))
To
AND PDT_ID IN (1)
(You seem to have found a solution, yet I've decided to share my thoughts about this problem anyway.)
Given you've got a set of TableA IDs, each of which is accompanied by a set of some values, and you want to test the entire row set against this TableB thing using the rules you've set forth, I think the entire checking process might look like this:
Match every pair of TableA.ID and Value against TableB and get aggregate maximums of Required and Disqualifies for every TableA.ID along the way.
Derive a separate list of TableA_ID values with their corresponding maximum values of Required, from TableB. That will be for us to know whether a particular TableA_ID must have a required value at all.
Match the row set obtained at Stage 1 against the derived table (Stage 2) and check the aggregate values:
1) if the actual aggregate Disqualifies for a TableA_ID is 1, discard this TableA_ID set;
2) if a TableA_ID has a match in the Stage 2 derived table and the aggregate maximum of Required that we obtained at Stage 1 doesn't match the maximum Required in the derived table, discard the set as well.
Something tells me that it would be better at this point to move on to some sort of illustration. Here's a sample script, with comments explaining which part of the script implements which part of the description above:
;
WITH
/* this is the row set to be tested and which
is supposed to contain TableA.IDs and Values */
testedRowSet AS (
SELECT
TableA.ID AS TableA_ID,
SomethingElse.TestedValue AS Value,
...
FROM TableA
JOIN SomethingElse ON some_condition
...
),
/* at this point, we are getting the aggregate maximums
of TableB.Required and TableB.Disqualifies for every
TableA_ID in testedRowSet */
aggregated AS (
SELECT
testedRowSet.TableA_ID,
testedRowSet.Value,
...
DoesHaveRequiredValues = MAX(CASE TableB.Required WHEN 1 THEN 1 ELSE 0 END) OVER (PARTITION BY testedRowSet.TableA_ID),
HasDisqualifyingValues = MAX(CASE TableB.Disqualifies WHEN 1 THEN 1 ELSE 0 END) OVER (PARTITION BY testedRowSet.TableA_ID)
FROM testedRowSet
LEFT JOIN TableB ON testedRowSet.TableA_ID = TableB.TableA_ID
AND testedRowSet.Value = TableB.Value
),
/* this row set will let us see whether a particular
TableA_ID must have a required value */
properties AS (
SELECT
TableA_ID,
MustHaveRequiredValues = MAX(CASE Required WHEN 1 THEN 1 ELSE 0 END)
FROM TableB
GROUP BY TableA_ID
),
/* this is where we are actually checking the previously
obtained aggregate values of Required and Disqualifies */
tested AS (
SELECT
aggregated.TableA_ID,
aggregated.Value,
...
FROM aggregated
LEFT JOIN properties ON aggregated.TableA_ID = properties.TableA_ID
WHERE aggregated.HasDisqualifyingValues = 0
AND (properties.TableA_ID IS NULL
OR properties.MustHaveRequiredValues = aggregated.DoesHaveRequiredValues)
)
SELECT * FROM tested

Resources