[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Update query with MAX and GROUP BY with other fields

Posted on 2009-07-01
16
Medium Priority
?
385 Views
Last Modified: 2012-05-07
I have the following table-structure (which I cannot change)
qPortfolioAndValue (PK: ObjID_Cportfolio) - Account (PK: ObjID, FK: ObjID_Cportfolio)
     qPortfolioAndValue has 0 or more records in Account
Account (PK: ObjID) - cashMovement (PK: ObjID, FK: ObjID_CAccount)
   an Account has 0 or more record(s) in cashMovement
Account (PK: ObjID) - securityMovement (PK: ObjID, FK: ObjID_CAccount)
   an Account has 0 or more record(s) in cashMovement

What I need:
I want to update the record in qPortfolioAndValue (fields LatestMove, LatestPrice) with the following record:
The record of cashMovement or SecurityMovement with the most recent date (= max date)

The problem is that I  manage to get the latest record of CashMovement with the following query:
select QP.ObjID_Cportfolio, Max(CM.Date)
from cashMovement CM
   left join Account A ON CM.ObjID_CAccount = A.ObjID
   left join uvw_qPortfolioAndValue QP ON A.ObjID_Portfolio = QP.ObjID_Cportfolio
GROUP BY QP.ObjID_Cportfolio

But I don't know how to get the other field (CM.Price) to use it in an update query
And if I got that field, how about the latest of cashMovement or securityMovement ?
0
Comment
Question by:Dhaest
[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
  • 8
  • 8
16 Comments
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24752640
can you please try this one?



select QP.ObjID_Cportfolio as id, Max(CM.Date) as dt,price=
(
	select c1.price
	from cashMovement C1 
	   left join Account Aa ON C1.ObjID_CAccount = A1.ObjID
	   left join uvw_qPortfolioAndValue Q1 ON A1.ObjID_Portfolio = Q1.ObjID_Cportfolio
		where q1.objID_Cportfolio=qp.objID_Cportfolio and c1.date=cm.dt
)
	from cashMovement CM 
	   left join Account A ON CM.ObjID_CAccount = A.ObjID
	   left join uvw_qPortfolioAndValue QP ON A.ObjID_Portfolio = QP.ObjID_Cportfolio
	GROUP BY QP.ObjID_Cportfolio

Open in new window

0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24752653
or may be this one
select id,dt,price=
(
	select top 1 c1.price
	from cashMovement C1 
	   left join Account Aa ON C1.ObjID_CAccount = A1.ObjID
	   left join uvw_qPortfolioAndValue Q1 ON A1.ObjID_Portfolio = Q1.ObjID_Cportfolio
		where q1.objID_Cportfolio=t.id and c1.date=t.dt
)
from
(
	select QP.ObjID_Cportfolio as id, Max(CM.Date) as dt
		from cashMovement CM 
		   left join Account A ON CM.ObjID_CAccount = A.ObjID
		   left join uvw_qPortfolioAndValue QP ON A.ObjID_Portfolio = QP.ObjID_Cportfolio
		GROUP BY QP.ObjID_Cportfolio
) as t	

Open in new window

0
 
LVL 53

Author Comment

by:Dhaest
ID: 24752690
>> c1.date=cm.dt
The uvw_qPortfolioAndValue does not contain that Date-field.
I want to update a date-field in uvw_qPortfolioAndValue  with the latest date (= Max(CM.Date) from cashMovement)
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 53

Author Comment

by:Dhaest
ID: 24752698
I think that you're second query returns what I want (I need to verify some  items).
How can I use this query to update the values in uvw_qPortfolioAndValue  (id, dt and code)

select id,dt,code=
(
	select top 1 c1.code
	from cashMovement C1 
	   left join Account A1 ON C1.ObjID_CAccount = A1.ObjID
	   left join uvw_qPortfolioAndValue Q1 ON A1.ObjID_Portfolio = Q1.ObjID_Cportfolio
		where q1.objID_Cportfolio=t.id and c1.date=t.dt
)
from
(
	select QP.ObjID_Cportfolio as id, Max(CM.Date) as dt
		from cashMovement CM 
		   left join Account A ON CM.ObjID_CAccount = A.ObjID
		   left join uvw_qPortfolioAndValue QP ON A.ObjID_Portfolio = QP.ObjID_Cportfolio
		GROUP BY QP.ObjID_Cportfolio
) as t	

Open in new window

0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24752711
I didn't use date field of uvw_qPortfolioAndValue, I have tried to get latest price based on the max date comming in base query to cashMovement of inner query.
0
 
LVL 53

Author Comment

by:Dhaest
ID: 24752714
>> I didn't use date field of uvw_qPortfolioAndValue, I have tried to get latest price based on the max date comming in base query to cashMovement of inner query.

I saw it, I was too quick with posting my comment !
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24752721
ok. try the query and let me know whether it is giving proper results or not.
0
 
LVL 53

Author Comment

by:Dhaest
ID: 24752738
As far as I saw it quickly, it seems to be ok. How can I use this query to update the values in uvw_qPortfolioAndValue  (dt and code)
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24752759
here you go,

check on test DB first and do check ON condition of update, mainly field name, I may made mistake in that.



update uvw_qPortfolioAndValue set uvw_qPortfolioAndValue.date=tempdb.t.dt, uvw_qPortfolioAndValue.code=t.code from
 
(
	select id,dt,code=
	(
			select top 1 c1.code
			from cashMovement C1 
			   left join Account A1 ON C1.ObjID_CAccount = A1.ObjID
			   left join uvw_qPortfolioAndValue Q1 ON A1.ObjID_Portfolio = Q1.ObjID_Cportfolio
					where q1.objID_Cportfolio=t.id and c1.date=t.dt
	)
	from
	(
			select QP.ObjID_Cportfolio as id, Max(CM.Date) as dt
					from cashMovement CM 
					   left join Account A ON CM.ObjID_CAccount = A.ObjID
					   left join uvw_qPortfolioAndValue QP ON A.ObjID_Portfolio = QP.ObjID_Cportfolio
					GROUP BY QP.ObjID_Cportfolio
	) as tt  
) as t
join uvw_qPortfolioAndValue on uvw_qPortfolioAndValue.ObjID_Cportfolio=t.id and uvw_qPortfolioAndValue.date=t.dt

Open in new window

0
 
LVL 53

Author Comment

by:Dhaest
ID: 24752941
What if I need an extra field
select id,dt,code=
      (  select top 1 c1.code

Can I just alter it to
select id,dt,code,anotherfield=
      (
                  select top 1 c1.code, c1.anotherfield

0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24752953
it all depends on your need, but yes, you can do something like that also.
0
 
LVL 53

Author Comment

by:Dhaest
ID: 24752970
When I try that, I got an error:

Msg 116, Level 16, State 1, Line 1
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
update uvw_qPortfolioAndValue 
   set uvw_qPortfolioAndValue.LatestMovementDate=t.dt, 
       uvw_qPortfolioAndValue.LatestMovementType=t.code,
       uvw_qPortfolioAndValue.LatestMovementProductObjID = t.objid_CProductMainInfo
from
(
	select id,dt,code=
	(
		select top 1 c1.code, c1.objid_Cproductmaininfo
		from securityMovement C1 
			left join Account A1 ON C1.ObjID_CAccount = A1.ObjID
			left join uvw_qPortfolioAndValue Q1 ON A1.ObjID_Portfolio = Q1.ObjID_Cportfolio
		where q1.objID_Cportfolio=tt.id and c1.date=tt.dt
	)
	from
	(
			select QP.ObjID_Cportfolio as id, Max(CM.Date) as dt
					from securityMovement CM 
					   left join Account A ON CM.ObjID_CAccount = A.ObjID
					   left join uvw_qPortfolioAndValue QP ON A.ObjID_Portfolio = QP.ObjID_Cportfolio
					GROUP BY QP.ObjID_Cportfolio
	) as tt  
) as t
join uvw_qPortfolioAndValue on uvw_qPortfolioAndValue.ObjID_Cportfolio=t.id

Open in new window

0
 
LVL 31

Accepted Solution

by:
RiteshShah earned 2000 total points
ID: 24753014
TRY THIS ONE:



update uvw_qPortfolioAndValue 
   set uvw_qPortfolioAndValue.LatestMovementDate=t.dt, 
       uvw_qPortfolioAndValue.LatestMovementType=t.code,
       uvw_qPortfolioAndValue.LatestMovementProductObjID = t.objid_CProductMainInfo
from
(
        select id,dt,code=
        (
                select top 1 c1.code, c1.objid_Cproductmaininfo
                from securityMovement C1 
                        left join Account A1 ON C1.ObjID_CAccount = A1.ObjID
                        left join uvw_qPortfolioAndValue Q1 ON A1.ObjID_Portfolio = Q1.ObjID_Cportfolio
                where q1.objID_Cportfolio=tt.id and c1.date=tt.dt
        ), objid_CProductMainInfo=
        (
			        select top 1 c1.objid_Cproductmaininfo
                from securityMovement C1 
                        left join Account A1 ON C1.ObjID_CAccount = A1.ObjID
                        left join uvw_qPortfolioAndValue Q1 ON A1.ObjID_Portfolio = Q1.ObjID_Cportfolio
                where q1.objID_Cportfolio=tt.id and c1.date=tt.dt
        )
        from
        (
                        select QP.ObjID_Cportfolio as id, Max(CM.Date) as dt
                                        from securityMovement CM 
                                           left join Account A ON CM.ObjID_CAccount = A.ObjID
                                           left join uvw_qPortfolioAndValue QP ON A.ObjID_Portfolio = QP.ObjID_Cportfolio
                                        GROUP BY QP.ObjID_Cportfolio
        ) as tt  
) as t
join uvw_qPortfolioAndValue on uvw_qPortfolioAndValue.ObjID_Cportfolio=t.id

Open in new window

0
 
LVL 53

Author Comment

by:Dhaest
ID: 24753078
That's working, now I only have still to figure this out:
The record of cashMovement or SecurityMovement with the most recent date (= max date)
(now the query is retrieving the maxdate from cashMovement or securityMovement, but not the latest date from the union of both tables)
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24753095
not getting you
0
 
LVL 53

Author Closing Comment

by:Dhaest
ID: 31598694
Thanks for helping me figure this out !
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

650 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