| Author |
Topic |
|
Petehe
Starting Member
20 Posts |
Posted - 2003-04-28 : 19:31:39
|
| Table CustomerCustomerIDCustomerNameTable OrderOrderIDCustomerIDOrderNo.Is there a way to list all customers with only one (any one ) record of there order ?Like:CustomerA orderACustomerB orderBAlthough customerA may have orderC, but I just want to list one of them.Thanks. |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2003-04-28 : 19:38:40
|
| Have you tried using DISTINCT?Tara |
 |
|
|
Petehe
Starting Member
20 Posts |
Posted - 2003-04-28 : 19:41:52
|
| As far as know it won't work since the composition of customer and order no is unique. Is that right? |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2003-04-28 : 19:42:47
|
| How about using SELECT TOP 1 then?Tara |
 |
|
|
Petehe
Starting Member
20 Posts |
Posted - 2003-04-28 : 19:46:15
|
| Can you give me an example, thanks |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2003-04-28 : 19:47:34
|
| SELEC TOP 1 *FROM sysobjectsWHERE xtype = 'U'ORDER BY nameThis will give you the top most record in sysobjects that is a table.Tara |
 |
|
|
Petehe
Starting Member
20 Posts |
Posted - 2003-04-28 : 19:50:01
|
| Can you please tell me how to join two table together with select top 1 |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2003-04-28 : 19:58:03
|
quote: Can you please tell me how to join two table together with select top 1
Sure...SELECT TOP 1 o.name, c.idFROM sysobjects oINNER JOIN syscomments c ON o.id = c.idWHERE xtype = 'P' AND name NOT LIKE 'dt%'ORDER BY nameTara |
 |
|
|
Petehe
Starting Member
20 Posts |
Posted - 2003-04-28 : 19:58:48
|
| Thanks very much, you are genius |
 |
|
|
Petehe
Starting Member
20 Posts |
Posted - 2003-04-28 : 20:05:00
|
| Sorry that didn't solve the problemI want all the o.name and one c.id related to each o.name listedEdited by - petehe on 04/28/2003 20:07:53 |
 |
|
|
mohdowais
Sheikh of Yak Knowledge
1456 Posts |
Posted - 2003-04-29 : 03:40:02
|
I thought we had done this enough times on this forum Petehe:When you say I want only one order per customer, will *any* order number do? Or do you have something more specific, like the last or first order? This should take care of something simple:SELECT CustomerID, MAX(OrderID) AS LastOrder FROM [Order] GROUP BY CustomerIDAs simple as that...and notice you dont need the Customer table, unless you have some customers who dont have any orders. In that case you need to do a left join to the Customer table, but the same trick applies.OS |
 |
|
|
|