使用group by name 来进行分组
解决方案 »
- 聚簇索引和非聚簇索引插入的时候哪个更费时间?
- win7使用odbc连接SQL server2008数据库
- 字段x为datetime类型,查询where x='2009-7-4'的sql语句如何写??
- 请问如何在一个表里Table1,查找ID列相同的纪录?
- 这样的sql 怎样写
- 请问要SQL中怎么用BCP将来表中的数据转换成.txt文件?
- 初级问题
- 100分相送...在SQL Server 7.0为何不能实现远程访问(利用外部IP)???
- 如何避免多用户同时更改数据库?
- 在一块新硬盘上创建sql数据库失败,提示扇区大小原因
- 关于sql2008的基本使用,师兄师姐们指导一下,谢谢了
- 关于客户端访问服务端的数据传输方式疑问
我这边不能操作到数据库 无法去写存错过程或重编视图 很少做sql这类的项目 一时间找不到解决办法
自己知道每列名 nameA1 nameA2 nameB1 nameB2
18 20 22 16
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2014-03-03 10:21:28
-- Version:
-- Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64)
-- Dec 28 2012 20:23:12
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: )
--
----------------------------------------------------------------
--> 测试数据:[test]
if object_id('[test]') is not null drop table [test]
go
create table [test]([name] varchar(1),[time] varchar(4),[money] int)
insert [test]
select 'a','上月',18 union all
select 'a','本月',20 union all
select 'b','上月',22 union all
select 'b','本月',16 union all
select 'c','上月',10 union all
select 'c','本月',5
--------------开始查询--------------------------
declare @s nvarchar(4000)
set @s=''
Select @s=@s+','+quotename('Money')+'=max(case when [name]='+quotename([name],'''')+' and [time]='+quotename([time],'''')+' then [money] else 0 end)'
from [test] group by [name],[time]
exec('select [name],[time]'+@s+' from [test] where name=''a'' and time=''上月'' group by [name],[time]')----------------结果----------------------------
/*
name time Money Money Money Money Money Money
---- ---- ----------- ----------- ----------- ----------- ----------- -----------
a 上月 0 18 0 0 0 0
*/
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2014-03-03 10:21:28
-- Version:
-- Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64)
-- Dec 28 2012 20:23:12
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: )
--
----------------------------------------------------------------
--> 测试数据:[test]
if object_id('[test]') is not null drop table [test]
go
create table [test]([name] varchar(1),[time] varchar(4),[money] int)
insert [test]
select 'a','上月',18 union all
select 'a','本月',20 union all
select 'b','上月',22 union all
select 'b','本月',16 union all
select 'c','上月',10 union all
select 'c','本月',5
--------------开始查询--------------------------
declare @s nvarchar(4000)
set @s=''
Select @s=@s+','+quotename('name'+name+CAST(ROW_NUMBER()OVER(PARTITION BY name ORDER BY getdate()) AS VARCHAR))+'=max(case when [name]='+quotename([name],'''')+' and [time]='+quotename([time],'''')+' then [money] else 0 end)'
from [test] group by [name],[time]
exec('select [name],[time]'+@s+' from [test] where name=''a'' and time=''上月'' group by [name],[time]')----------------结果----------------------------
/*
name time namea1 namea2 nameb1 nameb2 namec1 namec2
---- ---- ----------- ----------- ----------- ----------- ----------- -----------
a 上月 0 18 0 0 0 0*/
你这样也太复杂了 老板一直让我尽量别用函数 或许客户那边不支持 我看了别人给我这样的 select a.money+''+b.money+''+c.money... from ((select money from where )as a ,(select money from where )as b...)
可行吗