0

I am trying to create a new table that contains a count for a specific column. The column I want the count for contains different organization names. I created a new table that contains two columns one for the count and one for the organization names. I able to get the count to work, but I can't figure out how to insert into the table. This is what I have currently:

INSERT INTO ORG_countnumber (COUNT_number, OrgLBN)
SELECT 
    COUNT(*), OrgLBN FROM ORG_NPI_HumanName_Add_Phone GROUP BY OrgLBN
as COUNT_number,
OrgLBN
AS OrgLBN
FROM ORG_NPI_HumanName_Add_Phone; 

Thanks in advance!

2
  • 1
    A view might work better for you. Commented Jul 17, 2017 at 15:58
  • Why not just this: INSERT INTO ORG_countnumber (COUNT_number, OrgLBN) SELECT COUNT(*), OrgLBN FROM ORG_NPI_HumanName_Add_Phone GROUP BY OrgLBN Commented Jul 17, 2017 at 15:59

2 Answers 2

1

I think the first half of your query was on the right track:

INSERT INTO ORG_countnumber (COUNT_number, OrgLBN)
SELECT COUNT(*), OrgLBN
FROM ORG_NPI_HumanName_Add_Phone
GROUP BY OrgLBN

But as was suggested in the comment, a view might make more sense here:

CREATE VIEW orgCountView AS
SELECT COUNT(*), OrgLBN
FROM ORG_NPI_HumanName_Add_Phone
GROUP BY OrgLBN

The reason a view would make sense is that the counts in your table could change all the time, and therefore the data in the ORG_countnumber table could easily become stale. A view would allow you to get the latest numbers without committing to a new table.

Sign up to request clarification or add additional context in comments.

Comments

0

You put the GROUP BY on the wrong place.

INSERT INTO ORG_countnumber (COUNT_number, OrgLBN)
SELECT 
    COUNT(*) as COUNT_number, OrgLBN
FROM ORG_NPI_HumanName_Add_Phone
GROUP BY OrgLBN; 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.