求SQL查询语句

问题描述

我想求出这样的结果,同一张单据(id相同为同一张)利润unitCost*inQuantity-unitCost*outQuantity最后的结果期望是这样的sn id pricesDB-20111129-61644 1042 50DB-20111129-81334 1043 -400原记录与附件中的图片一样谢谢! 问题补充:huoyj 写道

解决方案

select sn,id, sum(unitCost*inQuantity-unitCost*outQuantity) prices from t_v group by rollup(sn,id);select sn,id, sum(unitCost*inQuantity-unitCost*outQuantity) prices 这里就是查询sn,id 计算利润,as prices是给利润列起个别名叫prices,sum计算的时候是计算一个组里的所有的和,也就是group by分到一个组里的。group by rollup(sn,id); --由sn和id联合分组,即如果两条记录的sn和id相同,那就分到同一个组里面。
解决方案二:
select sn,id,sum(unitCost*inQuantity-unitCost*outQuantity) prices --prices应该是利润吧from XXXXX --不知道你的表名group by rollup(sn,id);写了一个,你看看符合要求嘛

时间: 2024-08-02 04:28:56

求SQL查询语句的相关文章

图片-qiushou求SQL查询语句问题, SQL语句忘记怎么写了

问题描述 qiushou求SQL查询语句问题, SQL语句忘记怎么写了 第一列是-主键-地区-销售人员-销售总额, 请问查询-每个地区销售金额最少的人是谁 解决方案 select * from ( select rank() over(partition by region order by total) as rid ,orderid,region,sales,total from salesorder ) t where t.rid = 1 解决方案二: select * from sale

sql-数据库 SQL查询语句 简化语句

问题描述 数据库 SQL查询语句 简化语句 请问 怎样从上面的数据表中查询出下面表的效果?求大神指教 解决方案 group by month 解决方案二: select months 月份type1+type2+type3 总数case when type1>0 then 1 else 0 then 执照1case when type2>0 then 1 else 0 then 执照2case when type3>0 then 1 else 0 then 执照3from(select

hibernate-web后台的sql查询语句问题

问题描述 web后台的sql查询语句问题 我使用spring+hibernate的框架做的后台,在dao层中sql查询语句查询所有select * from tbplayer t1 where nickname='"+shopID+"'就可以查出结果,然后在拼接显示自己想要的内容,但是一旦换成查某个字段select p3_account from tb_player t1 where nickname='"+shopID+"'就报错,说没有psn这列,但是我的语句中

mysql 查询指定日期时间内容sql查询语句

mysql教程 查询指定日期时间内容sql查询语句 在数据库教程设计时必须注意时间字段最好为int(4)这样,保存在数据库的时一个数字型日期时间截,我们可以用mktime函数求出当前日期的时间截进行加减就OK了,下面看实例 $time = time();   //一个月 $lastMonth = mktime(date('h'),date('i'),date('s'),date('m')-1,date('d'),date('y')); $where .= "  dtime < $lastM

注释-oracle sql查询语句优化

问题描述 oracle sql查询语句优化 各位大神,我想问下在生产PLSQL,将****内的注释以后,可以查询出结果(用时:16S秒),否则,SQL就一直处于执行状态,查询不出结果.在线等,求各位大神们解惑 select cs.P_VOICEDIAL_FLAG PVoicedialFlag cs.pri_card_nbr priCardNbr cs.total_bal totalBal cs.overdue_amount totalOdue cs.risk_score riskScore ci

select-oracle sql查询语句优化

问题描述 oracle sql查询语句优化 select moni.p_code,moni.department,moni.monitorserialno,moni.adminname,moni.buildingname from t_monitorroom moni where moni.p_code in (select roleauth.monitorroomid from t_monitorroom_role_authority roleauth left join t_monitorr

SQL查询语句 group by后, 字符串合并

原文:SQL查询语句 group by后, 字符串合并 合并列值 --******************************************************************************************* 表结构,数据如下: id value ----- ------ 1 aa 1 bb 2 aaa 2 bbb 2 ccc 需要得到结果: id values ------ ----------- 1 aa,bb 2 aaa,bbb,ccc 即:gr

SQL查询语句对象化的实现(C#)

对象|语句   在开发数据库应用的过程难免会编写大量的SQL语句,其中大部份是查询语句:为不同情况编写查询语句是一件很烦琐的事件.用过hibernate或Nhibernate会了解到把SQL查询语句对象化后使用起非常方便和快捷:也大大减少在编写查询SQL语句所带来的错误等问题.        前段时间在编写一个数据处理类的时候同样遇到这个问题,经过一段时间思考和设计现实现了SQL查询语句对象化的功能:在这里我把自己小小的成果共享一下. 在讲解前先看几个例子(数据是SQLServer的Northw

SQL查询语句中的bool类型字段值的写法

  SQL查询语句中的bool类型字段值的写法 没有系统地看过SQL语句的写法说明,只是看了一些常用SQL语句的例子.今天写了条select * from table where sex='true',老是提示标准数据类型不匹配(我用的Access),检查了半天,原来sex字段作为bool(是/否)类型,在SQL语句中其值不需要用''引起来.