sql server - SQL Query that returns only records with multiple rows -
i have database 2 tables: customers
, services
.
the customer
table has account information , services
table has individual records of every service company has performed.
the services
table linked customer
table id
column customers.id = services.customerid
.
i trying perform query return customer name , account number customers more 1 service has been performed within specified date range. example, show me customers i've performed work more twice in 2015.
how write type of query multiple service visits returned? can using cursors, counting results, etc. seems there must more efficient way.
just use join
, group by
so:
select customers.id customers join services on customer.id=services.customerid --where services date check here group customers.id -- can add more columns customers table here , in select needed having sum(1)>1
you can add where
clause filter services
' date range (just before group by
).
Comments
Post a Comment