简体   繁体   中英

SQL Server stored procedure to count matching records in two tables and insert number into another table

I have two tables that I can find the matching records for with a join. However, I would like to find the count of the unique columns along with the unique sum of the columns found, then insert that integer value in another table column that stores the count of matching records found.

For example with below scenario

Table A

ID  ColumnA  ColumnB
------------------------
1    John      Toyota
2    John      Nissan
3    John      Mercedez
4    Mary      Infiniti
5    Mary      BMW

TableB

ID ColumnA    ColumnB      ColumnC      ColumnD    ColumnE
-----------------------------------------------------------
1  John        Manager     Nissan        Toyota    Mercedez
2  Mary        CEO         BMW           Infiniti  Jaguar

So in this scenario, we join Table A and B based on ColumnA.

SELECT a.ColumnA, a.ColumnB, b.ColumnA 
FROM TableA a
JOIN TableB b ON a.ColumnB = b.ColumnA;

A join to find the matching records in TableA and TableB is easy as seen above, but I am having issues coming up with a stored procedure to find the the number of matching records along with the unique columns.

What I am expecting to do as end result is a stored procedure to store

  1. The Unique Column found
  2. The total count of the unique columns found

Then insert the above criteria in TableC. So the results would like below. Because John appears in TableA and TableB, we would return the unique data found, so in this case it would be Toyota, Nissan, and Mercedez - a total of 3.

TableC

ID ColumnA    ColumnB      ColumnC      ColumnD    ColumnE
-----------------------------------------------------------
1  John        Toyota      Nissan       Mercedez    3
2  Mary        Infiniti    BMW                      2

Would appreciate some pointers/help on this.

CREATE PROCEDURE [dbo].[_UpdateColCByMatch]
AS
BEGIN
    UPDATE y SET y.ColumnC=x.MatchCount 
    FROM tableB y 
    INNER JOIN 
        (
            SELECT COUNT(a.ColumnA) AS MatchCount, ColumnA
            FROM TableA a
            JOIN TableB b ON a.ColumnA = b.ColumnA
            GROUP BY ColumnA
        ) x ON y.ColumnA=x.ColumnA;
END
GO

This answer is making the assumption you are looking to update ColumnC based on the number of matches of ColumnA.

It works by getting ColumnA and Match count, which is simple. But then we use that as a correlated subquery to join on with an update statement.

UPDATE TableB SET ColumnC = (SELECT COUNT(*) FROM TableA WHERE ColumnA = TableB.ColumnA)

Fiddle.

Although I am not an expert on SQL Server, I think the below solution would work for you.

Within your stored procedure you will need to query the count and then assign that value to a variable.

Once you do that, you could then use the variable as a part of an insert statement within the same stored procedure.

Ultimately, I think it will look something like this:

CREATE PROCEDURE InsertCountProc()
AS
BEGIN
SET NOCOUNT ON

SELECT COUNT(*) INTO @newVariable
FROM TableA a
INNER JOIN TableB b
ON a.ColumnA = b.ColumnA;

INSERT INTO COUNT_TABLE (COUNT_COLUMN)
VALUES (@newVariable);

END

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM