Second Order By overrides first in SQL Server - sql-server

In the following code the purchase_price overrides the case order by statement.
SELECT
i.products_mpn, i.active,
i.distributor_code, i.sales_price
FROM
#duplicates d
LEFT JOIN
import i ON i.products_mpn = d.products_mpn
AND i.distributor_code = (SELECT TOP 1 distributor_code
FROM dbo.kting_ICECAT_import
WHERE products_mpn = d.products_mpn
ORDER BY
(SELECT
CASE
WHEN (i.distributor_code = 'x' or i.distributor_code = 'y') AND CAST(stock as int) > 0
THEN 2
WHEN CAST(stock as int) > 0
THEN 1
ELSE 0
END) DESC,
CAST(purchase_price AS decimal(28,12)))
Is there any way to get it to work with ordering by the case statement and then the purchase_price?

If you want the ordering of the CASE statement to be applied first followed by the ordering of the purchase_price, then your ORDER BY already has the terms in the right place. I don't know what SELECT is doing in your CASE expression, or even if your current query runs. Something like this may be what you had in mind:
ORDER BY CASE WHEN (i.distributor_code = 'x' or i.distributor_code = 'y') AND
CAST(stock as int) > 0 THEN 2
WHEN CAST(stock as int) > 0 THEN 1
ELSE 0
END DESC,
CAST(purchase_price AS DECIMAL(28,12))

The problem was in the ORDER BY SELECT. I had used columns from the left join "i.distributor_code" should just have been distributor_code

Related

how to count quantity of different between 2 tables?

i need to count different between 2 tables (same table from 2 different days) to see what have changed.
for example table 1:
table 2:
and i want to get this table:
i try this code:
select a.of_key , case when a.color != b.color then count (a.color) ELSE 0 END AS color,
case when a.side != b.side then count (a.side) else 0 end as side
from 130720 A right JOIN 100720 B
ON a.of_key = b.of_key and a.num = b.bum
group by a.of_key
it is not working
please help
thanks!
Put your aggregate outside the case statement:
select a.of_key , SUM(case when a.color != b.color then 1 ELSE 0 END) AS color,
SUM(case when a.side != b.side then 1 else 0 end) as side
from 130720 A right JOIN 100720 B
ON a.of_key = b.of_key and a.num = b.bum
group by a.of_key

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

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.

SQL Server Remove unwanted rows from a CASE statement

I do a SELECT with a CASE statement with this following:
SELECT DISTINCT
n.NiveauId, n.Description,
CASE WHEN n.NiveauId NOT IN (SELECT ccs.idNiveau WHERE ccs.centreCout = 60001) THEN 0 ELSE 1 END AS attribue
FROM pa.dbo.Niveau n
JOIN BDC.dbo.CentreCoutSecteur ccs ON n.NiveauId = ccs.idNiveau
Explication :
In case "NiveauId" is not present in the other table, the value of "attribue" is 0. Else, if it's present, the value is 1.
This works, but every rows that contains a 1 also shows the same row with a 0.
Exemple:
How would I change the SELECT query to remove the unwanted duplicate rows that contain 0?
Thanks in advance!
Try wrapping your select in a max (if you only want the rows with the highest value for attribue.
SELECT b.NiveauID, b.Description, MAX(b.attribue)
FROM
(SELECT DISTINCT
n.NiveauId, n.Description,
CASE WHEN n.NiveauId NOT IN (SELECT ccs.idNiveau WHERE ccs.centreCout = 60001) THEN 0 ELSE 1 END AS attribue
FROM pa.dbo.Niveau n
JOIN BDC.dbo.CentreCoutSecteur ccs ON n.NiveauId = ccs.idNiveau) b
Group By b.NiveauID, b.Description

Add ORDER BY statement to SELECT statement

I have the following SELECT statement and I need to add ORDER BY [UserName].
SELECT *
FROM [UserInfo]
WHERE ([AWSAccountID] = CASE WHEN #AWSAccountID = -1 THEN [AWSAccountID]
ELSE #AWSAccountID END)
I'm having some trouble figuring out where the ORDER BY needs to go, assuming it can be added at all.
Unless I am missing something you just add your ORDER BY to the end of the query:
SELECT *
FROM [UserInfo]
WHERE ([AWSAccountID] = CASE WHEN #AWSAccountID = -1 THEN [AWSAccountID]
ELSE #AWSAccountID END)
ORDER BY [UserName]

Resources