Showing posts with label hii. Show all posts
Showing posts with label hii. Show all posts

Wednesday, March 21, 2012

Row number in a select statement

Hi
I am looking for a solution to get an incremental row number along my other
select arguments after joining more than one tables.
Thanks in advance.
Ashokuse pubs
GO
--SELECT * FROM jobs
Select job_desc, (Select Count(*) + 1 FROM jobs B
WHERE B.job_desc < A.job_desc) AS RecNo
FROM jobs A
ORDER By job_desc
Roji. P. Thomas
Net Asset Management
https://www.netassetmanagement.com
"Ashok" <akumar@.buildfolio.com> wrote in message
news:uweREUBFFHA.2156@.TK2MSFTNGP10.phx.gbl...
> Hi
> I am looking for a solution to get an incremental row number along my
> other
> select arguments after joining more than one tables.
> Thanks in advance.
> Ashok
>
>|||Why can't you do it client-side? That's probably much more efficient
than attempting it in SQL.
If you really must, the following is one example of a SQL-based method
(from Pubs). You've said that more than one table is involved but it's
hard to give a full answer for that more complex scenario without more
info - like the DDL for the tables involved and some sample data to
work with.
SELECT au_id, au_lname, au_fname,
(SELECT COUNT(*)
FROM Authors
WHERE au_id <= A.au_id) AS row_num
FROM Authors AS A
David Portas
SQL Server MVP
--

Tuesday, March 20, 2012

row info wanted in column (like cube)

Hi

I have a table called tblsample, where i have information stored row wise. Ther four quarter information is stored for many years. I want those information column wise for a given year.

say

select col1, col2 from tblsample where rqtr=1 and ryear = 2000
select col1, col2 from tblsample where rqtr=2 and ryear = 2000
select col1, col2 from tblsample where rqtr=3 and ryear = 2000
select col1, col2 from tblsample where rqtr=4 and ryear = 2000

i want information like

for the Year 2000

1 qtr 2 qtr 3 qtr 4 qtr

How to acheive this in MSSQL 2000there are several ways select col1, sum(s1) as q1, sum(s2) as q2, sum(s3) as q3, sum(s4) as q4
from (
select col1, col2 as s1, 0 as s2, 0 as s3, 0 as s4
from tblsample where rqtr=1 and ryear = 2000
union all
select col1, 0, col2, 0, 0,
from tblsample where rqtr=2 and ryear = 2000
union all
select col1, 0, 0, col2, 0
from tblsample where rqtr=3 and ryear = 2000
union all
select col1, 0, 0, 0, col2
from tblsample where rqtr=4 and ryear = 2000
) dt
group by col1|||sorry forgot to add one column.

This exercise is for 50 employees

means

select empname, col1, col2 from tblsample where rqtr=1 and ryear = 2000
select empname, col1, col2 from tblsample where rqtr=2 and ryear = 2000
select empname, col1, col2 from tblsample where rqtr=3 and ryear = 2000
select empname, col1, col2 from tblsample where rqtr=4 and ryear = 2000

i need an output like
-------------------
Name 1 qtr 2 qtr 3 qtr 4 qtr
-------------------
1st emp
2nd emp
3rd emp
50th emp
--------------------|||which 50

just rework the query i gave you to add the extra column