can I use a if clause within a where clause? - filesystems

where
if prog.levelnumber = 2 then
i.level = 2 ---> select only the persons with level = 2
if prog.numnivel=1 then
i.level = 1 ---> select only the persons with level = 1

This looks like SQL to me, so I'm answering it in that way...
Use a case statement:
select fields
from tablename
where i.level = case prog.levelnumber
when 2 then 2
else
case prog.numnivel
when 1 then 1
end
end

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.

Why is this SQL case statement behaving like an OR statement?

Consider the following query:
declare #RentalId int = 1
SELECT
r.RentalId
,r.[Name]
,rt.TypeId
FROM dbo.Rental r
LEFT JOIN dbo.RentalRateType rt ON (
r.RentalId = rt.RentalId
AND rt.TypeId = (
case when rt.TypeId = 6 and coalesce(rt.[Max], rt.[Min]) is not null then 6
when rt.TypeId = 1 and coalesce(rt.[Max], rt.[Min] is not null then 1
else -1 end
))
WHERE r.RentalId = #RentalId
I'm attempting to return a single record/row. The particular rental in question has 2 records in the dbo.RentalRateType table, and when I run the above query, I get 2 results, but I want it to short circuit on the first match in the case where.
Basically, the end user can fill in multiple rate types, more than what you see in this example, and each of those types has a priority. 6 is the highest priority in the example.
So I'm getting this result:
RentalId | Name | TypeId
----------------------------
1 Super Car 6
1 Super Car 1
But if the type (6) exists, I would expect only the first row above returned.
I must be missing something silly. This works as expected:
case when 1=2 then 6
when 1=1 then 1
else -1 end
While I'm here, I'm open to a more efficient manner of handling this if exists.
Use an apply instead, these are an efficient way to get "top n" queries:
SELECT
r.RentalId
, r.[Name]
, oa.TypeId
FROM dbo.Rental r
OUTER APPLY (
SELECT TOP (1)
rt.TypeId
FROM dbo.RentalRateType rt
WHERE r.RentalId = rt.RentalId
ORDER BY
rt.TypeId DESC
) oa
WHERE r.RentalId = #RentalId

SQL Server : select when record is 1 then return line else return the 0 record

I need a little help with a query.
I have written a script that brings back an order number and the number of containers needed (code below):
SELECT
CONI.CONTNO,
CONI.ITEMNO,
CONI.[WEIGHT],
CONI.QTY,
STOK.PGROUP,
CASE WHEN CPRO.TNTCOL = 1 THEN 1
WHEN CPRO.TNTCOL = 0 THEN 0
WHEN CPRO.TNTCOL IS NULL THEN 0 END AS [TNT],
CONI.RECID,
CPRO.RECKEY
INTO
#SUB
FROM
ContItems CONI
LEFT JOIN
ContractItemProfiles CPRO ON CONI.RECID = CPRO.RECKEY
JOIN
Stock STOK ON CONI.ITEMNO = STOK.ITEMNO
WHERE
STOK.PGROUP LIKE 'FLI%'
SELECT
#SUB.CONTNO,
#SUB.TNT,
SUM(#SUB.QTY) AS [Number of flight cases]
FROM
#SUB
WHERE
#SUB.CONTNO = '123/321581'
GROUP BY
#SUB.CONTNO,
#SUB.TNT
DROP TABLE #SUB
I get this result:
Contno TNT Number of flight cases
------------------------------------------
123/321581 0 20.00
123/321581 1 1.00
I need to conditionally bring back the line that has the TNT = 1 Else if there isn't a 1 in the TNT column then bring back the record with 0
I hope this is explained enough.
That case can be replaced with
isnull(CPRO.TNTCOL, 0)
select top 1
from ( SELECT #SUB.CONTNO,
#SUB.TNT,
SUM(#SUB.QTY) AS [Number of flight cases]
FROM #SUB
WHERE #SUB.CONTNO = '123/321581'
) t
order by TNT desc

How subtract two aliases which are result of two cases

I'm struggling trying to subtract to results of a case within a SQL query. Do you guys can help me out?
Here is a "simplified" version of the query.
SELECT dbo.tbla.cola, dbo.tblb.colb, dbo.tblb.colc,
CASE WHEN
(SELECT TOP 1 dstStart
FROM tblDst
WHERE tblcd.Key = dbo.tblDst.Key) >= DTimeU AND
(SELECT TOP 1 dstEnd
FROM tblDst
WHERE tblcd.Key = dbo.tblDst.Key) <= DTimeU
THEN tblcd.ApUtc + 1 ELSE tblcd.ApUtc END AS DepDST, CASE WHEN
(SELECT TOP 1 dstStart
FROM tblDst
WHERE tblca.Key = dbo.tblDst.Key) >= ATimeU AND
(SELECT TOP 1 dstEnd
FROM tblDst
WHERE tblca.Key = dbo.tblDst.Key) <= ATimeU THEN tblca.ApUtc + 1 ELSE tblca.ApUtc END AS ArrDST
FROM ...
WHERE ...
How could I perform (ArrDST-DepDST) As DiffDST ?
Regards,
Dave
Simply wrap the query with another select :
SELECT t.*, t.ArrDST - t.DepDST as DiffDST
FROM (YOUR QUERY HERE) t
Then all the calculated columns will be available for use.

How case in SELECT clause can affect the resultset in 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.

Resources