Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server: when Else is case it is showing 0 as default in CASE clause

Tags:

sql-server

This query is run on AdventureWorks2014.Sales.SalesOrderDetail table.

My query: in the CASE clause I have clearly stated to show - when it is the ELSE case. This query removes the repeating SalesOrderID and after 1st time shown it should be replaced with "-" this character.

SELECT 
    CASE WHEN ROW_NUMBER() OVER(PARTITION BY SalesOrderID ORDER BY SalesOrderID ASC) = 1 THEN SalesOrderID ELSE '-' END  AS SalesOrderID,
    R.ProductID,
    R.OrderQty,
    R.UnitPrice,
    R.LineTotal

FROM AdventureWorks2014.Sales.SalesOrderDetail AS R

currently showing 0 even though i said in the ELSE to show me -

enter image description here

like image 245
Zabi Sidiqkhil Avatar asked Jan 30 '26 18:01

Zabi Sidiqkhil


1 Answers

Below query will solve your issue

 SELECT 
        CASE WHEN ROW_NUMBER() OVER(PARTITION BY SalesOrderID ORDER BY SalesOrderID ASC) = 1 THEN CONVERT(varchar(10),SalesOrderID) ELSE '-' END  AS SalesOrderID,
        R.ProductID,
        R.OrderQty,
        R.UnitPrice,
        R.LineTotal

    FROM AdventureWorks2014.Sales.SalesOrderDetail AS R
like image 197
Chetan Sanghani Avatar answered Feb 02 '26 15:02

Chetan Sanghani