Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 如何在sql中获取最近六个月的数据_Asp.net_Sql_Sql Server - Fatal编程技术网

Asp.net 如何在sql中获取最近六个月的数据

Asp.net 如何在sql中获取最近六个月的数据,asp.net,sql,sql-server,Asp.net,Sql,Sql Server,我正在开发一个图表,我想分别显示上六个月的详细信息。 我想在条形图中显示细节 但我没有得到任何解决方案。 我在sql查询方面有点弱 到现在为止,我得到了上个月的数据。 但我要过去六个月的数据。 这是我的密码 DECLARE @StartDate DATETIME, @EndDate DATETIME SET @StartDate = DATEADD(mm, DATEDIFF(mm,0,getdate())-1, 0) SET @EndDate = DATEADD(mm, 1, @StartDat

我正在开发一个图表,我想分别显示上六个月的详细信息。 我想在条形图中显示细节

但我没有得到任何解决方案。 我在sql查询方面有点弱

到现在为止,我得到了上个月的数据。 但我要过去六个月的数据。 这是我的密码

DECLARE @StartDate DATETIME, @EndDate DATETIME
SET @StartDate = DATEADD(mm, DATEDIFF(mm,0,getdate())-1, 0)
SET @EndDate = DATEADD(mm, 1, @StartDate)

SELECT  sum(quantity)
FROM RS_Sell_Order_Master as s
left outer join RS_Sell_Order_Mapping as sm on sm.sell_order_no = s.sell_order_no 
left outer join RS_GIN_Master as GIN on gin.product_id = sm.product_id 

WHERE del_date BETWEEN @StartDate AND @EndDate
我想要过去六个月的o/p,就像这样假设我上个月卖出10个数量,上个月之前卖出20个数量&我之前没有卖出,所以o/p应该是

null
null
null
null
10
20

像这样的东西

这将在6个月前指定一个日期-似乎没有理由需要返回SqlServer日期时间的零数据:

编辑

假设您希望从每个月的第一个月到最后一个月对齐, 假设因为您有空值,但仍然希望显示数据,我们将需要制造丢失的月份,例如,使用递归CTE

DECLARE @StartDate DATETIME;
SET @StartDate = DATEADD(mm, -6, CURRENT_TIMESTAMP);

WITH cte AS
(
    SELECT 0 AS TheMonth
    UNION ALL
    SELECT TheMonth + 1
    FROM cte
    WHERE TheMonth < 5
)
SELECT sum(quantity)
FROM
    cte 
    LEFT OUTER JOIN RS_Sell_Order_Master as s
    ON del_date >= DATEADD(MM, cte.TheMonth, @StartDate) AND del_date < DATEADD(MM, cte.TheMonth + 1, @StartDate)
    -- Other Joins here
GROUP BY cte.TheMonth
ORDER BY cte.TheMonth;

每月的项目总数可按如下方式计算

select sum(items),left(date,6)+'01' from yourTable
group by left(date,6)

假设您的日期是112格式,并且是varchar8类型,您认为这是在做什么:DATEDIFFmm,0,getdate-1?考虑到这一点,你认为你将如何得到最后6个月而不是最后1个月?你的意思是在本月1日开始查询,还是在今天,6个月前的同一天开始查询?我知道整个查询都是错误的,我不认为我会通过此查询获得所需的输出。我感谢你的努力,斯图尔特。但我要每月数量的总和。假设我上个月卖出了20,上个月卖出了10,前4个月的o/p应该是空的,然后上个月的第二个月的o/p应该是20,上个月的o/p应该是10谢谢你的努力StuartLCUpdated对上6个月的数据进行分组-apols,我上一次更新中有错我在最近2-3天就这么做了。你在2分钟内解决了这个问题。伟大的令人惊叹的保持不变DudIt说的是无效的列名DateDate替换为包含时间值的列的日期。。。我想现在是“每月”。。。试试看
select sum(items),left(date,6)+'01' from yourTable
group by left(date,6)