0

I have a SQL statement:

select 
    tms.Team, Count(tms.MSApprovedNPC) as JulyNPCResults 
from 
    tmsorder2 tms
where 
    Msapprovednpc not in ('N/A','No') and Month(Month) = 11 and Year(month) = '2013'
group by 
    tms.Team

these results are for a specific month of July 2013.

Now I want to load the results in a table NPRData for other months in to one table which has columns like

TEAM, JulyNPRResults, AugNPRResults, SepNPRResults, OctNPRResults

How can I loop the above query to insert data for other months?

Thanks in advance

2
  • Why do you want to loop rather than insert all of them? Commented Apr 14, 2014 at 19:56
  • As i have multiple months to insert in to NPRData i need to Loop the above query for all the months rt? Commented Apr 14, 2014 at 20:00

1 Answer 1

2

I would advise against storing your data in a denormalized format. It would be better to have a table like:

NPRData (Team,YearMonth,Results)

But you can do this without looping via a conditional SUM() or COUNT():

SELECT tms.Team
     ,SUM(CASE WHEN MONTH(Month)=11 and YEAR(month) ='2013' THEN 1 END) As NovNPRResults
     ,SUM(CASE WHEN MONTH(Month)=12 and YEAR(month) ='2013' THEN 1 END) As DecNPRResults
FROM tmsorder2 tms
WHERE Msapprovednpc NOT IN ('N/A','No') 
GROUP BY tms.Team
Sign up to request clarification or add additional context in comments.

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.