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
 General SQL Server Forums
 New to SQL Server Programming
 Query Help

Author  Topic 

aswindba1
Yak Posting Veteran

62 Posts

Posted - 2013-08-27 : 20:38:31
Hi

I have a requirement like below. I have to give the flavours to the application.We have thousands of records to update.

Please help me.


Actual table

APP|||COLOUR|||FLAVOUR

ap1|||GREEEN
ap1|||YELLOW
ap1|||YELLOW
ap1|||GREEEN


ap2|||YELLOw
ap2|||YELLOW
ap2|||YELLOW

ap3|||GREEEN
ap3|||YELLOW
ap3|||RED
ap3|||GREEEN

ap4|||GREEN
ap4|||GREEN
ap4|||GREEN

Output would look like below

APP|||COLOUR|||FLAVOUR

ap1|||GREEEN|||YELLOWA
ap1|||YELLOW|||YELLOWA
ap1|||YELLOW|||YELLOWA
ap1|||GREEEN|||YELLOWA

ap2|||YELLOW|||YELLOWB
ap2|||YELLOW|||YELLOWB
ap2|||YELLOW|||YELLOWB


ap3|||GREEEN|||RED
ap3|||YELLOW|||RED
ap3|||RED |||RED
ap3|||GREEEN|||RED

ap4|||GREEN|||GREENA
ap4|||GREEN|||GREENA
ap4|||GREEN|||GREENA

let me know if you need additional information

Thanks
Aswin


waterduck
Aged Yak Warrior

982 Posts

Posted - 2013-08-27 : 21:58:02
How do you define flavour?
when there was green and yellow(higher priority), you get yellowa;
when there was green, yellow, red(higher priority, you get red
when there was only green or yellow, you get back respective color with additional alpha (yellowb or greena)?
Go to Top of Page

aswindba1
Yak Posting Veteran

62 Posts

Posted - 2013-08-28 : 00:22:00
Hi
thanks for your prompt your prompt reply.
Yes I need the output as you explained.
Can you help me.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-08-28 : 01:17:37
something like

SELECT APP,COLOUR,FLAVOUR + CHAR(64 + ROW_NUMBER() OVER (PARTITION BY FLAVOUR ORDER BY APP)) AS Seq
FROM
(
SELECT APP,COLOUR,
CASE WHEN SUM(CASE WHEN COLOUR = 'RED' THEN 1 END) OVER (PARTITION BY APP) > 0 THEN 'RED'
WHEN SUM(CASE WHEN COLOUR = 'YELLOW' THEN 1 END) OVER (PARTITION BY APP) > 0 THEN 'YELLOW'
ELSE 'GREEN'
END AS FLAVOUR
FROM Table
)t


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2013-08-28 : 03:15:19
quote:
Originally posted by waterduck


;WITH TableA AS(
SELECT *, CASE WHEN COLOUR IN ('GREEEN', 'GREEN') THEN 3
WHEN COLOUR = 'RED' THEN 1
WHEN COLOUR = 'YELLOW' THEN 2 END Priority
FROM (VALUES('ap1','GREEEN'),
('ap1','YELLOW'),
('ap1','YELLOW'),
('ap1','GREEEN'),
('ap1','GREEEN'),
('ap2','YELLOw'),
('ap2','YELLOW'),
('ap2','YELLOW'),
('ap3','GREEEN'),
('ap3','YELLOW'),
('ap3','RED'),
('ap3','GREEEN'),
('ap4','GREEN'),
('ap4','GREEN'),
('ap4','GREEN')) AS TableA(APP, COLOUR)
)
SELECT *
FROM TableA TableA
CROSS APPLY
(SELECT TOP 1 COLOUR + CHAR(64 + DENSE_RANK() OVER (PARTITION BY Priority ORDER BY APP))
FROM TableA TableB
WHERE TableA.APP = TableB.APP
ORDER BY Priority)TableB(FLAVOUR)
ORDER BY 1,2


Go to Top of Page
   

- Advertisement -