SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to get consecutive record number in two select statement


How to get consecutive record number in two select statement

Author
Message
visweswaran28
visweswaran28
SSC Rookie
SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)

Group: General Forum Members
Points: 37 Visits: 10
Hi,

select row_number() over(order by b.inv_no) as sl_no,b.fyear_code,b.inv_no,b.inv_date,b.service_nature,b.inv_total,c.bal_amount,b.client_code,b.amt_received from fn_receipt a, trn_invoice_history b, mst_expenditureinvoice c where a.client_id=b.client_code and a.client_name=b.client_name and a.location=b.client_location and a.receiptno=35 and b.fully_recived not in ('Y') and b.amt_balance>0 and (c.fyear_code=b.fyear_code and b.inv_no=c.inv_no)
union
select row_number() over(order by b.inv_no) as sl_no,b.fyear_code,b.inv_no,b.inv_date,b.service_nature,b.inv_total,c.bal_amount,b.client_code,b.amt_received from fn_receipt a, trn_invoice_history b, mst_invoice c where a.client_id=b.client_code and a.client_name=b.client_name and a.location=b.client_location and a.receiptno=35 and b.fully_recived not in ('Y') and b.amt_balance>0 and (c.fyear_code=b.fyear_code and b.inv_no=c.inv_no)
Above query will return set of values. Now I want to get consecutive record number. How can I achieve
LutzM
LutzM
SSC-Dedicated
SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)

Group: General Forum Members
Points: 38155 Visits: 13559
You could wrap your UNION statment in a subquery or CTE. This way you could save one sorting operation by moving the ROW_NUMBER() function to the outer SELECT.
At a first glance it seems like the UNION statment can be eliminated by using LEFT OUTER JOINS (btw: you should change your syntax to use JOIN instead of cross join and WHERE conditions...). To verify it I would need table definition and smoe sample data to test against.

;
WITH cte AS
(
SELECT 1 AS intermed,b.fyear_code,b.inv_no,b.inv_date,b.service_nature,b.inv_total,c.bal_amount,b.client_code,b.amt_received FROM fn_receipt a, trn_invoice_history b, mst_expenditureinvoice c WHERE a.client_id=b.client_code AND a.client_name=b.client_name AND a.location=b.client_location AND a.receiptno=35 AND b.fully_recived NOT IN ('Y') AND b.amt_balance>0 AND (c.fyear_code=b.fyear_code AND b.inv_no=c.inv_no)
UNION
SELECT 2 AS intermed,b.fyear_code,b.inv_no,b.inv_date,b.service_nature,b.inv_total,c.bal_amount,b.client_code,b.amt_received FROM fn_receipt a, trn_invoice_history b, mst_invoice c WHERE a.client_id=b.client_code AND a.client_name=b.client_name AND a.location=b.client_location AND a.receiptno=35 AND b.fully_recived NOT IN ('Y') AND b.amt_balance>0 AND (c.fyear_code=b.fyear_code AND b.inv_no=c.inv_no)
)
SELECT
ROW_NUMBER() OVER(ORDER BY intermed,b.inv_no) AS total_no,
ROW_NUMBER() OVER(partitioned BY intermed ORDER BY b.inv_no) AS sl_no,
fyear_code,
inv_no,
inv_date,
service_nature,
inv_total,
bal_amount,
client_code,
amt_received
FROM cte





Lutz
A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search