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 |
anjali5
Posting Yak Master
121 Posts |
Posted - 2015-03-20 : 17:31:58
|
Hello all,I have a table in which the columns has values like thiscol1Master Card6.000000987 PQ016.000000987 PQ026.000000987 PQ036.000000345 PQ036.000000345 PQ04Test Card2P00011-04-8765P00011-05-9876P00011-05-9876P00011-09-1234P00011-10-3333P00011-10-2222P00011-09-1111P00011-10-2345P00011-11-4532P00011-12-2345P00011-13-4532P00011-14-2345P00011-15-4532Visa Card In the above table, I need to write the query to get all the P00011-04-8765 that has a mid number higher than 10 so I only need P00011-11-4532,P00011-12-2345,P00011-13-4532,P00011-14-2345,P00011-15-4532. In these the mid number is 11, 12,13, 14 and 15Mid number is substring([col] , 8, 2), I also need all the text values like master card, test Card2 and Visa Card and I also need, the top most row of the repeated values so for e.g, I have 6.000000987 PQ01,6.000000987 PQ02, I only need the 6.000000987 PQ01 and 6.000000345 PQ03 and not 6.000000345 PQ02,6.000000987 PQ03 and 6.000000345 PQ04, the repeated values are the once that has same ending number before the space so 6.000000987 PQ01,6.000000987 PQ02 are repeated values and 6.000000345 PQ03, 6.000000345 PQ04 are repeated values. I only need the top row of the repeated values.The final table will be:Col1Master Card6.000000987 PQ016.000000345 PQ03Test Card2P00011-11-4532P00011-12-2345P00011-13-4532P00011-14-2345P00011-15-4532 any help will be appreciated. |
|
jleitao
Posting Yak Master
100 Posts |
Posted - 2015-03-20 : 18:31:36
|
not sure if everything "result" with your data. However try this:PS - i have created 3 distinct query. One for each "scenario" you need.-- #1 --select col1FROM TABwhere SUBSTRING(col1, 7,4) like '-%-'and SUBSTRING(col1, 8,2) > 10UNION-- #2 --selectcol1from TABwhere ISNUMERIC( substring(col1,3,1)) = 0 -- check if the 3rd char is not numeric. confirm if this is a valid condition to your dataUNION-- #3 --SELECTCOL1_AUX1 + ' ' + MIN(COL1_AUX2)FROM( SELECT col1, LEFT(col1, CHARINDEX(' ', COL1, 1) -1) AS COL1_AUX1, RIGHT(col1, LEN(col1) - CHARINDEX(' ', col1, 1)) COL1_AUX2 FROM TAB WHERE RIGHT(col1, 4) LIKE 'PQ%' -- confirm if this is a valid condition to your data)ZGROUP BY COL1_AUX1having COUNT(*) >1------------------------PS - Sorry my bad english |
|
|
anjali5
Posting Yak Master
121 Posts |
Posted - 2015-03-23 : 16:59:08
|
quote: Originally posted by jleitao not sure if everything "result" with your data. However try this:PS - i have created 3 distinct query. One for each "scenario" you need.-- #1 --select col1FROM TABwhere SUBSTRING(col1, 7,4) like '-%-'and SUBSTRING(col1, 8,2) > 10UNION-- #2 --selectcol1from TABwhere ISNUMERIC( substring(col1,3,1)) = 0 -- check if the 3rd char is not numeric. confirm if this is a valid condition to your dataUNION-- #3 --SELECTCOL1_AUX1 + ' ' + MIN(COL1_AUX2)FROM( SELECT col1, LEFT(col1, CHARINDEX(' ', COL1, 1) -1) AS COL1_AUX1, RIGHT(col1, LEN(col1) - CHARINDEX(' ', col1, 1)) COL1_AUX2 FROM TAB WHERE RIGHT(col1, 4) LIKE 'PQ%' -- confirm if this is a valid condition to your data)ZGROUP BY COL1_AUX1having COUNT(*) >1------------------------PS - Sorry my bad english
Thank You. |
|
|
|
|
|
|
|