sql - Execute a query foreach row from a result set. -
i have query needs executed each row in result set. understand need cursors, dont know how.
the first query contains customers specific condition, need retreive id:s them.
the second query selects latest activity these customers.
query 1
select distinct c.name, c.id persons p join customers c on c.id = p.customer_id c.state = 3 , c.salesperson_id ='a3160011-caa6-e411-a83e-ac7ba1b90a6d' , c.id not in ( select distinct p.customer_id activities join persons p on a.person_id = p.id a.[type] in (5,6) , a.[time] > getdate()-30 , a.[time] < getdate() ) , c.id not in ( select distinct p.customer_id persons p join htmlemails on p.id = he.userid join reportlog rl on he.id = rl.htmlemails_id rl.[time] > getdate()-30 , rl.[time] < getdate()) query 2
select top 1 * from( select top 1 c.name 'customername', c.id 'customerid', a.[time] 'lastactivity',(p.firstname + ' ' + p.lastname) 'userfullname' , 'login' 'type' activities join persons p on p.id = a.person_id join customers c on p.customer_id = c.id a.[type] in (5,6) , a.[time] < getdate()-30 order a.[time] desc union select top 1 c.name 'customername', c.id 'customerid', a.[time] 'lastactivity',(p.firstname + ' ' + p.lastname) 'userfullname' , 'email' 'type' activities join htmlemails on a.targetid = he.id join reportlog rl on he.id = rl.htmlemails_id join persons p on p.id = a.person_id join customers c on p.customer_id = c.id a.[time] < getdate()-30 order a.[time] desc )as x order 'lastactivity' desc thanks!
Comments
Post a Comment