I have a table of Salesmen and as a child table, with PK FK constraint, the
OrdersBySalesmen with a DateOrdered field in the latter.
Table Salesmen
Id Type IntName nvarchar(50)
Table OrderBySalesmen
Id Type Int
SalesmenId TypeInt
DateOrdered Type DateTime
MyQuestion is a s follows.
I need to obtain as a query result the last (most recent) 10 sales records
for each salesman.
Can anyone tell me how this query would be made, If I do a select TOP 10
with innner joints order by dateOrdered DESC I only get 10 records, not the
top 10 for EVERY salesman.
Any help would be greatly appreciated.
RDTry,
select SalesmenId, [Id], DateOrdered
from OrderBySalesmen as a
where [id] in (select top 10 [id] from OrderBySalesmen as b where
b.SalesmenId = a.SalesmenId)
AMB
"RD" wrote:
> I have a table of Salesmen and as a child table, with PK FK constraint, th
e
> OrdersBySalesmen with a DateOrdered field in the latter.
> Table Salesmen
> Id Type IntName nvarchar(50)
> Table OrderBySalesmen
> Id Type Int
> SalesmenId TypeInt
> DateOrdered Type DateTime
> MyQuestion is a s follows.
> I need to obtain as a query result the last (most recent) 10 sales records
> for each salesman.
> Can anyone tell me how this query would be made, If I do a select TOP 10
> with innner joints order by dateOrdered DESC I only get 10 records, not th
e
> top 10 for EVERY salesman.
> Any help would be greatly appreciated.
> RD
>
>|||Correction,
select SalesmenId, [Id], DateOrdered
from OrderBySalesmen as a
where [id] in (select top 10 [id] from OrderBySalesmen as b where
b.SalesmenId = a.SalesmenId
order by DateOrdered desc
)
AMB
"Alejandro Mesa" wrote:
> Try,
> select SalesmenId, [Id], DateOrdered
> from OrderBySalesmen as a
> where [id] in (select top 10 [id] from OrderBySalesmen as b where
> b.SalesmenId = a.SalesmenId)
>
> AMB
> "RD" wrote:
>
Friday, February 24, 2012
How to Query Question
Labels:
child,
constraint,
database,
dateordered,
field,
latter,
microsoft,
mysql,
oracle,
query,
salesmen,
salesmenid,
server,
sql,
table,
theordersbysalesmen
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment