谁能帮我写这个sql语句啊?

jiang10000 2004-01-30 05:06:14
字段名:
intime outtime begstation carnum type gross
2004-01-20 2004-01-25 ss 1 玉米 20
2004-01-20 2004-01-26 ss 1 玉米 10
2004-01-21 2004-01-30 ss 1 玉米 30
我想统计出1月份总共有多少车皮出库,因为第一二是同一个车皮分两次出库,所以只能算一个车皮,那么统计出来的数据就应该是2,我现在就是想请教一下这个sql语句该怎么写呢?只有intime和carnum才能唯一确定一个车皮.
...全文
25 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
ufan 2004-01-31
  • 打赏
  • 举报
回复
上一语句写错一点,应如下:
select intime,carnum,count(*) from tablename where DATEPART(month,outtime) = 1 group by intime,carnum
ufan 2004-01-30
  • 打赏
  • 举报
回复
select intime,carnum,count(*) from tablename group by intime,carnum where DATEPART(month,outtime) = 1
1ssp 2004-01-30
  • 打赏
  • 举报
回复
不好意思,上面的有的问题!
create table t1
(intime datetime, outtime datetime, carnum int, type char(10))
go
insert into t1 values('2004-01-20', '2004-01-25', 1, '玉米')
insert into t1 values('2004-01-20', '2004-01-25', 1, '玉米')
insert into t1 values('2004-01-21', '2004-01-25', 1, '玉米')
select num=sum(carnum) from
(select distinct intime ,type,carnum from t1) a

go
drop table t1

num
-----------
2

(所影响的行数为 1 行)

leeboyan 2004-01-30
  • 打赏
  • 举报
回复
看不太懂:
select carnum into table2 from table1 group by carnum where month(outtime)=1
select count(carnum) from table2
drop table table2
1ssp 2004-01-30
  • 打赏
  • 举报
回复
你试试!
select a.intime,a.type ,num=sum(carnum) from
(select distinct intime ,type,carnum from tablename) a
group by a.intime,a.type
愉快的登山者 2004-01-30
  • 打赏
  • 举报
回复
select sum(1) from
(select distinct intime, carnum from tablename) as A
伍子V5 2004-01-30
  • 打赏
  • 举报
回复
select count(1) count from tablename group by intime,carnum

select sum(gross) gross from tablename group by intime,carnum

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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