•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the MS SQL section within the Web Development category of DaniWeb, a massive community of 402,545 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,298 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our MS SQL advertiser: Programming Forums
Views: 795 | Replies: 4
![]() |
•
•
Join Date: Sep 2007
Posts: 4
Reputation:
Rep Power: 0
Solved Threads: 0
Customer: customerId, customerName
Order: orderId,customerId,product, date, productDefinetion
I want to display the order which is the latest order from the customer and
I want to display all customer Name
Example:
customerName product productDefinetion
John Video bla bla bla
Maria ------ -----------
Order: orderId,customerId,product, date, productDefinetion
I want to display the order which is the latest order from the customer and
I want to display all customer Name
Example:
customerName product productDefinetion
John Video bla bla bla
Maria ------ -----------
•
•
Join Date: Feb 2005
Location: Braintree, UK
Posts: 1,165
Reputation:
Rep Power: 7
Solved Threads: 58
Untested but this should be close if not perfect. Basically you need to group up the order table to reduce it to just the most recent (max) date for each customer id (this is a sub-query) then you can join in the customer and order tables for the rest of the info (customer names, product names etc.) That's the outter query.
select
c.customerName, o.product, o.productDefinition, q.orderDate
from
(
select
customerId, max(date) as orderDate
from
Order
group by
customerId
) q
join customer c on q.customerId = c.customerId
join order o on q.customerId = o.customerId
and q.orderDate = o.date•
•
Join Date: Sep 2007
Posts: 4
Reputation:
Rep Power: 0
Solved Threads: 0
It is not give the my mind . I wan to that
my table is
Customer Customer Id
--------- ----------------
Mary 1
Jhon 2
Anna 3
OrderId CustomerId Product ProductDesc
------- --------- --------- -----------
1 1 video bla bla
2 1 tv bla bala
3 2 video bla bla
4 2 cd bla bla
I want to see
-------
marry tv bla bla
John cd bla bla
anna
my table is
Customer Customer Id
--------- ----------------
Mary 1
Jhon 2
Anna 3
OrderId CustomerId Product ProductDesc
------- --------- --------- -----------
1 1 video bla bla
2 1 tv bla bala
3 2 video bla bla
4 2 cd bla bla
I want to see
-------
marry tv bla bla
John cd bla bla
anna
•
•
Join Date: Feb 2005
Location: Braintree, UK
Posts: 1,165
Reputation:
Rep Power: 7
Solved Threads: 58
•
•
•
•
It is not give the my mind . I wan to that
I have given you enough to solve the problem. You have changed your schema, what happened to the date column? Now I only have the orderid to key off and assume it's an increasing identity field.
Using the info you provide here is the solution:
create table Customer
(
Customer varchar(32),
[Customer Id] int
)
create table [Order]
(
OrderId int,
[CustomerId] int,
Product varchar(32),
ProductDesc varchar(32)
)
insert into Customer (Customer, [Customer Id])
select 'Mary', 1
union all
select 'Jhon', 2
union all
select 'Anna', 3
insert into [Order](OrderId, [CustomerId], Product, ProductDesc)
select 1, 1, 'video', 'bla bla'
union all
select 2, 1, 'tv', 'bla bala'
union all
select 3, 2, 'video', 'bla bla'
union all
select 4, 2, 'cd', 'bla bla'
select
c.Customer I, ISNULL(o.Product,'') want, ISNULL(o.ProductDesc,'') [to see]
from
Customer c left join
(
select
[CustomerId], max(OrderId) as OrderId
from
[Order]
group by
[CustomerId]
) q on q.[CustomerId] = c.[Customer Id]
left join [Order] o on q.[CustomerId] = o.[CustomerId]
and q.OrderId = o.OrderId
order by
c.[Customer Id] I have run this in query analyser and this is the result set:
I want to see -------------------------------- -------------------------------- -------------------------------- Mary tv bla bala Jhon cd bla bla Anna (3 row(s) affected)
So I double dare you to tell me it's not what you want !!!
The cheek of some people!
Last edited by hollystyles : Sep 21st, 2007 at 8:02 am.
@ OP: You should give the correct information for your tables at the beginning when someone is trying to help you. And your second example makes no sense! Look at it again.
And while you are at it learn something. Look at the tutorials for starters.
http://www.intermedia.net/support/sql/sqltut.asp
And while you are at it learn something. Look at the tutorials for starters.
http://www.intermedia.net/support/sql/sqltut.asp
Last edited by iamthwee : Sep 22nd, 2007 at 8:15 am.
Member of: F-ugly code club
Join today don't delay!
Join today don't delay!
![]() |
•
•
•
•
•
•
•
•
DaniWeb MS SQL Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Removing Query Strings (ASP.NET)
- Double MySQL Query (PHP)
- Dynamic Query (JSP)
- MySQL nested query / joined query conversion help (MySQL)
- problem with lengthy query (Java)
- Retreiving variables from a sql query into a form (PHP)
- Sql Query (VB.NET)
- Query Building (Database Design)
Other Threads in the MS SQL Forum
- Previous Thread: SQL SERVER 2000 Selecting a record based on an aggregate function
- Next Thread: DTS on MSSQL 2005 Express edition



Linear Mode