Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to Extract Data in Two table

Author  Topic 

Easwar
Yak Posting Veteran

59 Posts

Posted - 2007-03-15 : 06:13:02
I've two table.......
One table Name Form1
Ex
----------
Name|F1No
----|-----
23wa|1
xyzx|2
abcd|3
......
......
......
---------
F1No====>primary Key
Second table Name Form2
Ex
--------
F2No
----
3
1
2
2
2
1
......
......
......
--------
F2No===>Foreign Key

i want top 3 Name(field) in first table(Form1).

Result
------
xyzx
23wa
abcd


SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-03-15 : 06:17:55
select top 3 name
from form1
order by f1no desc


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

Easwar
Yak Posting Veteran

59 Posts

Posted - 2007-03-15 : 06:23:36
that query i can't get result
that is wrong query...

that query no based second table(form2)
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-03-15 : 06:25:31
Wrong query?
It gives the result you want (at least as you have posted it).

Maybe you should be more clear about your objective here, and why there is a need to JOIN the two tables.


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-03-15 : 06:28:33
Or you mean TOP 3 according to number of records of each?

SELECT TOP 3 f1.Name
FROM Form1 AS f1
INNER JOIN Form2 AS f2 ON f2.F2No = f1.F1No
GROUP BY f1.Name, f1.F1No
ORDER BY COUNT(*) DESC


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

Easwar
Yak Posting Veteran

59 Posts

Posted - 2007-03-15 : 06:34:24
i need top 3 name. ( top 3 name means in second table maximum selected value.. My ex table mentioned. in second table F2No field 2 is one or time selected. so i need that no(2 is top) in my first table mention name(xyzx), so my result table i mentioned first position(2))...

now are u clear...
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-03-15 : 06:37:00
I already posted a working query 8 minutes ago. Have you tried that?


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

Easwar
Yak Posting Veteran

59 Posts

Posted - 2007-03-15 : 06:42:59
SELECT TOP 3 f1.Name
FROM Form1 AS f1
INNER JOIN Form2 AS f2 ON f2.F2No = f1.F1No
GROUP BY f1.Name, f1.F1No
ORDER BY COUNT(*) DESC

This query now worked

Very very thanks.........
Go to Top of Page
   

- Advertisement -