1. sql查询一个表里面相同的列,并且求这些和
select
a
from
dbo.table1
where
id=0001
union
all
select
a
from
dbo.table2
where
id=0001
union
all
select
a
from
dbo.table3
where
id=0001
union
all
select
a
from
dbo.table4
where
id=0001
a代表列名。
如果要显示多列
就写多个列名。但是这几个表列的数量和排序一定要一样。
列名可以不一样。
默认查出来按第一个查询中的列名显示。
2. sql语句查询出结果如何求一列的和啊
select sum(字段名) as 变量名 from tables
调用sum和使用 变量名 就可以了。
3. SQL列与列之间如何求和
你需要对这个结果产生的表再执行一次GROUPBY操作。
主要是这个语句中你已经对Book.Price,Consumer.Discount,Book.WholeSell执行分组操作了,因此对于这单个字段计算的到的结果肯定是唯一的,所以你计算sum也是对一个记录计算,当然一样了
SELECTBook.BID,BName,Price*DiscountAS[Money],WholeSell,Sold.CID,SUM(Price*Discount-WholeSell)AS[AllProfit]
FROMBook,Sold,Consumer
WhereBook.BID=Sold.BIDANDConsumer.CID=Sold.CIDANDTimebetweendateadd(dd,-30,getdate())andgetdate()
GROUPBYSold.CID,Book.BName,Book.BID
4. sql 查询当月某列值的总和
select sum(downcount)
from 表
where date between getdate()-day(getdate())+1 and getdate()+32-day(getdate()+32)
这样能保证用上date列建的索引
5. sql语句怎么查询一列数据的总和
MS-SQL中求和如下:
1、select sum (foamt) from t_ACRD_GthMst
2、select sum (foamt) from t_ACPD_PayMst
t_ACRD_GthMst和t_ACPD_PayMst表示某ERP系统中的两个表,foamt表示要求和的一例。
(5)sql查询一列的和扩展阅读:
常见语句
更新:update table1 set field1=value1 where 范围
查找:select * from table1 where field1 like ’%value1%’ (所有包含‘value1’这个模式的字符串)
排序:select * from table1 order by field1,field2 [desc]
求和:select sum(field1) as sumvalue from table1
平均:select avg(field1) as avgvalue from table1
最大:select max(field1) as maxvalue from table1
最小:select min(field1) as minvalue from table1[separator]
6. PL/SQL 查询 某列合计
if exists (select 1 from sysobjects where name='test1')
drop table test1
go
create table test1
(
a varchar(10),
b int
)
insert into test1 select '好','1 '
insert into test1 select '很好','2 '
insert into test1 select '非常好','3 '
SELECT V1.a, V2.b
FROM (SELECT TOP (1) a
FROM dbo.test1) AS V1 CROSS JOIN
(SELECT SUM(b) AS b
FROM dbo.test1 AS test1_1) AS V2
结果:
a b
好 6
7. SQL 如何查询 列名1列到列名5 列之间所有数值的和
假如表名为table1,字段列名为cid1,cid2,cid3,cid4,cid5
则:
列名1列到列名5 列之间所有数值的和 为:
select cid1+cid2+cid3+cid4+cid5 as total from table1
8. sql列统计求和
这个方法看起来步骤多 其实蛮简单的。 希望采纳
9. sql如何列统计求和
有个思路:
1、在系统表中找出表名对应的列名,并把每个列名都加上SUM()
select 'sum('+name+'),' from syscolumns
where id=(select id from sysobjects where name='表名')
2、把查询结果复制出来,前面加select 后面加 from 表名。。。。你懂的
注意:复制出来后把最后一个逗号去掉。
3、执行查询
也可以写个存储过程来完成。
10. SQL获取同一列的数值之和应该怎么写呢
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Data;
usingSystem.Data.SqlClient;
///<summary>
///执行查询首行首列的SQL语句
///</summary>
///<paramname="sql">要执行的SQL语句-string</param>
///<returns>返回字符串-string</returns>
publicstaticstringExecuteSql_string(stringsql)
{
stringstr="";
SqlConnectioncon=newSqlConnection("DRIVER=SQLServer;SERVER=192.168.1.201;UID=sa;PWD=;DATABASE=ku");
try
{
OpenCon(con);
SqlCommandcmd=newSqlCommand();
cmd.Connection=con;
cmd.CommandText=sql;
str=Convert.ToString(cmd.ExecuteScalar());
}
catch(Exceptionerr)
{
thrownewException(err.Message);
}
finally
{
CloseCon(con);
}
returnstr;
}
MsgBOX
MsgBOX.Show(ExecuteSql_string("selectsum(lie)frombiao"));