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.
| Author |
Topic |
|
Turing
Starting Member
3 Posts |
Posted - 2007-09-20 : 17:22:04
|
| Customer: customerId, customerNameOrder: orderId,customerId,product, date, productDefinetionI want to display the order which is the latest order from the customer and I want to display all customer NameExample:customerName product productDefinetionJohn Video bla bla blaMaria ------ ----------- |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-09-20 : 17:25:37
|
| [code]SELECT customerName, product, productDefinitionFROM YourTable yINNER JOIN ( SELECT customerName, MAX(date) AS date FROM YourTable GROUP BY customerName) tON y.customerName = t.customerName AND y.date = t.date[/code]Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
Turing
Starting Member
3 Posts |
Posted - 2007-09-20 : 17:52:00
|
| In additon I want to displat the customer name which is not have any order in the same result |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-09-20 : 17:52:57
|
| Well my query displays the customerName. I don't know what the following means: "which is not have any order in the same result"Please provide a data example of exactly what you have in your tables and then a data example of what result set should be returned.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
Turing
Starting Member
3 Posts |
Posted - 2007-09-21 : 07:03:10
|
| my table isCustomer Customer Id--------- ----------------Mary 1Jhon 2Anna 3OrderId CustomerId Product ProductDesc------- --------- --------- -----------1 1 video bla bla2 1 tv bla bala3 2 video bla bla4 2 cd bla blaI want to see-------marry tv bla blaJohn cd bla blaanna |
 |
|
|
PeterNeo
Constraint Violating Yak Guru
357 Posts |
Posted - 2007-09-21 : 08:39:50
|
| Hi,DECLARE @Customer TABLE ( Customer VARCHAR(30), CustomerId INT)INSERT INTO @Customer SELECT 'Mary', 1 UNION ALLSELECT 'Jhon', 2 UNION ALLSELECT 'Anna', 3DECLARE @Order TABLE ( OrderId INT, CustomerId INT, Product VARCHAR(30), ProductDesc VARCHAR(30))INSERT INTO @Order SELECT 1, 1, 'video', 'bla bla' UNION ALLSELECT 2, 1, 'tv', 'bla bala' UNION ALLSELECT 3, 2, 'video', 'bla bla' UNION ALLSELECT 4, 2, 'cd', 'bla bla'--SELECT * FROM @Customer --SELECT * FROM @Order SELECT C.Customer, O.Product, O.ProductDescFROM @Customer CLEFT JOIN ( SELECT CustomerId, Product, ProductDesc, ROW_NUMBER() OVER (PARTITION BY CustomerId ORDER BY OrderId DESC) AS 'Rank' FROM @Order) O ON O.CustomerId = C.CustomerId AND O.Rank = 1 |
 |
|
|
|
|
|
|
|