如何从SQL获取4月到现在的数据

时间:2014-05-14 12:47:44

标签: asp.net sql sql-server-2008

我正在从事金融领域工作。所以我想要4月到5月的数据。

假设当月是6月那么我只想要今年4月到6月,它应按年和月分组。

如果假设当月是2月,那么我想要去年4月到1月的详细信息,即2013年4月到2014年1月

我通过CTE得到了解决方案如果我只运行CTE部分查询它的工作完全正常 这部分查询..

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

但我希望 sm.is_approved cloumn也可以像上面的查询一样工作。当我用上面的CTE表达式jon其他表。我希望通过 sm.is_approved 列进行适当的分组。

我唯一怀疑加入另一张桌子的条件是什么。 请为查询的以下粗体部分提供适当的条件。

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)

任何人都可以告诉我如何以上查询的条件,即上述查询的粗体部分

即这部分我只想要这个条件是真的   LEVE OUTER JOIN RS_Sell_Order_Master作为SM on sm.is_approved&gt; = DATEADD(MM,1,@ LastApril)

5 个答案:

答案 0 :(得分:2)

我试图理解你的问题,但我不确定我是否明白:

CTE检索自去年四月以来的所有月份,并且您希望显示与RS_Sell_Order_Master(发票,库存和税收......)相关联的信息。这应该意味着,你必须显示自去年四月以来所有月份的细节。

所以,也许你只需要在Periods个月和几年的时间里匹配RS_Sell_Order_Master CTE月份和年份。

所以你只需要这样做:

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])

这里有完整的代码:

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)

如果我错了,或者如果你有其他问题,请发表评论。

答案 1 :(得分:0)

我不确定,但在桌子上看到我认为您的联接应该如下:

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 

分组为:

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

答案 2 :(得分:0)

尝试使用下面的代码而不是前三行,我基本上将日期转换为没有时间的日期,这可能会正确地加入到您的期间表中

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)

答案 3 :(得分:0)

首先你必须比较日期,例如比较2014年2月和2014年4月,如果错误,您必须从4月份的一年中减去一个。 您可以使用函数来比较和子串日期并减去日期。

答案 4 :(得分:-1)

首先,您生成日期的方式,您应该使用以下方法删除小时部分:

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

其次,尝试使用Inner Join而不是left join。

我仍然建议您更好地了解数据的存储方式。可能会在SQL Fiddle

中添加基本架构和测试值

因此,我可以执行查询并更好地了解您的需求。