Change only one cell value on select statement i have these db tables:
 Table Combo:
id int identity(1,1)
name varchar
duration int
type int

table related
sourcecombo int
destinationcombo int
finalCombo int

sales
id int identity(1,1)
username varchar
comboid int
saledate datetime 

Now, if a combo has a duration=0, it means that the client is upgrading or downgrading its product. In the interface, I need to display the destinactioncombo value, with the condition that the sales table has one record comboid, where duration=0.But I need to display the value only at the cell that has as a primary value sourceCombo. The query I am trying to use is the following:
    if exists 
(select comboid 
from sales 
inner join combo on sales.comboid=combo.id
where combo.duration=0 and  username=@name)
select sales.username,saledate, 
(select destinationcombo from related
where FinalCombo = 
(select comboid
from sales where 
saledate=(select max(saledate) from 
Sales where username=@name)))
as comboid from Sales where username=@name
else select  '0'

where @name is a value taken by input from the user. The result i get until now, is the following:

The problem is, that it does capture the destinationCombo as it should, but it displays it at every single row when it has to display only at the last row.
UPDATE 1
So, the steps I should follow to achieve the result are like follows:

Check the sales table if it has any combo with duration=0 for
the selected user
If there is a result, then take the most recent combo (with
duration&lt;&gt;0 and with most recent saledate)
After finding that combo, than calculate from relation the
destinationcombo, where FinalCombo is the combo found on step 1) and
sourcecombo is the combo found on step 2).

Let's hope this clears the question a little more
The following query matches your description from UPDATE 1 (with inline comments showing how it matches) and also yields the correct result for your SQL Fiddle (there is no longer an unwanted row for for the 2015-10-21 sale).

-- (1) Check the sales table if it has any combo with duration=0 for the selected user
DECLARE @finalComboId INT = (
    SELECT TOP 1 s.comboid
    FROM sales s
    JOIN combo c
        ON s.comboid = c.id
        AND c.duration = 0
    WHERE s.username = 'user1'
)

IF (@finalComboId IS NOT NULL)
BEGIN
    SELECT s.username, s.saledate, r.destinationcombo as comboid
    FROM (
        -- (2) If there is a result, then take the most recent combo
        -- (with duration &lt;&gt; 0 and with most recent saledate)
        SELECT TOP 1 c.id AS recent_comboid
        FROM combo c
        JOIN sales sc
            ON sc.comboid = c.id
            AND sc.username = 'user1'
        WHERE c.duration &lt;&gt; 0
        ORDER BY sc.saledate DESC
    ) rc
    -- Find all sales rows from that most recent combo
    -- (The SQL Fiddle has just one, but more generally I assume there could be multiple)
    JOIN sales s
        ON s.comboid = rc.recent_comboid
    -- (3) After finding that combo, than calculate from relation the destinationcombo...
        -- NOTE: I intrepret this as finding all sales rows from that most recent combo
    JOIN related r
        ON r.finalCombo = 3 --...where FinalCombo is the combo found on step 1)
        AND r.sourceCombo = s.comboid --...and sourcecombo is the combo found on step 2).
END
ELSE
    SELECT * FROM sales WHERE username='user1'
GO


However, there are a few things that remain confusing and leave me wondering if I am interpreting the problem correctly:


The sourcecombo field is not used in your example query, but the problem statement indicates that it is important when matching to relation.
Why does the ELSE clause select all rows from sales when the the main body is selecting only the most recent combo?
What if there is more than one combo with duration = 0? There is no guidance in the problem statement about which row should be picked.