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.
Author |
Topic |
aswindba1
Yak Posting Veteran
62 Posts |
Posted - 2013-08-27 : 20:38:31
|
HiI 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 tableAPP|||COLOUR|||FLAVOURap1|||GREEENap1|||YELLOWap1|||YELLOWap1|||GREEENap2|||YELLOwap2|||YELLOWap2|||YELLOWap3|||GREEENap3|||YELLOWap3|||RED ap3|||GREEENap4|||GREENap4|||GREENap4|||GREENOutput would look like belowAPP|||COLOUR|||FLAVOURap1|||GREEEN|||YELLOWAap1|||YELLOW|||YELLOWAap1|||YELLOW|||YELLOWAap1|||GREEEN|||YELLOWAap2|||YELLOW|||YELLOWBap2|||YELLOW|||YELLOWBap2|||YELLOW|||YELLOWBap3|||GREEEN|||REDap3|||YELLOW|||REDap3|||RED |||REDap3|||GREEEN|||REDap4|||GREEN|||GREENAap4|||GREEN|||GREENAap4|||GREEN|||GREENAlet me know if you need additional informationThanksAswin |
|
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 redwhen there was only green or yellow, you get back respective color with additional alpha (yellowb or greena)? |
 |
|
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. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-08-28 : 01:17:37
|
something likeSELECT APP,COLOUR,FLAVOUR + CHAR(64 + ROW_NUMBER() OVER (PARTITION BY FLAVOUR ORDER BY APP)) AS SeqFROM (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 FLAVOURFROM Table)t ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
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 TableACROSS APPLY(SELECT TOP 1 COLOUR + CHAR(64 + DENSE_RANK() OVER (PARTITION BY Priority ORDER BY APP))FROM TableA TableBWHERE TableA.APP = TableB.APPORDER BY Priority)TableB(FLAVOUR)ORDER BY 1,2
|
 |
|
|
|
|
|
|