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 |
allan8964
Posting Yak Master
249 Posts |
Posted - 2014-02-20 : 13:55:51
|
Hi there,I have a table with column of Year and Quarter like this:Year | Qt | Score ...2013 | 3 | 2132013 | 4 | 1432014 | 1 | 3422014 | 2 ...Now I need select some rows between Quarter 4 in previous year and current Quarter and only 4 Quarters allowed totally. How can I do that?Thanks in advance. |
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2014-02-20 : 14:01:00
|
one way:where ([year]*10)+[qt] between 20134 and 20144Be One with the OptimizerTG |
|
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2014-02-20 : 14:01:49
|
[code]SELECT TOP ( 4 ) Year , Qt , ScoreFROM YourTableWHERE Year <= '2013' AND Qt <= 3 -- if you want a starting pointORDER BY Year DESC, Qt DESC [/code] |
|
|
allan8964
Posting Yak Master
249 Posts |
Posted - 2014-02-20 : 15:52:14
|
Thanks gents for reply. TG's idea is brilliant now I put both together as following:Select top 4 YR, QT, ScoreFrom myTablewhere (YR * 10 + QT) between ((YEAR(GetDate()) - 1 ) * 10 + QT) and (YEAR(GetDate()) ) * 10 + QTOrder by YR desc, QT descit works perfect!thank you again. |
|
|
|
|
|