Solved

Sql Query to pull data by dates

Posted on 2010-08-27
14
255 Views
Last Modified: 2012-06-21
I am creating an application to pull delinquent data, based on dates in 30,60,90, 120 day increments.  I wrote a sql query which seems to compile correctly, but errors out when I create it as a view.  I get the following error:

Incorrect syntax near the keyword 'WHERE'.  how is my query incorrect?  

Thanks in advance.  
select a.Title, a.PrdNumber, 
(select Datediff(day, a.Schedule_Date, getDate()) 
WHERE Datediff(day, a.Schedule_Date, getDate()) BETWEEN 1 and 30) 
AS '30 Days',
(select Datediff(day, a.Schedule_Date, getDate()) 
WHERE Datediff(day, a.Schedule_Date, getDate()) 
BETWEEN 31 and 61) as '60 Days',
(select Datediff(day, a.Schedule_Date, getDate()) 
WHERE Datediff(day, a.Schedule_Date, getDate()) 
BETWEEN 62 and 91) as '90 Days',
(select Datediff(day, a.Schedule_Date, getDate()) 
WHERE Datediff(day, a.Schedule_Date, getDate()) 
BETWEEN 92 and 121) as '120 Days'
FROM tmp_Logs_RapidCue r
INNER JOIN tmp_Logs_VCIAsrun a 
ON r.Media = a.PrdNumber
--WHERE a.PrdNumber <> ''
Order by a.Title ASC

Open in new window

0
Comment
Question by:ayoZen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +3
14 Comments
 
LVL 11

Expert Comment

by:Rajesh Dalmia
ID: 33544962
what's that -- near WHERE clause?
0
 
LVL 16

Expert Comment

by:vdr1620
ID: 33545023
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 33545024
When I tested your query, it seems to be working!
Can you check this attached query, that is just realligned your query
Raj
select a.Title, a.PrdNumber, 
(select Datediff(day, a.Schedule_Date, getDate()) WHERE Datediff(day, a.Schedule_Date, getDate()) BETWEEN 1 and 30) AS '30 Days',
(select Datediff(day, a.Schedule_Date, getDate()) WHERE Datediff(day, a.Schedule_Date, getDate()) BETWEEN 31 and 61) as '60 Days',
(select Datediff(day, a.Schedule_Date, getDate()) WHERE Datediff(day, a.Schedule_Date, getDate()) BETWEEN 62 and 91) as '90 Days',
(select Datediff(day, a.Schedule_Date, getDate()) WHERE Datediff(day, a.Schedule_Date, getDate()) BETWEEN 92 and 121) as '120 Days'
FROM tmp_Logs_RapidCue r
INNER JOIN tmp_Logs_VCIAsrun a 
ON r.Media = a.PrdNumber
--WHERE a.PrdNumber <> ''
Order by a.Title ASC

Open in new window

0
How To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

 
LVL 8

Expert Comment

by:Mohit Vijay
ID: 33545033
You missed the from cluase.
0
 
LVL 8

Expert Comment

by:Mohit Vijay
ID: 33545052
you are facing problem in such type of inner queries

(select Datediff(day, a.Schedule_Date, getDate())
WHERE Datediff(day, a.Schedule_Date, getDate()) BETWEEN 1 and 30)

0
 
LVL 16

Expert Comment

by:vdr1620
ID: 33545086
just read your question again

Oops.. You get that error because you are trying to create a view with Order by Clause...

***Views cannot be created be created with Order by clause unless its being with TOP in select
0
 
LVL 16

Expert Comment

by:vdr1620
ID: 33545130
check this article on views

http://www.devx.com/dbzone/Article/8048/1954
0
 
LVL 16

Expert Comment

by:vdr1620
ID: 33545149
below query should work as a view..
select TOP 100 PERCENT WITH TIES a.Title, a.PrdNumber, 
(select Datediff(day, a.Schedule_Date, getDate()) WHERE Datediff(day, a.Schedule_Date, getDate()) BETWEEN 1 and 30) AS '30 Days',
(select Datediff(day, a.Schedule_Date, getDate()) WHERE Datediff(day, a.Schedule_Date, getDate()) BETWEEN 31 and 61) as '60 Days',
(select Datediff(day, a.Schedule_Date, getDate()) WHERE Datediff(day, a.Schedule_Date, getDate()) BETWEEN 62 and 91) as '90 Days',
(select Datediff(day, a.Schedule_Date, getDate()) WHERE Datediff(day, a.Schedule_Date, getDate()) BETWEEN 92 and 121) as '120 Days'
FROM tmp_Logs_RapidCue r
INNER JOIN tmp_Logs_VCIAsrun a 
ON r.Media = a.PrdNumber
--WHERE a.PrdNumber <> ''
Order by a.Title ASC

Open in new window

0
 

Author Comment

by:ayoZen
ID: 33545326
Thanks so much for your responses.

In my view,  I removed the order by clause and still received the same error.  
vdr1620,  I tried your suggestion with the ties and the error persists.

vjsoft, you mentioned that the problem lies with my inner queries.  I cleaned up my data and I can't see the problem.

please advise.  
select a.Title, a.PrdNumber, 
(select Datediff(day, a.Schedule_Date, getDate()) 
WHERE Datediff(day, a.Schedule_Date, getDate()) BETWEEN 1 and 30) 
AS '30 Days',
(select Datediff(day, a.Schedule_Date, getDate()) 
WHERE Datediff(day, a.Schedule_Date, getDate()) 
BETWEEN 31 and 61) as '60 Days',
(select Datediff(day, a.Schedule_Date, getDate()) 
WHERE Datediff(day, a.Schedule_Date, getDate()) 
BETWEEN 62 and 91) as '90 Days',
(select Datediff(day, a.Schedule_Date, getDate()) 
WHERE Datediff(day, a.Schedule_Date, getDate()) 
BETWEEN 92 and 121) as '120 Days'
FROM tmp_Logs_RapidCue r
INNER JOIN tmp_Logs_VCIAsrun a 
ON r.Media = a.PrdNumber

Open in new window

0
 
LVL 16

Accepted Solution

by:
vdr1620 earned 300 total points
ID: 33545613
i don't see any error..it works perfectly.. Do you mean that you don't see expected results

Can you post your error message
CREATE TABLE #tmp_Logs_RapidCue
(
media Int
)

CREATE  TABLE #tmp_Logs_VCIAsrun
(
Title Varchar(10),
Schedule_Date Date,
PrdNumber Int
)

INSERT INTO #tmp_Logs_VCIAsrun VALUES ('A','08/20/2010',1),('B','06/20/2010',2)
INSERT INTO #tmp_Logs_RapidCue VALUES (1),(2)

select a.Title, a.PrdNumber, 
(select Datediff(day, a.Schedule_Date, getDate()) 
WHERE Datediff(day, a.Schedule_Date, getDate()) BETWEEN 1 and 30) 
AS '30 Days',
(select Datediff(day, a.Schedule_Date, getDate()) 
WHERE Datediff(day, a.Schedule_Date, getDate()) 
BETWEEN 31 and 61) as '60 Days',
(select Datediff(day, a.Schedule_Date, getDate()) 
WHERE Datediff(day, a.Schedule_Date, getDate()) 
BETWEEN 62 and 91) as '90 Days',
(select Datediff(day, a.Schedule_Date, getDate()) 
WHERE Datediff(day, a.Schedule_Date, getDate()) 
BETWEEN 92 and 121) as '120 Days'
FROM #tmp_Logs_RapidCue r
INNER JOIN #tmp_Logs_VCIAsrun a 
ON r.Media = a.PrdNumber

DROP TABLE  #tmp_Logs_VCIAsrun
DROP TABLE #tmp_Logs_RapidCue

Open in new window

0
 
LVL 60

Assisted Solution

by:Kevin Cross
Kevin Cross earned 200 total points
ID: 33545763
I agree. It executes for me also, but note you really don't need to be using subquery for this.
create view logsView as
select 
   a.Title, a.PrdNumber
 , case 
     when Datediff(day, a.Schedule_Date, getDate()) between 1 and 30
     then Datediff(day, a.Schedule_Date, getDate())
   end AS '30 Days'
 , case 
     when Datediff(day, a.Schedule_Date, getDate()) between 31 and 60
     then Datediff(day, a.Schedule_Date, getDate())
   end AS '60 Days'
 , case 
     when Datediff(day, a.Schedule_Date, getDate()) between 61 and 90
     then Datediff(day, a.Schedule_Date, getDate())
   end AS '90 Days'
 , case 
     when Datediff(day, a.Schedule_Date, getDate()) between 91 and 120
     then Datediff(day, a.Schedule_Date, getDate())
   end AS '120 Days'
FROM tmp_Logs_RapidCue r
INNER JOIN tmp_Logs_VCIAsrun a 
ON r.Media = a.PrdNumber
;

Open in new window

0
 

Author Comment

by:ayoZen
ID: 33546222
I mispoke.  I retested.  Thank you so much!!!!!!!!!!!
0
 

Author Closing Comment

by:ayoZen
ID: 33546229
Thank you so much!!!!!!!  The solution was perfect.
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 33546271
That was me too commented initially  :)

Raj
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question