User Name Password Register
DaniWeb IT Discussion Community
All
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 426,809 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 1,856 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: 844 | Replies: 4
Reply
Join Date: Sep 2007
Posts: 4
Reputation: Turing is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Turing Turing is offline Offline
Newbie Poster

Query Help

  #1  
Sep 20th, 2007
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 ------ -----------
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2005
Location: Braintree, UK
Posts: 1,165
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Rep Power: 7
Solved Threads: 59
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: Query Help

  #2  
Sep 20th, 2007
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
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote  
Join Date: Sep 2007
Posts: 4
Reputation: Turing is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Turing Turing is offline Offline
Newbie Poster

Re: Query Help

  #3  
Sep 21st, 2007
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
Reply With Quote  
Join Date: Feb 2005
Location: Braintree, UK
Posts: 1,165
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Rep Power: 7
Solved Threads: 59
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: Query Help

  #4  
Sep 21st, 2007
It is not give the my mind . I wan to that
Pardon ?????

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.
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote  
Join Date: Aug 2005
Posts: 4,782
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 319
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: Query Help

  #5  
Sep 22nd, 2007
@ 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
Last edited by iamthwee : Sep 22nd, 2007 at 8:15 am.
I'm not a programmer. My attitude starts with ignorance, holds steady at conversation, and ends with a trip to the hospital. Get used to it.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb MS SQL Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the MS SQL Forum

All times are GMT -4. The time now is 7:51 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC