| Author |
Topic |
|
ben_53
Yak Posting Veteran
67 Posts |
Posted - 2011-07-28 : 15:25:19
|
| Hi again, now I want to assign the sequence for a table with 4 columns.col1 is identity, col4 is named seq.now, col2 have only 4 values,Apple,Banana,Strawberry & Blueberry.col3 can have 3 vales,LA, SF & NY.Business logic: sequence should be first according to col1Banana>Blueberry>Apple>Strawberrythen according to col2 depending on col1for banana col2 order = NY>LA>SFfor Blueberry, order = LA>SF>NYand so on...So, It should look like:Banana NY 0001Banana LA 0002Banana SF 0003Blueberry LA 0004Blueberry SF 0005Blueberry NY 0006 and so on...Kindly write a general query for this.mine is not working accordingly.Thanks |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2011-07-28 : 15:38:48
|
Can you post what you've already tried? Can you also clarify:quote: then according to col2 depending on col1for banana col2 order = NY>LA>SFfor Blueberry, order = LA>SF>NYand so on...
Because while that may be "business logic", it's not real logic, or at least not readily apparent. |
 |
|
|
ben_53
Yak Posting Veteran
67 Posts |
Posted - 2011-07-28 : 15:56:41
|
| Result set will clarify this:id col1 col2 seq102 Banana NY 0001147 Banana LA 0002145 Banana SF 0003234 Blueberry LA 0004784 Blueberry SF 0005547 Blueberry NY 0006874 Apple NY 0007258 Apple SF 0008324 Apple LA 0009154 Strawberry NY 0010671 Strawberry LA 0011361 Strawberry SF 0012now first, sequence by col1 order: Banana > blueberry > Apple > StrawberryThen sequence by col2, order for banana : NY > LA > SFfor blueberry: LA > SF > NYfor Apple: NY > SF > LAfor Strawberry: NY > LA > SFplease see the resultset again, I hope its crystal clear now. I cant come up with anything. sorry |
 |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2011-07-28 : 16:08:28
|
| If you only have 12 rows, couldn't you just manually enter the sequence number? The only solution I can think of is about as long as the INSERT statements to generate the data explicitly. |
 |
|
|
ben_53
Yak Posting Veteran
67 Posts |
Posted - 2011-07-28 : 16:15:31
|
| no, I have thousands of rows.Lets put it this way for banana > NY say I have 100 rowsthen banana > LA (50 rows)should start from 101then banana > SF should start from 151.sat I want to sort col1>col2 in ascending order of ID.478 banana NY 0001479 banana NY 0002480 banana NY 0003so on...Does that makes sense to you ? |
 |
|
|
namman
Constraint Violating Yak Guru
285 Posts |
Posted - 2011-07-28 : 16:47:16
|
| Using case, I think you can solve this problem ...If you need help in detail, let me know |
 |
|
|
ben_53
Yak Posting Veteran
67 Posts |
Posted - 2011-07-28 : 16:56:58
|
| Namman :I am using case as follows:Update my_tableset seq = newseqfrom ( select seq,newseq = case col1when 'Banana' then case = col2 when 'NY' then ......It is not working at all. moreover I have not defined the order for col1 or col2. Please help. Im stuck. |
 |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2011-07-28 : 17:00:41
|
| select *, ROW_NUMBER() over (order bycase col1 when 'Banana' then case col2 when 'NY' then 1when 'LA' then 2 else 3 endwhen 'Blueberry' then case col2when 'LA' then 4when 'SF' then 5 else 6 endwhen 'Apple' then case col2when 'NY' then 7when 'SF' then 8 else 9 endwhen 'Strawberry' then case col2 when 'NY' then 10when 'LA' then 11 else 12 endend, id) as Seqfrom MyTable |
 |
|
|
ben_53
Yak Posting Veteran
67 Posts |
Posted - 2011-07-28 : 17:07:46
|
| namman:I have thousands of rows.Lets put it this wayfor banana > NY say I have 100 rowsthen banana > LA (50 rows)should start from 101then banana > SF should start from 151.sat I want to sort col1>col2 in ascending order of ID.478 banana NY 0001479 banana NY 0002480 banana NY 0003so on...Does that makes sense to you ? |
 |
|
|
namman
Constraint Violating Yak Guru
285 Posts |
Posted - 2011-07-29 : 11:38:15
|
| Do you have a solution, ben_53 ?I think the script provided by robvolk is working fine. |
 |
|
|
Ghanta
Yak Posting Veteran
96 Posts |
Posted - 2011-07-29 : 15:05:41
|
| WITH my_cte AS (SELECT fruit, location, CASE WHEN fruit = 'Banana' THEN 4 WHEN fruit = 'Blueberry' THEN 3 WHEN fruit = 'Apple' THEN 2 WHEN fruit = 'Strawberry' THEN 1 ELSE 0 END fruitscore, CASE WHEN fruit = 'Banana' AND location = 'NY' THEN 3 WHEN fruit = 'Banana' AND location = 'LA' THEN 2 WHEN fruit = 'Banana' AND location = 'SF' THEN 1 WHEN fruit = 'Blueberry' AND location = 'LA' THEN 3 WHEN fruit = 'Blueberry' AND location = 'SF' THEN 2 WHEN fruit = 'Blueberry' AND location = 'NY' THEN 1 WHEN fruit = 'Apple' AND location = 'SF' THEN 3 WHEN fruit = 'Apple' AND location = 'NY' THEN 2 WHEN fruit = 'Apple' AND location = 'LA' THEN 1 WHEN fruit = 'Strawberry' AND location = 'NY' THEN 3 WHEN fruit = 'Strawberry' AND location = 'LA' THEN 2 WHEN fruit = 'Strawberry' AND location = 'SF' THEN 1 ELSE 0 END locscore FROM tblseq) SELECT fruit, location, fruitscore + locscore AS score, Row_number() OVER ( ORDER BY fruitscore + locscore DESC) seq FROM my_cte ORDER BY score DESC |
 |
|
|
ben_53
Yak Posting Veteran
67 Posts |
Posted - 2011-07-29 : 16:05:35
|
| @Ghanta:Imagine you have 100000000 rows.I was looking for automated solution.Well I found a different route to solve the problem.I used:update my_tableset seq = newseqfrom (select seq,newseq = case col1 when 'Banana' then + row_number () over(PARTITION by col2 order by col1 )and so on for all casesENDIt assigns the sequence automatically but my business logic is far beyond this.Thanks Anyways. |
 |
|
|
Ghanta
Yak Posting Veteran
96 Posts |
Posted - 2011-07-29 : 16:28:04
|
I thought you have millions of rows but you only have 4 fruits and 3 locations...quote: Originally posted by ben_53 @Ghanta:Imagine you have 100000000 rows.I was looking for automated solution.Well I found a different route to solve the problem.I used:update my_tableset seq = newseqfrom (select seq,newseq = case col1 when 'Banana' then + row_number () over(PARTITION by col2 order by col1 )and so on for all casesENDIt assigns the sequence automatically but my business logic is far beyond this.Thanks Anyways.
|
 |
|
|
ben_53
Yak Posting Veteran
67 Posts |
Posted - 2011-07-29 : 16:35:43
|
| @ ghanta:I have a column named ID that I have mentioned earlier. That can actually make million rows with 4 fruits and 3 locations.Thanks |
 |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2011-07-29 : 19:49:14
|
| I guess you didn't try my solution then, it worked fine for the sample data you provided. |
 |
|
|
|