sql语句 ( 积分: 100 )

  • 主题发起人 主题发起人 wy91267
  • 开始时间 开始时间
W

wy91267

Unregistered / Unconfirmed
GUEST, unregistred user!
select&nbsp;bz&nbsp;,sum(worknum)&nbsp;worknum&nbsp;from&nbsp;accv_worktime4<br>where&nbsp;worknum&gt;0&nbsp;and&nbsp;workdate&gt;='2007-09-01'&nbsp;&nbsp;&nbsp;and&nbsp;&nbsp;workdate&lt;='2007-09-21'<br>group&nbsp;by&nbsp;bz&nbsp;order&nbsp;by&nbsp;worknum&nbsp;desc<br>这个代码是算出&nbsp;&nbsp;2007-09-01到2007-09-31这个时间段&nbsp;bz&nbsp;这个字段的&nbsp;合计&nbsp;worknum<br>但是我想在要个字段workdate,我怎么写SQL语句呢?不能下面这样写&nbsp;&nbsp;算出来的合计就不对了<br>select&nbsp;workdate,bz&nbsp;,sum(worknum)&nbsp;worknum&nbsp;from&nbsp;accv_worktime4<br>where&nbsp;worknum&gt;0&nbsp;and&nbsp;workdate&gt;='2007-09-01'&nbsp;&nbsp;&nbsp;and&nbsp;&nbsp;workdate&lt;='2007-09-21'<br>group&nbsp;by&nbsp;workdate,bz&nbsp;order&nbsp;by&nbsp;worknum&nbsp;desc
 
select&nbsp;a1.bz&nbsp;,sum(a1.worknum)&nbsp;worknum,<br>&nbsp;(select&nbsp;workdate&nbsp;from&nbsp;accv_worktime4&nbsp;a2&nbsp;where&nbsp;a2.bz=a1.bz)&nbsp;work_date<br>from&nbsp;accv_worktime4&nbsp;a1<br>where&nbsp;a1.worknum&gt;0&nbsp;and&nbsp;a1.workdate&gt;='2007-09-01'&nbsp;and&nbsp;a1.workdate&lt;='2007-09-21'<br>group&nbsp;by&nbsp;bz&nbsp;order&nbsp;by&nbsp;worknum&nbsp;desc
 
你的方法不行&nbsp;报错<br>提示“子查询返回的值多于一个”
 
select&nbsp;bz&nbsp;,sum(worknum)&nbsp;worknum,&nbsp;min(workdate)&nbsp;as&nbsp;workdate&nbsp;from&nbsp;accv_worktime4<br>where&nbsp;worknum&gt;0&nbsp;and&nbsp;workdate&gt;='2007-09-01'&nbsp;and&nbsp;&nbsp;workdate&lt;='2007-09-21'<br>group&nbsp;by&nbsp;bz&nbsp;order&nbsp;by&nbsp;worknum&nbsp;desc<br>既要取值又不group&nbsp;by,那么可能一行对应多个值,只能取其中一个
 
你的表应该一个主键,假定是&nbsp;ID:<br>select&nbsp;a1.bz&nbsp;,sum(a1.worknum)&nbsp;worknum,<br>&nbsp;(select&nbsp;workdate&nbsp;from&nbsp;accv_worktime4&nbsp;a2&nbsp;where&nbsp;a2.ID=a1.ID)&nbsp;work_date<br>from&nbsp;accv_worktime4&nbsp;a1<br>where&nbsp;a1.worknum&gt;0&nbsp;and&nbsp;a1.workdate&gt;='2007-09-01'&nbsp;and&nbsp;a1.workdate&lt;='2007-09-21'<br>group&nbsp;by&nbsp;bz&nbsp;order&nbsp;by&nbsp;worknum&nbsp;desc
 
ysai的方法到是可以。但是还是不是我想要的。<br>其实'2007-09-01'&nbsp;和'2007-09-21'&nbsp;是两个wwDBDateTimepicker&nbsp;&nbsp;开始时间wwDBDateTimepicker1&nbsp;和结束时间wwDBDateTimepicker2&nbsp;&nbsp;显示bz、worknum,workdate1,workdate2&nbsp;&nbsp;显示这四个字段&nbsp;&nbsp;但是显示的workdate1,workdate2&nbsp;不是我的录入的'2007-09-01'&nbsp;和'2007-09-21'&nbsp;&nbsp;怎么做呢?&nbsp;<br>select&nbsp;bz&nbsp;,sum(worknum)&nbsp;worknum,&nbsp;min(workdate)&nbsp;as&nbsp;workdate1,MAX(workdate)&nbsp;as&nbsp;workdate2&nbsp;from&nbsp;accv_worktime4<br>where&nbsp;worknum&gt;0&nbsp;and&nbsp;workdate&gt;='2007-09-01'&nbsp;and&nbsp;&nbsp;workdate&lt;='2007-09-21'<br>group&nbsp;by&nbsp;bz&nbsp;order&nbsp;by&nbsp;worknum&nbsp;desc
 
不是很明楼主的意思,不知是不是要这样的效果?<br>select&nbsp;bz&nbsp;,sum(worknum)&nbsp;worknum,workdate1='2007-09-01',workdate2='2007-09-21'&nbsp;from&nbsp;accv_worktime4<br>where&nbsp;worknum&gt;0&nbsp;and&nbsp;workdate&gt;='2007-09-01'&nbsp;and&nbsp;&nbsp;workdate&lt;='2007-09-21'<br>group&nbsp;by&nbsp;bz&nbsp;order&nbsp;by&nbsp;worknum&nbsp;desc
 
来晚了,同意&nbsp;lee19841218&nbsp;<br>楼主只需把固定的日期换成你时间控件wwDBDateTimepicker1和wwDBDateTimepicker2<br>的日期就能实现随机查任意两个日期间的数据了
 
可以这样写试下<br>select&nbsp;workdate,bz&nbsp;,sum(worknum)&nbsp;worknum&nbsp;from&nbsp;accv_worktime4<br>where&nbsp;worknum&gt;0&nbsp;and&nbsp;workdate&gt;='2007-09-01'&nbsp;&nbsp;&nbsp;and&nbsp;&nbsp;workdate&lt;='2007-09-21'<br>group&nbsp;by&nbsp;left(workdate,1),bz&nbsp;order&nbsp;by&nbsp;worknum&nbsp;desc&nbsp;&nbsp;<br><br>试下看&nbsp;呵呵~&nbsp;&nbsp;只要不跨千年都没的问题
 
to&nbsp;lee19841218,<br>select&nbsp;bz&nbsp;,sum(worknum)&nbsp;worknum,workdate1='2007-09-01',workdate2='2007-09-21'&nbsp;from&nbsp;accv_worktime4<br>where&nbsp;worknum&gt;0&nbsp;and&nbsp;workdate&gt;='2007-09-01'&nbsp;and&nbsp;&nbsp;workdate&lt;='2007-09-21'<br>group&nbsp;by&nbsp;bz&nbsp;order&nbsp;by&nbsp;worknum&nbsp;desc<br>这是你写的代码&nbsp;&nbsp;我表中没有workdate1和workdate2字段,我只有workdate这个字段。在SQL中显示四个字段&nbsp;&nbsp;bz、worknum,workdate1,workdate2&nbsp;&nbsp;显示这四个字段&nbsp;&nbsp;workdate1和workdate2为时间段,开始时间和结束时间
 
没有也可以显示的
 
你用的什么数据库?<br>为什么不用&nbsp;between????
 
&quot;字段workdate&quot;干什么用呀?是什么意义?楼主你要想清楚。<br>既然是一段时间内的sum求和,&quot;字段workdate&quot;意味着什么?<br>逻辑上就有问题,SQL自然通不过。[:)]
 
后退
顶部