Solved

SQL Compare Two Rows Same ID and Update?

Posted on 2014-01-27
3
357 Views
Last Modified: 2014-01-27
I'm at-a-loss trying to compare two table rows with the same ID.
I need to update the 'IsPriceChg' in the row where Price column data is greater.

I tried using  Select 'Top 1' ...

ID      [Date]                                           IsPriceChg      Harmonica
88      2012-07-29 00:00:00.000      NULL                1.00
88      2012-07-30 00:00:00.000      NULL                2.00

I have this much, but not working yet:


Update Toys
Set IsPriceChg = 1
where ListID = 88
and max([Harmonica]) > min([Harmonica])
0
Comment
Question by:WorknHardr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 200 total points
ID: 39813372
Update t
Set IsPriceChg = 1
FROM Toys t
INNER JOIN (
    SELECT ListID, MAX([Harmonica]) AS Max_Harmonica
    FROM Toys
    GROUP BY ListID
    HAVING MAX([Harmonica]) > MIN([Harmonica])
) AS t_max ON
    t_max.ListID = t.ListID AND
    t.Harmonica = t_max.Harmonica
0
 
LVL 16

Assisted Solution

by:Surendra Nath
Surendra Nath earned 200 total points
ID: 39813493
if you are using SQL 2005 or above you can use the below query

;with CTE  AS
(
 select row_number() OVER(partition by ID,ListID order by Harmonica desc) rn, * from Toys
)
UPDATE CTE
SET IsPriceChg = 1 
WHERE Rn= 1

Open in new window

0
 

Author Closing Comment

by:WorknHardr
ID: 39813511
Very nice, thx
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Format Date fields 11 65
Tempdb Contention - SQL SERVER 10 41
Remove () 10 42
Using a SUBQUERY for the set variable 10 30
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question