Click here to Skip to main content
15,921,452 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Table_Check:
sequence_no Name Age Sex Empno
1 A 11 M 123
2 B 12 F 456
3 C 13 M 457
4 D 14 F 123
5 E 15 M 121
6 F 16 F 112
7 G 17 M 110
8 H 18 F 124

So if i write two queries as :
Select name as NameT1 from Table_Check where sequence_no%2<>0;
Its Resultant Will Be:
NameT1
A
C
E
G
Select name as NameT2 from Table_Check where sequence_no%2=0;
Its Resultant Will Be:
NameT1
B
D
F
H
Resultant required Is the resultant of above two queries but in a single table like this:
NameT1 NameT2
A B
C D
E F
G H

So usually how it can be done is writing two different queries.
Now how to create this new table with new 2 columns having result of query1 in ist column and result of query II in second column..
Eagerly waiting for help..
Thanks..
Posted

I can suggest a way of doing it, but before I do so, can you answer why you are relying on the sequence_no field (that looks a lot like an auto-numbering primary key) to determine what to put in column 1 and 2?

The reason I ask is that it would appear that you are trying to "partner up" people - what happens to your sequence when there's a single person entered?
 
Share this answer
 
I can suggest a way of doing it, but before I do so, can you answer why you are relying on the sequence_no field (that looks a lot like an auto-numbering primary key) to determine what to put in column 1 and 2?
The reason I ask is that it would appear that you are trying to "partner up" people - what happens to your sequence when there's a single person entered?


MR. DAMIAN..
Acctually the report i need to formulate this result into is supposed to have two columns and where in the ist record from this table goes into the left column of the report and second record goes into the right column abd then the third record is again inserted into the ist column and likewize till the end of records..And when there is only single person enterred that will be keyed upto 1.
And this report association is with a particular employee where in we check the uploaded files of a particular employee..
For e.g
ID------Empno-----Desc----Filename------ref_no-----seq_no
1-------12345-----a.doc----ABC-----------01---------1
1-------12345-----b.doc----XYZ-----------02---------2
1-------12345-----d.doc----PQR-----------81---------3
1-------12345-----x.doc----CDE-----------11---------4
1-------67890-----a1.doc---ABCD----------011--------1
1-------67890-----b1.doc---XYZD----------021--------2
1-------67890-----d1.doc---PQRD----------811--------3
1-------67890-----x1.doc---CDED----------111--------4


Now when i am formulating this result in my procedure i just need the Desc column for a particular employee for e.g employee no -12345

Desc1----------Desc2
a.doc----------b.doc
c.doc----------d.doc

And why i have used this sequence_no is to separate the odd and the even rows by two different queries and then display them into a single table as one above..

Hope you get my point.
 
Share this answer
 
I think this query works:
SQL
select NameT1 = o.name, NameT2 = e.name
from
(
    select *, t=(sequence_no-1)/2
    from Table_Check 
    where sequence_no%2<>0
) o inner join
(
    select *, t=(sequence_no-1)/2
    from Table_Check 
    where sequence_no%2=0
) e on e.t = o.t



But it is not a nice one!
 
Share this answer
 
@WOUL:

No this is not the correct one coz one it will duplicate the data and second it wont display the last record if the number of records to be displayed is odd in number.. Coz the join wont have any record for the last record to be merged with.. Hope you can find a better and appropriate solution and i hope to get that soon..
thanks..
 
Share this answer
 
SQL
select NameT1 = o.name, NameT2 = e.name
from
(
    select *, t=(sequence_no-1)/2
    from Table_Check 
    where sequence_no%2<>0
) o left outer join
(
    select *, t=(sequence_no-1)/2
    from Table_Check 
    where sequence_no%2=0
) e on e.t = o.t
 
Share this answer
 
@WoulT

Thanks a ton and i really appreciate that you spared your valuable time to look into this issue..
Thanks once again..
 
Share this answer
 
Create two views (or queries, depending on the database you are using)

View1

select empno, desc, seq_no as sequencenum from tablename
where seq_no mod 2 = 1

View2

select empno, desc, seq_no - 1 as sequencenum from tablename
where seq_no mod 2 = 0

This gives you the first view having a list of items with the odd sequence number, and the second view having a list of items that previously HAD even sequence numbers, but subtracting one from each, so you can link them.

Then, create a new query like this:

select view1.empno, view1.desc, view2.desc
from view1 left join view2 on view1.empno = view2.empno and view1.sequencenum = view2.sequencenum

It's hideous, but it gives you what you asked for.
 
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