Select Statement

Experts,

Here is the SQL I am running and no problems with the SQL.

select       T1.Column1,T2.Column3,count(T1.Column6) as TestColumn1
from       Table T1
inner join Tablle2 T2
on T1.Column1 = T2.Column1
where       T1.Column1 > 'myvalue'
group by  T1.Column1,T2.Column3

union
select       T1.Column1,T3.Column3,count(T1.Column6) as TestColumn1
from       Table T1
inner join TempTablle T3
on T1.Column1 = T3.Column1
where       T3.Column3 > 'myvalue'
group by  T1.Column1,T3.Column3


I need to add one more column to my select and the output or the result of the newcolumn is like following.

C1 C2  C3   NewColumn
A   100     1          1
B    200     0          2
C   500     1           3
D   720     1           4


if my select gives 2 rows, newcolumns values should be 1 and 2
if my select gives 10 rows, newcolumns values should be 1 to 10

Thanks in advance and let me know if the question is not clear.
Tpaul_10Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cyberkiwiCommented:
Wrap it in a subquery, then add the ROW_NUMBER counter over it.
You need to order by something.  I put "order by (select 1)" twice as a placeholder.

You should replace both with something sensible, such as "order by Column1"

select Column1,Column3,TestColumn1,Column4 = Row_Number() over (order by (select 1))
FROM
(
select       T1.Column1,T2.Column3,count(T1.Column6) as TestColumn1
from       TableX T1
inner join Tablle2 T2
on T1.Column1 = T2.Column1
where       T1.Column1 > 'myvalue'
group by  T1.Column1,T2.Column3
union all
select       T1.Column1,T3.Column3,count(T1.Column6) as TestColumn1
from       TableX T1
inner join TempTablle T3
on T1.Column1 = T3.Column1
where       T3.Column3 > 'myvalue'
group by  T1.Column1,T3.Column3
) X
order by (select 1)

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Anthony PerkinsCommented:
>>then add the ROW_NUMBER counter over it.<<
Let's hope, they are at least using SQL Server 2005 (if not 2008).
Alpesh PatelAssistant ConsultantCommented:
select Columns 1...4, Rownumber()(order by columns1)
from (your query)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.