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
 combined conditions

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 | 213
2013 | 4 | 143
2014 | 1 | 342
2014 | 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 20144

Be One with the Optimizer
TG
Go to Top of Page

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2014-02-20 : 14:01:49
[code]SELECT TOP ( 4 )
Year ,
Qt ,
Score
FROM YourTable
WHERE Year <= '2013' AND Qt <= 3 -- if you want a starting point
ORDER BY Year DESC, Qt DESC [/code]
Go to Top of Page

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, Score
From myTable
where (YR * 10 + QT) between ((YEAR(GetDate()) - 1 ) * 10 + QT) and (YEAR(GetDate()) ) * 10 + QT
Order by YR desc, QT desc

it works perfect!
thank you again.
Go to Top of Page
   

- Advertisement -