Question

I am working on financial domain. So I want data from April To May.

Suppose Current Month IS June then I only want April to June of this year and it should group by year and month.

And If Suppose Current Month is February then I want details from last April to Jan i.e. April-2013 to Jan-2014

I have got the solution through CTE If i only run CTE Part of the query it is working perfectly fine This Part of query..

DECLARE @CurrentDate AS datetime = getdate();
    DECLARE @FirstDayInCurrentMonth AS datetime = DATEADD(day, 1 - DATEPART(day, @CurrentDate), @CurrentDate);
    DECLARE @LastApril AS datetime = DATEADD(month, -(DATEPART(month, @FirstDayInCurrentMonth) + 8) % 12, @FirstDayInCurrentMonth);

    WITH Periods AS (
        SELECT @LastApril AS [Period]
      UNION ALL
        SELECT DATEADD(month, 1, [Period])
        FROM Periods
        WHERE [Period] < @FirstDayInCurrentMonth
    )
    SELECT 
            DATENAME(month, [Period]),
            DATEPART(year, [Period]),  

    FROM Periods

But I want sm.is_approved cloumn also work as above query. when I jon other table with above CTE expression. I want proper group by of sm.is_approved column.

I only have the doubt that what will be the condion for joining the other table. Please give men the proper condition for the below bold part of the query.

DECLARE @CurrentDate AS datetime = getdate();
DECLARE @FirstDayInCurrentMonth AS datetime = DATEADD(day, 1 - DATEPART(day, @CurrentDate), @CurrentDate);
DECLARE @LastApril AS datetime = DATEADD(month, -(DATEPART(month, @FirstDayInCurrentMonth) + 8) % 12, @FirstDayInCurrentMonth);

WITH Periods AS (
    SELECT @LastApril AS [Period]
  UNION ALL
    SELECT DATEADD(month, 1, [Period])
    FROM Periods
    WHERE [Period] < @FirstDayInCurrentMonth
)
SELECT 
        DATENAME(month, [Period]),
        DATEPART(year, [Period]),  
        MONTH(sm.is_approved) as [MONTH],
        isnull(sum(ISM.selling_price * siim.qty),0) as Price,  
        isnull(sum(((tm.tax_amount*(ism.selling_price * siim.qty))/100)),0) as Tax,  
        isnull(sum((ism.selling_price * siim.qty) + (((tm.tax_amount*(ism.selling_price * siim.qty))/100))),0) as TotalPrice 
FROM Periods

**LEFT OUTER JOIN RS_Sell_Order_Master as SM on sm.is_approved >= DATEADD(MM, 1, @LastApril)**
left outer join RS_Sells_Invoice_Info_Master as SIIM  on sm.sell_order_no = SIIM.sell_order_no  
left outer join RS_Inventory_Selling_Master as ISM on ISM.selling_product_id = SIIM.selling_product_id  
left outer join RS_Tax_Master as TM on Tm.tax_id = SIIM.tax_id  
group by 
DATENAME(month, [Period]),
DATEPART(year, [Period]),
MONTH(sm.is_approved)

Can anyone tell me that how to what will be the condition for above query ie bold part of the above query

ie this part I only want this condtion to be true LEFT OUTER JOIN RS_Sell_Order_Master as SM on sm.is_approved >= DATEADD(MM, 1, @LastApril)

Was it helpful?

Solution

I tried to understand your question, but I'm not sure if I understood :

CTE retrieves all months since last April and you want to display informations linked to RS_Sell_Order_Master (Invoices, Inventory and Taxes...). This should mean, you have to display for all months since last April thoses details.

So maybe you just simply need to make match Periods CTE months and years with RS_Sell_Order_Master months and years.

So you just simply needs to do that :

FROM Periods as P
left outer join RS_Sell_Order_Master as SM 
    on MONTH(sm.is_approved) = MONTH(P.[Period])
    and YEAR(sm.is_approved) = YEAR(P.[Period])

Here in the full code :

DECLARE @CurrentDate AS datetime = getdate();
DECLARE @FirstDayInCurrentMonth AS datetime = DATEADD(day, 1 - DATEPART(day, @CurrentDate), @CurrentDate);
DECLARE @LastApril AS datetime = DATEADD(month, -(DATEPART(month, @FirstDayInCurrentMonth) + 8) % 12, @FirstDayInCurrentMonth);

WITH Periods AS (
    SELECT @LastApril AS [Period]
  UNION ALL
    SELECT DATEADD(month, 1, [Period])
    FROM Periods
    WHERE [Period] < @FirstDayInCurrentMonth
)

SELECT 
        DATENAME(month, [Period]),
        DATEPART(year, [Period]),  
        MONTH(sm.is_approved) as [MONTH],
        isnull(sum(ISM.selling_price * siim.qty),0) as Price,  
        isnull(sum(((tm.tax_amount*(ism.selling_price * siim.qty))/100)),0) as Tax,  
        isnull(sum((ism.selling_price * siim.qty) + (((tm.tax_amount*(ism.selling_price * siim.qty))/100))),0) as TotalPrice 
FROM Periods as P
    left outer join RS_Sell_Order_Master as SM 
        on MONTH(sm.is_approved) = MONTH(P.[Period])
        and YEAR(sm.is_approved) = YEAR(P.[Period])
    left outer join RS_Sells_Invoice_Info_Master as SIIM  
        on sm.sell_order_no = SIIM.sell_order_no  
    left outer join RS_Inventory_Selling_Master as ISM 
        on ISM.selling_product_id = SIIM.selling_product_id  
    left outer join RS_Tax_Master as TM 
        on Tm.tax_id = SIIM.tax_id  
group by 
    DATENAME(month, [Period]),
    DATEPART(year, [Period]),
    MONTH(sm.is_approved)

If i'm wrong correct me, or if you have another issue just leave a comment.

OTHER TIPS

I am not sure, but seeing at the tables I think your joins should be as follows:

FROM Periods
LEFT OUTER JOIN RS_Sell_Order_Master as SM on sm.is_approved >= DATEADD(MM, 1, @LastApril)
INNER join RS_Sells_Invoice_Info_Master as SIIM  on sm.sell_order_no = SIIM.sell_order_no  
INNER join RS_Inventory_Selling_Master as ISM on ISM.selling_product_id = SIIM.selling_product_id  
INNER join RS_Tax_Master as TM on Tm.tax_id = SIIM.tax_id 

And Grouping as:

GROUP BY
DATEPART(year, [Period]), 
DATENAME(month, [Period]),
MONTH(sm.is_approved)

Try the code below instead of your first three lines, I basically turned the date into a Date without the time which will probably join onto your periods table properly

DECLARE @CurrentDate AS datetime = CAST(FLOOR(CAST(getdate() AS FLOAT)) AS DATETIME)
DECLARE @FirstDayInCurrentMonth AS datetime = DATEADD(day, 1 - DATEPART(day, @CurrentDate), @CurrentDate)
DECLARE @LastApril AS datetime = DATEADD(month, -(DATEPART(month, @FirstDayInCurrentMonth) + 8) % 12, @FirstDayInCurrentMonth)

first you have to compare between dates e.g. compare Feb-2014 and Apr-2014 if false the you have to subtract one from the year part of April. You can use functions to compare and substring the date and subtract the date.

First thing, the way you generate dates, you should remove the Hour part using:

DATEADD(hour, 0, DATEDIFF(DAY, 0,@CurrentDate))

Second, try using Inner Join instead of left join.

Still I would suggest you give us better idea of how the data is stored. Possibly add the basic schema and test values in the SQL Fiddle

So I can execute the query and get better idea of what you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top