Solved

select row from table and most update row from the second table for each customer

Posted on 2016-08-07
6
57 Views
Last Modified: 2016-08-07
hi there ,
i have customer table like this :
Customers
CID NAME DATE ADDRESS CITY
1     dan     1.1.16  xxxx        yyyy
2     dana    5.5.16 ttttt        ccccc
Orders
OID  CID  OrderDate  OrderSum
1       1      10.1.16          100
2       2      5.5.16             40
3      1       10.2.16          100
4      2       10.06.16       250

now for each customer there is some orders i want to pull out the customer and the most new order to the same row
for dan i get this :
CID NAME DATE ADDRESS CITY OID  CID  OrderDate  OrderSum
1     dan      01.01.16 xxxx   yyyy  3      1     10.2.16         100
2    dana     05.05.16 tttt     cccc   4     2      10.6.16         250

how can i get it ?
thanks....
0
Comment
Question by:Tech_Men
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 49

Expert Comment

by:PortletPaul
ID: 41746472
Use row_number() over() in a subquery of the orders table partitioned by [CID] ordered by [OrderDate]

select *
From customers c
Inner join (
  Select *
, row_number() over(partition by cid order by orderdate desc) as rn
  From orders
 ) d on c.cid=d.cid
Where d.rn=1

Note if you ordered the date ASCending you get the first order instead. So to get the latest date use DESCending.
0
 

Author Comment

by:Tech_Men
ID: 41746476
hi
thanks for your message
i didn't understand i need a query that work
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 41746478
Please refresh page and relook. I am using a phone on a train and it is v.hard to write accurate code. It the  best i can do right now.
0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 

Author Comment

by:Tech_Men
ID: 41746484
ok
now its work but i need more 1 thing
i still need to see the customer that they not have any orders

Customers
CID NAME DATE ADDRESS CITY
1     dan     1.1.16  xxxx        yyyy
2     dana    5.5.16 ttttt        ccccc
3    ran        1.1.16  ffff f      pffff
Orders
OID  CID  OrderDate  OrderSum
1       1      10.1.16          100
2       2      5.5.16             40
3      1       10.2.16          100
4      2       10.06.16       250
CID NAME DATE ADDRESS CITY OID  CID  OrderDate  OrderSum
1     dan      01.01.16 xxxx   yyyy  3      1     10.2.16         100
2    dana     05.05.16 tttt     cccc   4     2      10.6.16         250
3   simon    01.01.16 ffff  ff  off
0
 
LVL 49

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 41746497
Use a LEFT JOIN instead of the INNER JOIN and change d.rn=1 to a join condition
SELECT
  *
FROM customers c
LEFT JOIN (
          SELECT
            *
            , ROW_NUMBER() OVER (PARTITION BY cid ORDER BY orderdate DESC) AS rn
          FROM orders
          ) d ON c.cid = d.cid AND d.rn = 1

Open in new window

0
 

Author Closing Comment

by:Tech_Men
ID: 41746503
A++
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question