---
title: "T-SQL How to Match Multiple Rows"  
description: "T-SQL How to Match Multiple Rows"  
author: "Anonymous User"  
published: 2015-05-25  
updated: 2015-05-25  
canonical: https://www.mindstick.com/forum/23262/t-sql-how-to-match-multiple-rows  
category: "mssql server"  
tags: ["sql server 2008"]  
reading_time: 2 minutes  

---

# T-SQL How to Match Multiple Rows

In my sample [application](https://www.mindstick.com/articles/12824/calculator-application-in-android) [two tables](https://www.mindstick.com/forum/159646/how-to-join-two-tables-in-oracle-to-get-single-line-results) company and items in my case company contains multiple items. We need to recognize multiple [products](https://www.mindstick.com/news/2358/apple-will-use-us-made-semiconductors-in-its-products-to-lessen-its-dependency-on-asia) whether they can match a company which is [already](https://yourviews.mindstick.com/view/88453/the-hidden-ways-ai-is-already-controlling-your-daily-life) in items record some script are below.

```
DECLARE @tblcompany TABLE(    companyID   int,    ItemID   int)INSERT INTO @tblcompany VALUES(436, 4313)INSERT INTO @tblcompany VALUES(436, 4305)INSERT INTO @tblcompany VALUES(436, 4986) INSERT INTO @tblcompany VALUES(437, 4313)INSERT INTO @tblcompany VALUES(437, 4305)INSERT INTO @tblcompany VALUES(442, 4313)INSERT INTO @tblcompany VALUES(442, 4335)INSERT INTO @tblcompany VALUES(445, 4305)INSERT INTO @tblcompany VALUES(445, 4335) DECLARE @tblitem TABLE(    ItemtID   int)INSERT INTO @tblitem VALUES(4305) INSERT INTO @tblitem VALUES(4313)
```

We have two item 4305 and 4313, then I need to [retrieve](https://www.mindstick.com/forum/34400/how-to-retrieve-form-values-in-controller-action) the [matched](https://answers.mindstick.com/qa/95756/what-are-the-benefits-of-google-adsense-matched-content) [package](https://www.mindstick.com/blog/12619/tips-for-finding-the-right-package-when-you-buy-instagram-followers) record 437. Only the exactly matched one can be return, so company 436 is not the [right one](https://answers.mindstick.com/qa/96967/what-operating-system-is-the-right-one). It's not easy to make a [multiple rows](https://www.mindstick.com/forum/160908/how-to-concatenate-text-from-multiple-rows-into-a-single-text-string-in-sql-server) query clause. please someone can have any suggestions? Thanks.

## Replies

### Reply by Anonymous User

Try this code

```
Declare @cnt IntSelect @cnt = count(distinct itemID) from tblitem  SELECT B.companyidFROM   (SELECT companyid        FROM   tblcompany         GROUP  BY companyid         HAVING Count(itemid) = @cnt) A        JOIN tblcompany B          ON a.companyid = b.companyid WHERE  EXISTS (SELECT 1  FROM tblitem c WHERE c.itemid = b.itemid) GROUP  BY B.companyid HAVING Count(DISTINCT B.itemid) = @cnt
```


---

Original Source: https://www.mindstick.com/forum/23262/t-sql-how-to-match-multiple-rows

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
