Using T-SQL to Count Corporate Partners

Downloads
24009.zip

My company provides financial data about more than 10,000 US companies that are listed on different US stock exchanges. One of our clients asked me to develop a screening tool that would let their users analyze companies based on market cap, stock price, phase of product development, years in cash, number of corporate partners, and other factors.

I encountered a problem when I tried to display the count of corporate partners for each company. The data in the CorporatePartner column is a comma-delimited string that isn't in a relational database format. To display the count, I tried using dynamic SQL, which worked until I put in a WHERE condition to check the user-selected criteria—No of Partners<3 (or) No of Partners>3.

To find the number of corporate partners for each company, I wrote the T-SQL query that Listing 7 shows. This query logic works wonderfully. I can use a SELECT statement to display the count of corporate partners, and I can use the same logic in the WHERE clause to check the count based on user-selected criteria. This query subtracts the derived length (by replacing all commas with an empty string) from the total length of each corporate partner. The code in Listing 8 includes the sample data that I used to populate the tblCompanyInfo table, which consists of company ID, symbol, and a list of each company's corporate partners.

Please or Register to post comments.

IT/Dev Connections

Las Vegas
September 30th - October 4th

Paul ThurottOur Experts will show you:
• Common SQL Server
Problems
• Best Practices for T-SQL
• SQL Server Integration
Services
• Database Development

Come See Mike Otey & Tim Ford in Person!

Early Registration Now Open

From the Blogs
May 21, 2013
blog

A Common Misconception about MAXDOP

Out of the box, SQL Server is (and has been) able to take advantage of multiple processors/cores without any effort on behalf of administrators....More
May 9, 2013
blog

My ISO 8601-Compliant Signature 2

My family recently just "officially" announced that we're in the process of adopting a child from South Africa. We're quite excited, of course, but there's a ton of paperwork to do—along with the need for gobs of signatures....More
May 8, 2013
blog

Use SSIS for ETL from Hadoop

In this blog post, Mark Kromer walks you through using SSIS as a way to use ETL techniques using Microsoft's Hadoop on Windows (HDInsight) as a source using Hive connectors...More
SQL Server Pro Forums

Get answers to questions, share tips, and engage with the SQL Server community in our Forums.