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 类别, 型号