jimmylew52
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.
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.
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]
from transactions
Group by [Column 0]
I think it should look like something like this :
SELECT table_a.ID, Count(transactions.IDofTab le) AS NumberOfTransactions
FROM table_a LEFT JOIN transactions ON table_a.ID = transactions.Corresponding IDFieldNam e
GROUP BY table_a.ID
SELECT table_a.ID, Count(transactions.IDofTab
FROM table_a LEFT JOIN transactions ON table_a.ID = transactions.Corresponding
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;
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]
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]
ASKER
This is close I think:
SELECT table_a.ID, Count(transactions.IDofTab le) AS NumberOfTransactions
FROM table_a LEFT JOIN transactions ON table_a.ID = transactions.Corresponding IDFieldNam e
GROUP BY table_a.ID
It does not give me the corresponding company name for the ID # just the row count.
SELECT table_a.ID, Count(transactions.IDofTab
FROM table_a LEFT JOIN transactions ON table_a.ID = transactions.Corresponding
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.IDofTab le) AS NumberOfTransactions
FROM table_a LEFT JOIN transactions ON table_a.ID = transactions.Corresponding IDFieldNam e
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.
SELECT table_a.CompanyNameField, table_a.ID, Count(transactions.IDofTab
FROM table_a LEFT JOIN transactions ON table_a.ID = transactions.Corresponding
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.
ASKER
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.
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
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
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
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
ASKER
Working Great, Thanks to everyone that contributed.