最近2个月的SQL记录 [英] Last 2 month record in SQL

查看:79
本文介绍了最近2个月的SQL记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试这个SQL查询来获取当前总数,也是最近2个月的记录和当前月份记录意味着总共3个月..



我有什么试过:



I try this SQL query to get current total and also last 2 months records and current month record means total 3 months..

What I have tried:

Select distinct tblRv.Owner , 
DATENAME(MONTH,tblRe.StartDate) as [Month],
count(tblvv.VName)
 as currentdata
from tblRe
inner join 
tblRv on tblRe.RID=tblRv.RID
inner join 
 tblvv on tblRv.ID=tblvv.MID
where tblRe.StartDate>=DATEADD(MONTH,-3,GETDATE())
AND tblRe.EndDate<=GETDATE()
AND tblRe.Region='uk'
and tblRv.Owner='roh'
and VName <> ''
group by tblRv.Owner,
DATENAME(MONTH,tblRe.StartDate)
order by tblRv.Owner







此节目我这样的数据




this show me data like this

Owner	Month	currentdata
roh	 July	  3
roh	 June	  10
roh	  May	  6





在我的表中数据是这样的



where as in my table data is like this

Owner	Month	currentdata
roh	 July	  3
roh	 June	  1
roh	  May	  0





格式就像我想要的那样



and formatting like thsi i want

Owner	   july  june may
roh	   3      1     0

推荐答案

你想要 pivot 结果。

在SQL查询中使用数据透视的简单方法 [ ^ ]

Sql Server中的PIVOT和UNPIVOT | SqlHints.com [ ^ ]



[更新]

您的问题是双重问题。

Pivot将从示例2到示例3.

您忘了解释如何更改示例1和示例2之间的数字。解释这些数字的含义。

使用改善问题以更新您的问题。
You want to pivot the result.
Simple Way To Use Pivot In SQL Query[^]
PIVOT and UNPIVOT in Sql Server | SqlHints.com[^]

[Update]
Your question is a double question.
Pivot will do from example 2 to example 3.
You forgot to explain how to change the numbers between example 1 and example 2. Explain the meaning of those numbers.
Use Improve question to update your question.


这篇关于最近2个月的SQL记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆