十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
不知道你碰到那个知识点的问题了!我就给你说说我对SqlServer多个表查询的理解!SqlServer 多个表的统计查询有三种方式1:嵌套查询 2:连接查询3:联合查询 ;(我用表名为student 的表做例子) 嵌套查询一般使用 Select * from student where 或者 having ;连接查询就是将多个表或某些列为条件进行连接,从而查询数据!连接查询分: 交叉连接 内连接 外连接 自连接;联合查询就是得使用union 关键字将两个select语句联合起来,进行数据查询!只要你做过题,你就会明白其中的每种连接方式的优势!希望对你有所帮助!
创新互联专业为企业提供崇仁网站建设、崇仁做网站、崇仁网站设计、崇仁网站制作等企业网站建设、网页设计与制作、崇仁企业网站模板建站服务,10多年崇仁做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
什么数据库啊?
select convert(varchar(7),注册时间,120),count(*) from 表名 group by convert(varchar(7),注册时间,120)以上为sqlserver写法,其他数据库写法可能不同
SQLSERVER 最原始的SQL写法
CREATE TABLE FenShu
(
name nvarchar(500),
cdate date,
je money
)
INSERT INTO FenShu VALUES('语文','2011-5-10',200)
INSERT INTO FenShu VALUES('数学','2011-5-20',600)
INSERT INTO FenShu VALUES('英语','2011-6-10',100)
SELECT name,cdate,je
FROM (
SELECT name,cdate,je,0 AS S1,cdate AS S2
FROM FenShu
UNION ALL
SELECT CAST(cdate AS CHAR(7)),'',SUM(je),1 AS S1,MAX(cdate) AS S2
FROM FenShu
GROUP BY CAST(cdate AS CHAR(7))
)AS T
以下以2013年11月为例
1、使用横向连接,以5天为例,简单但不易扩展
with data as ( select * from yourtable where date='2013-11-01' and date'2013-12-01')
select distinct name
from data t1 join data t2 on t1.name=t2.name and t1.date=t2.date+1
join data t3 on t2.name=t3.name and t2.date=t3.date+1
join data t4 on t3.name=t4.name and t3.date=t4.date+1
join data t5 on t4.name=t5.name and t4.date=t5.date+1
2、使用纵向分组统计
with t1(id,rq) as (
select distinct 人员, date from 表 where date='2013-11-01' and date'2013-12-01' ),
--t1求出指定月的人员编号及不同的打卡日期
t2 as (select s2.* from t1 s1 join t1 s2 on s1.id=s2.id and s1.rq=s2.rq-1),
--t2求出所有上一日也打过卡的日期
t3 as (select * from t1 except select * from t2),
--t3求出所有上一日未打过卡的日期
t as (
select id,rq,1 days from t3
union all
select t1.id,t1.rq,t.days+1 from t1 join t on t1.id=t.id and t1.rq=t.rq+1
)
--t4递归调用,每连续一日days+1,就是求每一打卡时间是连续的第几天
select id
from t
group by id
having max(days)=5
order by id
以上就不删了,以下可以改短点吧
with t as (
select 人员 id, date rq, 1 days from 表 t1
where not exists(select * from 表 t2 where t2.date=t1.date-1)
union all
select t1.id,t1.rq,t.days+1 from 表 t1 join t on t1.id=t.id and t1.rq=t.rq+1
)
select id
from t
group by id
having max(days)=5
order by id
With T
As
(
Select ID,结束月,相差月 From TT
Union All
Select T.ID,Dateadd(M,-1,T.结束月),T.相差月-1 From TT Inner Join T On
TT.id=T.id Where T.相差月1
)
Select ID,结束月 From T
Order By id,结束月
--TT是你的表名
--SQL2005或以上版本
可以完成,思路如下:
通过pid 商品编码分组,得到销售日期的每个月列,后用sum(case Fact_m WHEN 月份 then 数量end)来操作即可.
--年度售额:
select pid 商品编码
,isnull(convert(dec(18,2),sum(case Fact_m WHEN '01' then cCost end)),0) 'countCost_1'
,isnull(convert(dec(18,2),sum(case Fact_m WHEN '02' then cCost end)),0) 'countCost_2'
,isnull(convert(dec(18,2),sum(case Fact_m WHEN '03' then cCost end)),0) 'countCost_3'
,isnull(convert(dec(18,2),sum(case Fact_m WHEN '04' then cCost end)),0) 'countCost_4'
,isnull(convert(dec(18,2),sum(case Fact_m WHEN '05' then cCost end)),0) 'countCost_5'
,isnull(convert(dec(18,2),sum(case Fact_m WHEN '06' then cCost end)),0) 'countCost_6'
,isnull(convert(dec(18,2),sum(case Fact_m WHEN '07' then cCost end)),0) 'countCost_7'
,isnull(convert(dec(18,2),sum(case Fact_m WHEN '08' then cCost end)),0) 'countCost_8'
,isnull(convert(dec(18,2),sum(case Fact_m WHEN '09' then cCost end)),0) 'countCost_9'
,isnull(convert(dec(18,2),sum(case Fact_m WHEN '10' then cCost end)),0) 'countCost_10'
,isnull(convert(dec(18,2),sum(case Fact_m WHEN '11' then cCost end)),0) 'countCost_11'
,isnull(convert(dec(18,2),sum(case Fact_m WHEN '12' then cCost end)),0) 'countCost_12'
,isnull(convert(dec(18,2),sum(cCost))) 'countTotal'
from Tab group by pid
--Fact_m 指的就是销售日期[月],最后还有一列为年度总计
希望能帮到你!