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"));