Link to home
Create AccountLog in
Avatar of jimmylew52
jimmylew52Flag for United States of America

asked on

I need a sql query that will pull the company name from table_a and put it togeather with data from table_b

I  need a sql query that will pull the company name from table_a based on an ID number and put it together with the number of rows that exist for that ID number in another table, Transactions. I have been able to pull the ID and # of rows with the following:

select "Column 0",count(*)
from transactions
Group by "Column 0"

When I try to expand on this I get errors so I think I have no clue who to get it done.
Avatar of awking00
awking00
Flag of United States of America image

Not sure I understand the question. What is it you're trying to "put together"? Do you have a column in the transactions table for company_name? If so, are you looking to update it with values from tableA based on the ID? If not, do you want to add a column in the transactions table for company_name? Perhaps you can provide some sample data for the two tables and what you expect the end results to be so we can get a better idea of what it is you want to accomplish.
select [Column 0],count(*) cnt
from transactions
Group by [Column 0]
Avatar of Mike77
Mike77

I think it should look like something like this :

SELECT table_a.ID, Count(transactions.IDofTable) AS NumberOfTransactions
FROM table_a LEFT JOIN transactions ON table_a.ID = transactions.CorrespondingIDFieldName
GROUP BY table_a.ID
Still not sure what you need, perhaps something like this -
select a.company_name, count(t.id)
from table_a as a, transactions as t
where a.id = t.id;
You probably need a join:

SELECT a.[company_name], COUNT(b.id) AS Cnt
FROM [table_a] a INNER JOIN
    [table_b] b ON a.id = b.id
GROUP BY a.[company_name]
Avatar of jimmylew52

ASKER

This is close I think:

SELECT table_a.ID, Count(transactions.IDofTable) AS NumberOfTransactions
FROM table_a LEFT JOIN transactions ON table_a.ID = transactions.CorrespondingIDFieldName
GROUP BY table_a.ID

It does not give me the corresponding company name for the ID # just the row count.
You will have to add the company name field, which i am guessing is in your "table_a" table :

SELECT table_a.CompanyNameField, table_a.ID, Count(transactions.IDofTable) AS NumberOfTransactions
FROM table_a LEFT JOIN transactions ON table_a.ID = transactions.CorrespondingIDFieldName
GROUP BY table_a.CompanyNameField, table_a.ID

If you want only the companies that have transactions, you'll have to replace the left join for an inner join.
matthewspatrick

This works!

SELECT a.[company_name], COUNT(b.id) AS Cnt
FROM [table_a] a INNER JOIN
    [table_b] b ON a.id = b.id
GROUP BY a.[company_name]

But already I have been given, as expected, additional parameters. I need to list additional fields in the outpput along with the company name, ID, tel_num, address, city_name.

Is that easy to add? I made an effort and get errors.
ASKER CERTIFIED SOLUTION
Avatar of Patrick Matthews
Patrick Matthews
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Sorry I forgot the group by clause in my original comment. However, since you need additional columns, you might try this
select a.company_name, a.tel_num, a.address, a.city_name,
count(t.id) over (partition by a.company_name order by a.id) as cnt
from table_a as a, transactions as t
where a.id = t.id
Working Great, Thanks to everyone that contributed.