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
 Other Forums
 Other Topics
 using complete sql statement in in() function
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

poncho4u
Starting Member

Ghana
3 Posts

Posted - 11/26/2013 :  11:39:56  Show Profile  Reply with Quote
Hello everyone,
Is it possible to use a complete sql statement within an in() function? What is wrong with the query below please:

Select cell, sdate, PTTCH from stats_60
where ne = 'BSC4' and sdate in (
SELECT SDATE FROM (
SELECT TO_CHAR(SDATE,'YYYY-MM-DD'), SDATE
, MAX(ROUND(ERLANG,2))over(partition by TO_CHAR(SDATE,'YYYY-MM-DD')) BHERL, ERLANG
FROM
(
SELECT A.ELEMENT, A.SDATE SDATE, TO_CHAR(A.SDATE,'HH24'), A.ERLANG
FROM STATS_LAYER_60 A
WHERE A.ELEMENT='BSC4'
AND TO_CHAR(A.SDATE,'HH24') BETWEEN '00' AND '24'
AND A.SDATE >= to_date('25/11/2013','DD/MM/YYYY')
AND A.SDATE < to_date('26/11/2013','DD/MM/YYYY')
)
) where BHERL=ERLANG
)

Thanks

edit: moved to proper forum

James K
Flowing Fount of Yak Knowledge

3323 Posts

Posted - 11/26/2013 :  12:55:03  Show Profile  Reply with Quote
quote:
Originally posted by poncho4u

Hello everyone,
Is it possible to use a complete sql statement within an in() function? What is wrong with the query below please:

Select cell, sdate, PTTCH from stats_60
where ne = 'BSC4' and sdate in (
SELECT SDATE FROM (
SELECT TO_CHAR(SDATE,'YYYY-MM-DD'), SDATE
, MAX(ROUND(ERLANG,2))over(partition by TO_CHAR(SDATE,'YYYY-MM-DD')) BHERL, ERLANG
FROM
(
SELECT A.ELEMENT, A.SDATE SDATE, TO_CHAR(A.SDATE,'HH24'), A.ERLANG
FROM STATS_LAYER_60 A
WHERE A.ELEMENT='BSC4'
AND TO_CHAR(A.SDATE,'HH24') BETWEEN '00' AND '24'
AND A.SDATE >= to_date('25/11/2013','DD/MM/YYYY')
AND A.SDATE < to_date('26/11/2013','DD/MM/YYYY')
)
) where BHERL=ERLANG
)

Thanks

While there may be more efficient ways to write the query, there is nothing syntactically wrong with having a complete sql statement within an IN construct.

However, are you using Microsoft SQL Server, or anther RDBMS? TO_CHAR is not a T-SQL function.
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