Connecting Tech Pros Worldwide Forums | Help | Site Map

Problem with query

Newbie
 
Join Date: Apr 2009
Posts: 14
#1: Apr 8 '09
Hi,

I need a query in SQL Sever which will select the records based on the date of creation.The query is,

List the opportunity name where the date of creation is before 3 weeks.

I do not know to create a SQL query for this. Pls do help me out with this.
Thanks in advance.
Sivakrupa Arulmani.

Uncle Dickie's Avatar
Member
 
Join Date: Nov 2008
Posts: 47
#2: Apr 9 '09

re: Problem with query


Assuming your table has a DateCreated (or equivalent name) column then it should be a simple WHERE statement:

Expand|Select|Wrap|Line Numbers
  1. SELECT *
  2. FROM table
  3. WHERE DateCreated < DATEADD(dd,-21,getdate())
If there is no DateCreated column you will probably need to add one and use a trigger to populate it when a record is added (or something similar).
Newbie
 
Join Date: Mar 2009
Posts: 7
#3: Apr 13 '09

re: Problem with query


Hi friends,
I dont know where to post this query....so I am posting it here. Hope you guys will help me. I am using SQL Server 2005. Everything seems to be alright there. But the following Left Outer Join Query does not give the desired result.

SELECT m.meter_addr, r.Date, r.Reading
FROM meter_table AS m LEFT OUTER JOIN
WebSite_Support.dbo.Readings AS r ON m.meter_addr = r.meter_ID

the table m contains 210 records
the table r contains 207 records

But the above query returns only 207 records. According to me it should have returned 210 records. Please Help. Thanx in advance.
Newbie
 
Join Date: Jul 2009
Posts: 9
#4: Jul 17 '09

re: Problem with query


YOu are using left outer join ,so you need to put your first (table from which you want to take data) on left side of join.

SELECT m.meter_addr, r.Date, r.Reading
FROM WebSite_Support.dbo.Readings AS r
LEFT OUTER JOIN
meter_table AS m
ON m.meter_addr = r.meter_ID
Hope this will help you.

-Pankaj Tambe
Newbie
 
Join Date: Jul 2009
Posts: 5
#5: Jul 21 '09

re: Problem with query


Dear Sujitkar

R u using where condition in your Query??? And that too on table r???

As your join Query seems fine.

Regards,
Reply