如何用SQL查询当月销售业绩

ihope6 2008-12-04 01:50:35
有一个表:


yjbiao

id yj userid date
1 5345 1 2008-12-9
2 4234 1 2008-12-5
3 334 3 2008-12-3
4 454 1 2008-10-3
5 234 2 2008-12-1
6 456 1 2008-12-12
7 5345 1 2008-11-9
8 4234 1 2008-12-5
9 334 3 2008-10-3
10 454 1 2008-12-3
11 234 2 2008-12-1
12 456 1 2008-10-12





怎么使用SQL查询其中一个用户当月业绩,

关键是:

1.当个月有时30天有时31天
2.如果用户当天没有业绩(数据库中就没有这天的记录)查询出来也的显示为0,然后按日期排列输出




...全文
869 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
tomkan_jjp 2010-03-26
  • 打赏
  • 举报
回复
根据csdyyr的
再创建临时表,把最终SELECT的数据插入临时表
然后select count(业绩量字段),用户姓名,日期 from 临时表 group by 日期
ihope6 2008-12-04
  • 打赏
  • 举报
回复
csdyyr太强了!!!

已经实现大部分功能!!!

不过怎样实现求和呢?不知道是否是奢求了....

ihope6 2008-12-04
  • 打赏
  • 举报
回复
先谢谢csdyyr大侠,

我还有个疑问:为什么SET @END=@START就是显示当月的数据呢,如果我想实现当天业绩求和的功能,

能提供点思路吗?

谢谢


csdyyr 2008-12-04
  • 打赏
  • 举报
回复
DECLARE  @START SMALLDATETIME, @END SMALLDATETIME
DECLARE @DATE TABLE(COL SMALLDATETIME)

SET @START='2008-12-01'
SET @END=@START

WHILE DATEDIFF(MM,@START,@END)=0
BEGIN
INSERT @DATE SELECT @END
SET @END=DATEADD(DAY,1,@END)
END

SELECT id,col as date, isnull(yj,0) as yj
FROM @DATE left join yjbiao on col=date
ihope6 2008-12-04
  • 打赏
  • 举报
回复
csdyyr兄,第二个写的是对的,

但没有实现当天业绩求和的功能

(我加了半天没有加上,汗!)


csdyyr 2008-12-04
  • 打赏
  • 举报
回复

DECLARE @START SMALLDATETIME, @END SMALLDATETIME
DECLARE @DATE TABLE(COL SMALLDATETIME)

SET @START='2008-12-01'
SET @END=@START

WHILE DATEDIFF(MM,@START,@END)=0
BEGIN
INSERT @DATE SELECT @END
SET @END=DATEADD(DAY,1,@END)
END

SELECT * FROM @DATE
csdyyr 2008-12-04
  • 打赏
  • 举报
回复
DECLARE @DAYS INT
DECLARE @START SMALLDATETIME

SET @DAYS=0
SET @START='2008-12-01'

SET ROWCOUNT @DAYS
SELECT DATEADD(DAY, ID, @START)
FROM (
SELECT TOP 31 ID=(SELECT COUNT(*) FROM SYSOBJECTS WHERE ID<O.ID) FROM SYSOBJECTS AS O ORDER BY ID
) T

SET ROWCOUNT 0
/*
2008-12-01 00:00:00
2008-12-02 00:00:00
2008-12-03 00:00:00
......
2008-12-29 00:00:00
2008-12-30 00:00:00
2008-12-31 00:00:00
*/
ihope6 2008-12-04
  • 打赏
  • 举报
回复
dawugui写的有点开窍了,中间可否用For循环呢? 1循环到31?
dawugui 2008-12-04
  • 打赏
  • 举报
回复
select m.riqi , isnull(n.yj , 0) yj from
(
select '2008-12-01' riqi union all
select '2008-12-02' riqi union all
...
select '2008-12-31' riqi
) m left join yjbiao n
on m.riqi = n.date

ihope6 2008-12-04
  • 打赏
  • 举报
回复
其实大家说的我都知道,不过组合起来就不会了,唉,谁能把上面的想法组合到一起呢
「已注销」 2008-12-04
  • 打赏
  • 举报
回复
1.在查询结果中显示列名:
  a.用as关键字:select name as '姓名' from students order by age
  b.直接表示:select name '姓名' from students order by age
  2.精确查找:
  a.用in限定范围:select * from students where native in ('湖南', '四川')
  b.between...and:select * from students where age between 20 and 30
  c.“=”:select * from students where name = '李山'
  d.like:select * from students where name like '李%' (注意查询条件中有“%”,则说明是部分匹配,而且还有先后信息在里面,即查找以“李”开头的匹配项。所以若查询有“李”的所有对象,应该命令:'%李%';若是第二个字为李,则应为'_李%'或'_李'或'_李_'。)
  e.[]匹配检查符:select * from courses where cno like '[AC]%' (表示或的关系,与"in(...)"类似,而且"[]"可以表示范围,如:select * from courses where cno like '[A-C]%')
  3.对于时间类型变量的处理
  a.smalldatetime:直接按照字符串处理的方式进行处理,例如:
select * from students where birth > = '1980-1-1' and birth <= '1980-12-31'
  4.集函数
  a.count()求和,如:select count(*) from students (求学生总人数)
  b.avg(列)求平均,如:select avg(mark) from grades where cno=’B2’
  c.max(列)和min(列),求最大与最小
  5.分组group
  常用于统计时,如分组查总数:
select gender,count(sno)
from students
group by gender
(查看男女学生各有多少)
  注意:从哪种角度分组就从哪列"group by"
  对于多重分组,只需将分组规则罗列。比如查询各届各专业的男女同学人数 ,那么分组规则有:届别(grade)、专业(mno)和性别(gender),所以有"group by grade, mno, gender"
select grade, mno, gender, count(*)
from students
group by grade, mno, gender
  通常group还和having联用,比如查询1门课以上不及格的学生,则按学号(sno)分类有:
select sno,count(*) from grades
where mark<60
group by sno
having count(*)>1
  6.UNION联合
  合并查询结果,如:
SELECT * FROM students
WHERE name like ‘张%’
UNION [ALL]
SELECT * FROM students
WHERE name like ‘李%’
  7.多表查询
  a.内连接
select g.sno,s.name,c.coursename
from grades g JOIN students s ON g.sno=s.sno
JOIN courses c ON g.cno=c.cno
(注意可以引用别名)
b.外连接
b1.左连接
select courses.cno,max(coursename),count(sno)
from courses LEFT JOIN grades ON courses.cno=grades.cno
group by courses.cno
  左连接特点:显示全部左边表中的所有项目,即使其中有些项中的数据未填写完全。
  左外连接返回那些存在于左表而右表中却没有的行,再加上内连接的行。
  b2.右连接
  与左连接类似
  b3.全连接
select sno,name,major
from students FULL JOIN majors ON students.mno=majors.mno
  两边表中的内容全部显示
  c.自身连接
select c1.cno,c1.coursename,c1.pno,c2.coursename
from courses c1,courses c2 where c1.pno=c2.cno
  采用别名解决问题。
  d.交叉连接
select lastname+firstname from lastname CROSS JOIN firstanme
  相当于做笛卡儿积
  8.嵌套查询
  a.用关键字IN,如查询李山的同乡:
select * from students
where native in (select native from students where name=’ 李山’)
  b.使用关键字EXIST,比如,下面两句是等价的:
select * from students
where sno in (select sno from grades where cno=’B2’)
select * from students where exists
(select * from grades where
grades.sno=students.sno AND cno=’B2’)
  9.关于排序order
  a.对于排序order,有两种方法:asc升序和desc降序
  b.对于排序order,可以按照查询条件中的某项排列,而且这项可用数字表示,如:
select sno,count(*) ,avg(mark) from grades
group by sno
having avg(mark)>85
order by 3
  10.其他
  a.对于有空格的识别名称,应该用"[]"括住。
  b.对于某列中没有数据的特定查询可以用null判断,如select sno,courseno from grades where mark IS NULL
  c.注意区分在嵌套查询中使用的any与all的区别,any相当于逻辑运算“||”而all则相当于逻辑运算“&&”
  d.注意在做否定意义的查询是小心进入陷阱:
  如,没有选修‘B2’课程的学生 :
select students.*
from students, grades
where students.sno=grades.sno
AND grades.cno <> ’B2’
  上面的查询方式是错误的,正确方式见下方:
select * from students
where not exists (select * from grades
where grades.sno=students.sno AND cno='B2')
  11.关于有难度多重嵌套查询的解决思想:
  如,选修了全部课程的学生:
select *
from students
where not exists ( select *
from courses
where NOT EXISTS
(select *
from grades
where sno=students.sno
AND cno=courses.cno))
  最外一重:从学生表中选,排除那些有课没选的。用not exist。由于讨论对象是课程,所以第二重查询从course表中找,排除那些选了课的即可。
「已注销」 2008-12-04
  • 打赏
  • 举报
回复
select count(*) from yj

這個是求和語句
「已注销」 2008-12-04
  • 打赏
  • 举报
回复
select *
from yjbiao
where date > = '2008-1-1' and date <= '2008-1-31'

(如果用户当天没有业绩(数据库中就没有这天的记录)查询出来也的显示为0,然后按日期排列输出)
如果表里沒有業績為0的是顯然查不到的
至於要算總不太清楚 Excel裡面有這功能 SQL沒用過
csdyyr 2008-12-04
  • 打赏
  • 举报
回复
用辅助表left join yjbiao
ihope6 2008-12-04
  • 打赏
  • 举报
回复
如果一个用户当天有一条以上的数据必须相加后输出的。

比如我要查询用户1,12月份业绩总表




rq 当天业绩总和
1 4234
2 534
3 0
4 4234
. 6456
. 0
. 4234
31 423423

「已注销」 2008-12-04
  • 打赏
  • 举报
回复
按照時間查找
然後只顯示31項或30項

select top(30)*
from yjbiao
order by date desc
ihope6 2008-12-04
  • 打赏
  • 举报
回复
也就是说当天没有记录也得填充为0,然后按当月的日期1~31/30顺序显示输出。

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧