In Sql Server 2008R2, I need a script that will pull out memos attached to particular documents. There can be multiple memos attached to any one doc. Do not need to concatenate memos, just have one record for each memo with the results grouped and separated by roid (repair order id)
select rd.id, rd.timestamp, rd.by_emp, rd.roid, ro.memo
from (repdoc rd IN(select rd.roid, count(*) as memo_count
from repdoc rd
group by rd.roid
having count(*) > 1)
Returns error 'near IN'
Also tried changing IN to a FROM statement with no better results.
sample data
id roid timestamp by_emp memo
1458 698 date/time 14 needs brakes
1459 698 date/time 89 parts pulled
1460 698 date/time 32 parts installed
1521 698 date/time 32 tested
1200 145 date/time 14 picked up later
1201 1468 date/time 84 steering fluid needed
1203 1468 date/time 12 fluid installed
desired results
roid id timestamp by_emp memo
698 1458 date/time 14 needs brakes
698 1459 date/time 89 parts pulled
698 1460 date/time 32 parts installed
698 1521 date/time 32 tested
1200 145 date/time 14 picked up later
1468 1201 date/time 84 steering fluid needed
1468 1203 date/time 12 fluid installed
tabs not lining up correctly, but I think you can see what is needed.
Thank you!!
You should modify your query like
select rd.id,
rd.timestamp,
rd.by_emp,
rd.roid,
ro.memo,
xx.memo_count
from repdoc rd
join (select roid, count(*) as memo_count
from repdoc
group by roid
having count(*) > 1) xx on rd.roid = xx.roid;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.