Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
Hi
I have a below query. When I parse the query, It did not parsed. Could you please tell me where is the issue?
declare @row_number int
declare @acct_no bigint
declare @channel_name nvarchar

SET @row_number=0

select
 c.Account_Id,
 c.Channel_Name,
 c.Last_Refresh_Time 
 from
(
select
@row_number=CASE WHEN @acct_no = b.Account_Id and @channel_name = b.Channel_Name 
THEN @row_number + 1
ELSE 1
END AS rnk,
@acct_no=  b.Account_Id as Account_Id,
@channel_name= b.Channel_Name as Channel_Name,
 b.Last_Refresh_Time
from
 (
 select
a.Account_Id,
a.Channel_Name,
a.Last_Refresh_Time 
from
 (
 select
 a.acct_id  as Account_Id,
dp.name as Channel_Name,
dc.last_call_made_for_access As Last_Refresh_Time
from
stg.delivered_cap dc 
inner join stg.ordered_product tp on
tp.ord_prod_id=dc.ord_prod_id
 inner join stg.acct_order_item ai on
 tp.ord_prod_id = ai.ord_prod_id
inner join stg.acct_order ao on
ai.order_id = ao.order_id
 inner join stg.account a on
ao.acct_id = a.acct_id
left join  stg.delivery_point dp   on
 dp.del_point_id = dc.del_point_id
left join  stg.account_status dcact   on
dcact.acct_stat_id=dc.acct_status_id
where
 a.bu_id = 49
and dc.last_call_made_for_access is not null
) a
 order by Last_Refresh_Time desc
 ) b
) c
where
c.rnk=1


What I have tried:

Hi
I have a below query. When I parse the query, It did not parsed. Could you please tell me where is the issue?
Posted
Updated 3-Mar-16 17:32pm
v2
Comments
OriginalGriff 3-Mar-16 10:02am    
What error do you get?
Where do you get it?
Member 10833473 3-Mar-16 10:04am    
Hi,
Thanks for reply
I got the error in second select somewhere..
OriginalGriff 3-Mar-16 10:11am    
And?
What's the error message?
What line it is reporting it on?
PeejayAdams 3-Mar-16 10:52am    
Your code's kind of unreadable - proper casing and indentation would make a huge difference as would meaningful table aliases. Without any context or even an error message, it's somewhat hard to say what's causing the error.
Aria Jafarian 3-Mar-16 10:57am    
Give us some data on your tables and their relationships.
My first question is why do you use so many nested select
statements? Common table Expressions (CTE) can help you on Readability.

Have a feeling that this wont fix your problem though BUT

On thing that stands out for me is the declaration of

SQL
declare @channel_name nvarchar


because if you are supplying a value greater than 1 character it will be truncated to a single char.
 
Share this answer
 
There appears to be a few issues with it, but one main one is that you cannot use a single select statement to both assign values to a variable as well as retrieve data. The line where you fill in @row_number and @acct_no, etc, you also just select b.Last_Refresh_Time. You can't do that.
 
Share this answer
 
Comments
Simon_Whale 3-Mar-16 12:10pm    
Nice spot I missed that one
SQL
Code written is not clear, I suggest you use temporary tables or With such as common table to write SQL statements
I hope your structure is clea



<pre lang="SQL">declare @row_number int
declare @acct_no bigint
declare @channel_name nvarchar
 
SET @row_number=0


WITH a AS
(select
 a.acct_id  as Account_Id,
dp.name as Channel_Name,
dc.last_call_made_for_access As Last_Refresh_Time
from
stg.delivered_cap dc 
inner join stg.ordered_product tp on
tp.ord_prod_id=dc.ord_prod_id
 inner join stg.acct_order_item ai on
 tp.ord_prod_id = ai.ord_prod_id
inner join stg.acct_order ao on
ai.order_id = ao.order_id
 inner join stg.account a on
ao.acct_id = a.acct_id
left join  stg.delivery_point dp   on
 dp.del_point_id = dc.del_point_id
left join  stg.account_status dcact   on
dcact.acct_stat_id=dc.acct_status_id
where
 a.bu_id = 49
and dc.last_call_made_for_access is not null
),
b AS
(
select
a.Account_Id,
a.Channel_Name,
a.Last_Refresh_Time 
FROM a order by Last_Refresh_Time desc
),
c AS
(
select
@row_number=CASE WHEN @acct_no = b.Account_Id and @channel_name = b.Channel_Name 
THEN @row_number + 1
ELSE 1
END AS rnk,
@acct_no=  b.Account_Id as Account_Id,
@channel_name= b.Channel_Name as Channel_Name,
 b.Last_Refresh_Time
FROM b
)
select
 c.Account_Id,
 c.Channel_Name,
 c.Last_Refresh_Time 
 FROM c where
c.rnk=1</pre>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900