Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 How to perform group by clause in UPDATE query?

Author  Topic 

gk03
Starting Member

3 Posts

Posted - 2010-08-20 : 09:56:11
Hi All,

We have table something like this:-

CREATE TABLE TEMP
(
ID INT,
CH VARCHAR(30)
)

Records as below:-

INSERT INTO TEMP VALUES (1, 'Principal')
INSERT INTO TEMP VALUES (1, 'Teacher')
INSERT INTO TEMP VALUES (1, 'Director')
INSERT INTO TEMP VALUES (1, 'Student')
INSERT INTO TEMP VALUES (1, 'Something')
INSERT INTO TEMP VALUES (1, 'Pieon')
INSERT INTO TEMP VALUES (2, 'Student')
INSERT INTO TEMP VALUES (2, 'Something')
INSERT INTO TEMP VALUES (2, 'Pieon')
INSERT INTO TEMP VALUES (3, 'Teacher')
INSERT INTO TEMP VALUES (3, 'Pieon')
INSERT INTO TEMP VALUES (3, 'blablabla')
INSERT INTO TEMP VALUES (4, 'blablabla')

We want the update the table to make it with unique ID values and CH values based on our business logic (if id is "Director" than use it, else if "Principal" than use it, else if "Teacher" than use it, else if "Student" than use it, else if "Pieon" than use it, else what ever is left out) -- we need to check above if-else-if condition for each ID values

Can anybody tell me how to write update query so that with my if else statement so that I can achieve my expected output when I do "SELECT * FROM TEMP"?

Thanks

gk03

My expected o/p:-
ID CH
1 Director
2 Student
3 Teacher
4 blablabla

gk03

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-08-20 : 11:07:40
Here's one way..using another table that stores the Rank.
--Sample Data
CREATE TABLE #TEMP
(
ID INT,
CH VARCHAR(30)
)

INSERT INTO #TEMP VALUES (1, 'Principal')
INSERT INTO #TEMP VALUES (1, 'Teacher')
INSERT INTO #TEMP VALUES (1, 'Director')
INSERT INTO #TEMP VALUES (1, 'Student')
INSERT INTO #TEMP VALUES (1, 'Something')
INSERT INTO #TEMP VALUES (1, 'Pieon')
INSERT INTO #TEMP VALUES (2, 'Student')
INSERT INTO #TEMP VALUES (2, 'Something')
INSERT INTO #TEMP VALUES (2, 'Pieon')
INSERT INTO #TEMP VALUES (3, 'Teacher')
INSERT INTO #TEMP VALUES (3, 'Pieon')
INSERT INTO #TEMP VALUES (3, 'blablabla')
INSERT INTO #TEMP VALUES (4, 'blablabla')

--Rank Table
CREATE TABLE #RANK
(
[RANK] INT,
[CH] VARCHAR(30)
)
INSERT INTO #RANK
SELECT 1,'Director'
UNION ALL SELECT 2,'Principal'
UNION ALL SELECT 3,'Teacher'
UNION ALL SELECT 4,'Student'
UNION ALL SELECT 5,'Pieon'

--Query
SELECT T.ID,T.CH
FROM
(
SELECT ROW_NUMBER() OVER (PARTITION BY A.ID ORDER BY COALESCE(B.[RANK],99)) AS SEQ,
A.ID,A.CH,B.[RANK]
FROM #TEMP A
LEFT JOIN #RANK B ON A.[CH] = B.[CH]
) T
WHERE T.SEQ = 1

--Result
ID CH
----------- ------------------------------
1 Director
2 Student
3 Teacher
4 blablabla
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-08-20 : 11:19:21
do you have a table where you've set preference order as director,Student,etc

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -