Question: Can someone look at my select statement and offer some help? I have a field that has a whole number value that I need to convert to a decimal and depending on if that value is > 0 or not, then do some more math to ultimately get the discounted cost after the discount percent is taken off. The problem is it wants to put the discount field into my group by clause which I do not want since it then will group the discounted ones seperate from the not discounted ones. Hopefully this makes sense. Here is the statement.

```
SELECT CASE 
            WHEN ITEMS.DISCOUNT > 0 THEN 
                (
                     SUM(ITEMS.IT_UNLIST * QUANTS) 
                     - SUM(ITEMS.IT_UNLIST * QUANTS) 
                     * CAST((ITEMS.DISCOUNT/100) as DECIMAL(8,2))
                 )
            ELSE 
                SUM(ITEMS.IT_UNLIST * QUANTS) 
        END AS Sales
        , MONTH(ITEMS.IT_SDATE) AS Mnth
        , CUST.STATE

```

I group by Mnth, then by State and thats all I want to group by. I am pretty new to sql so any advice would be appreciated. Any thoughts?

Answer: 
Use the mult-thread controle `lock`. This prevents that your code will be run more the one time in same moment.

**EDITED**  

EXAMPLE:

```
public class Foo
{
    private static object _lockKey = new object();

    private void YourEventHandlerMethod(object sender, EventArgs e)
    {   
        lock (_lockKey)
        {
            // the code you put inside this block will be execute only
            // once at time.

            // If a second call has the intention to use this block before
            // the conclusion of the first call, the second call (the thread)
            // will be put in hold.
        }
    }
}

```
There are a few options to serialize handling of the events which may be fired concurrently from multiple threads, or on the same thread via re-enrancy/recursion.

One of the options is to use a task scheduler. In your scenario, the purpose of the task scheduler would be to queue callbacks for later serial execution. A well known conceptual examples of the task schedulers like that are WinForms message loop (starting with `Application.Run`, queuing with `Control.BeginInvoke`) and WPF Dispatcher loop (starting with `Dispatcher.Run`, queuing with `Dispatcher.BeginInvoke` or `Dispatcher.InvokeAsync`).

A less know but very useful implementation of a [TPL](https://stackoverflow.com/questions/tagged/task-parallel-library) task scheduler is [`StaTaskScheduler`](http://blogs.msdn.com/b/pfxteam/archive/2010/04/07/9990421.aspx) by Stephen Toub. I posted some code based upon it [here](https://stackoverflow.com/a/21371891/1768303) and [here](https://stackoverflow.com/a/21645679/1768303) (the latter one is from a closely question, [Queuing Actions/Delegates for Asyncronous Execution](https://stackoverflow.com/questions/21628490/queuing-actions-delegates-for-asyncronous-execution)).

Another option is described here: [Task sequencing and re-entracy](https://stackoverflow.com/questions/21424084/task-sequencing-and-re-entracy).

In this case, handlers are composed as tasks, each new handler is started when the previous task has completed.