1. sql語句怎樣使查詢同一表同一列多條數據分列顯示(在線等)
select編碼,名稱,sum(金額),sum(數量)from表1groupby編碼,名稱
2. SQL語句怎樣使查詢同一表同一列多條數據分列顯示
declare @sql varchar(8000)
set @sql = 'select 編碼,名稱'
select @sql = @sql+', max(case [項目序號] when '''+cast(項目序號 as varchar)+''' then 數額 else 0 end ) AS [項目序號'+cast(項目序號 as varchar)+']'
from (select distinct 項目序號 from 表名) as a
set @sql = @sql +' from 表名 group by 編碼,名稱'
select @sql
exec (@sql)
3. SQL 怎樣將查詢出某列的多行數據,變為一行顯示 通過SQL語句查詢出結果 AutoID cT
你好,你看看參考一下。
DECLARE@tTABLE(idint,valuevarchar(10))
INSERT@tSELECT1,'出國'
UNIONALLSELECT2,'回國'
SELECT[values]=STUFF((REPLACE(REPLACE((SELECTvalueFROM@tFORXMLPATH('')),'<value>',','),'</value>','')),1,1,'')
4. sql怎麼將一條記錄的欄位分行顯示
select a,b from
(select id,1 as idx,』id' as a,cast(id as nvarchar(250)) as b
union all
select id,2 as idx,'name『 as a,cast(name as nvarchar(250)) as b
unioin all
select id,3 as idx,'age『 as a,cast(age as nvarchar(250)) as b) as table1
order by id,idx
表有多少欄位可通過查詢系統表拼接字元串的方式執行以上類似sql語句
5. sql語句怎麼樣一次性查詢多個條件,並分列顯示
方法一,分別查詢出來,結果再關聯
selectfnum1,fnum2from
(selectcount(*)asfnum1from表名wherea=2andb=3)t1,
(selectcount(*)asfnum2from表名wherea=3andb=5)t2
方法二
selectsum(casewhena=2andb=3then1else0end)asfnum1,
sum(casewhena=3andb=5then1else0end)asfnum2
from表名
wherea=2andb=3
ora=3andb=5
有問題請追問
6. SQL查詢相同的類別求和
採集group by和sum即可。
select 類別, 型號, sum(數量) as 總數量
from table1
group by 類別, 型號
order by 類別, 型號