Home Forums SQL Server 2014 Development - SQL Server 2014 What is the best way to SELECT SUM while using PARTITION BY and WHERE and GROUP BY? RE: What is the best way to SELECT SUM while using PARTITION BY and WHERE and GROUP BY?

  • Hi :

    Since we dont have the information that is in your mind, we need a more structure approach so that we can effectively understand your issue without ambiguity. Here is another article that will help with that : http://www.sqlservercentral.com/articles/Best+Practices/61537/

    Your explanation seems fragmented and goes back and forth it seems, not linear. For example , what do the following mean >

    "Except that the rows are single transactions and the [Number of Employees] is the total number of employees for a [Company]. ",

    It cant be the total number of employees from the data you provided. This number of employees does not belong in a transaction table if it is a mere current snapshot. How is it that Leah has 44215 and 46289 employees from the data in the transaction table? There is more missing here.

    "And it has to be grouped by year and filtered down. And I need to do this based on input parameters (@Period_Year and @Period_Month)."

    Filtered by year OR year/month? Please be clear. I mean what happens if there are no transactions in a month? Are there no employees in the company that month?

    Just answering these questions is not a fix. Take the time to carefully read through the material and put forth the information (tables , data, what they represent at a granular level) asked for in the articles. Then we can get a better idea of what you have first and foremost , and where you want to go with it.

    ----------------------------------------------------