Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 275
  • Last Modified:

SQL Select on a Select with criteria

Hi

I have a number of queries that I have to select a date range on. The query below\
is SELECT [Transactions].* FROM [Transactions]) is one of these. What is wrong with my syntax below?


Select * From (SELECT [Transactions].* FROM [Transactions]) Where [Transaction Date] >'01 February 2013' AND [Transaction Date] <'26 May 2013'
0
Murray Brown
Asked:
Murray Brown
  • 3
  • 3
  • 2
  • +1
1 Solution
 
rpkhareCommented:
Please try below as inner query:
SELECT * FROM [Transactions]
WHERE [TransactionDate] BETWEEN
'2013-02-01' AND '2013-05-26'

Open in new window

0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Hi. Thanks but that doesn't help. I am trying to do a Select on a Select with criteria
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Worked it out myself

Select * From (SELECT [Transactions].* FROM [Transactions]) As oAlias Where [Transaction Date] >'01 February 2013' AND [Transaction Date] <'26 May 2013'
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for murbro's comment #a39197479

for the following reason:

worked it out myself
0
 
rpkhareCommented:
A valid and more better way was suggested for the inner query.
0
 
rpkhareCommented:
SELECT * FROM
(SELECT * FROM [Transactions]
WHERE [TransactionDate] BETWEEN
'2013-02-01' AND '2013-05-26') T

Open in new window

0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
>I am trying to do a Select on a Select with criteria
Just for kicks and giggles, can you give us an English-only description of what you mean by 'Select on a Select'?
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Reason I ask is because if we're talking only one table, and you're not doing anything funky such as filtering on sum values, rank orders, pivot/unpivot, or time series, then a simple select should meet most needs, as was in the first comment.
0
 
Dale FyeCommented:
I would agree with Jim, you do not need the sub query in this instance.

And I would recommend using a syntax other than BETWEEN when working with dates.

Where datefield  >= '2013-02-01' AND datefield < '2013-05-27'

This will ensure that if your datefield contains a time component you include all records that are attributed to 2013-05-26, not just those where the date value is '2013-05-26 00:00:00'
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Build your data science skills into a career

Are you ready to take your data science career to the next step, or break into data science? With Springboard’s Data Science Career Track, you’ll master data science topics, have personalized career guidance, weekly calls with a data science expert, and a job guarantee.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now