SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 combined conditions
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

allan8964
Posting Yak Master

249 Posts

Posted - 02/20/2014 :  13:55:51  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

USA
6062 Posts

Posted - 02/20/2014 :  14:01:00  Show Profile  Reply with Quote
one way:

where ([year]*10)+[qt] between 20134 and 20144

Be One with the Optimizer
TG
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3699 Posts

Posted - 02/20/2014 :  14:01:49  Show Profile  Reply with Quote
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
Go to Top of Page

allan8964
Posting Yak Master

249 Posts

Posted - 02/20/2014 :  15:52:14  Show Profile  Reply with Quote
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.

Edited by - allan8964 on 02/20/2014 15:53:32
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000