Sql Server 查询多行并一行
干货
CREATE TABLE #benefit_code21 (id INT,
number nvarchar(MAX),
pname nvarchar(255),
collegeID INT,
applicationdate datetime,
authorizedate datetime,
lawState int,
lawTime datetime,
country nvarchar(255),
category nvarchar(255),
mainCategoryNumber nvarchar(255),
allCategoryNumber nvarchar(255),
typess int,
levelss int,
Introduction nvarchar(MAX),
AgencyID int,
Agent nvarchar(255),
Remark nvarchar(max),
EName nvarchar(255),
Paname nvarchar(255),
Cont nvarchar(255),
Depname nvarchar(255),
name nvarchar(255));
insert into #benefit_code21(id,
number,
pname,
collegeID,
applicationdate,
authorizedate,
lawState,
lawTime,
country,
category,
mainCategoryNumber
,allCategoryNumber,
typess,
levelss,
Introduction,
AgencyID,
Agent
,Remark
,EName
,Paname
,Cont
,Depname,name)
SELECT
Patent.ID,
Patent.Number,
Patent.Name,
Patent.CollegeID
, Patent.ApplicationDateTime
, Patent.AuthorizeDateTime
, Patent.LawState
, Patent.LawStateTime
, Patent.Country
, Patent.Category
, Patent.MainCategoryNumber
, Patent.AllCategoryNumber
, Patent.Type
, Patent.Level
, Patent.Introduction
, Patent.AgencyID
, Patent.Agent
, Patent.Remark
,Expert.Name
,PatentAgency.Name,
PatentAgency.Contract,
Department.Name,
STUFF(
(
SELECT ';' + CAST(name AS VARCHAR(MAX))
FROM dbo.PatentInventer
WHERE (Patent.ID = PatentInventer.PatentID)
FOR XML PATH ('')
)
,1,1,'')
AS NameValues
FROM dbo.PatentInventer ,dbo.Patent,Expert,PatentAgency,Department
where Patent.ID = PatentInventer.PatentID
and PatentAgency.ID=patent.AgencyID
and patent.CollegeID=Department.ID
and PatentInventer.ExpertID = Expert.ID
and PatentInventer.IsPrincipal = 'True'
GROUP BY Patent.ID,
Patent.Number,
Patent.Name,
Patent.CollegeID
, Patent.ApplicationDateTime
, Patent.AuthorizeDateTime
, Patent.LawState
, Patent.LawStateTime
, Patent.Country
, Patent.Category
, Patent.MainCategoryNumber
, Patent.AllCategoryNumber
, Patent.Type
, Patent.Level
, Patent.Introduction
, Patent.AgencyID
, Patent.Agent
, Patent.Remark
,Expert.Name
,PatentAgency.Name,
PatentAgency.Contract,
Department.Name select * from #benefit_code21; drop table #benefit_code21;
Sql Server 查询多行并一行的更多相关文章
- sql server 查询分析器消息栏里去掉“(5 行受影响)”
sql server 查询分析器消息栏里去掉"(5 行受影响)" 在你代码的开始部分加上这个命令: set nocount on 记住在代码结尾的地方再加上: set ...
- SQL Server查询性能优化——堆表、碎片与索引(二)
本文是对 SQL Server查询性能优化——堆表.碎片与索引(一)的一些总结. 第一:先对 SQL Server查询性能优化——堆表.碎片与索引(一)中的例一的SET STATISTICS IO之 ...
- SQL Server查询性能优化——覆盖索引(二)
在SQL Server 查询性能优化——覆盖索引(一)中讲了覆盖索引的一些理论. 本文将具体讲一下使用不同索引对查询性能的影响. 下面通过实例,来查看不同的索引结构,如聚集索引.非聚集索引.组合索引等 ...
- 数据库表设计时一对一关系存在的必要性 数据库一对一、一对多、多对多设计 面试逻辑题3.31 sql server 查询某个表被哪些存储过程调用 DataTable根据字段去重 .Net Core Cors中间件解析 分析MySQL中哪些情况下数据库索引会失效
数据库表设计时一对一关系存在的必要性 2017年07月24日 10:01:07 阅读数:694 在表设计过程中,我无意中觉得一对一关系觉得好没道理,直接放到一张表中不就可以了吗?真是说,网上信息什么都 ...
- 通过DBCC Page查看在SQL Server中哪行数据被锁住了?
原文:通过DBCC Page查看在SQL Server中哪行数据被锁住了? 如何查看被锁的是哪行数据?通过dbcc page可以. 要想明白这个问题: 首先,需要模拟阻塞问题,这里直接模拟了阻塞问题的 ...
- [转] 利用SET STATISTICS IO和SET STATISTICS TIME 优化SQL Server查询性能
首先需要说明的是这篇文章的内容并不是如何调节SQL Server查询性能的(有关这方面的内容能写一本书),而是如何在SQL Server查询性能的调节中利用SET STATISTICS IO和SET ...
- 利用SET STATISTICS IO和SET STATISTICS TIME 优化SQL Server查询性能
首先需要说明的是这篇文章的内容并不是如何调节SQL Server查询性能的(有关这方面的内容能写一本书),而是如何在SQL Server查询性能的调节中利用SET STATISTICS IO和SET ...
- 如何找出你性能最差的SQL Server查询
我经常会被反复问到这样的问题:”我有一个性能很差的SQL Server.我如何找出最差性能的查询?“.因此在今天的文章里会给你一些让你很容易找到问题答案的信息向导. 问SQL Server! SQL ...
- Sql Server查询性能优化之走出索引的误区
据了解绝大多数开发人员对于索引的理解都是一知半解,局限于大多数日常工作没有机会.也什么没有必要去关心.了解索引,实在哪天某个查询太慢了找到查询条件建个索引就ok,哪天又有个查询慢了,再建立个索引就是, ...
随机推荐
- Base64编码与图片互转
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import ...
- linux command ---1
查看Linux的内核版本 当前系统的发行版信息(distribution):lsb_release -a , lsb(linux standard Base) and distribution inf ...
- Python tab 命令补全,以及 vim 补全
在python 命令行中,使用补全 python 查看 packages 的目录 可用 sys.path 查看. /usr/lib/python2.7/site-packages vim tab.py ...
- maven bundle
今天引入了几个bundle到pom,尽然说missing,我还以为是nexus组织下载.把type=bundle去掉可以下载,后来同事给了我这个连接https://issues.apache.org/ ...
- NSMutableDictionary
NSDictionary *dic = @{@"name":@"yj", @"age":@"24", @"ho ...
- Android之ListView的快速滑动模式:fastScrollEnabled以及滑块的自定义
http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/0917/1690.html http://blog.csdn.net/xyang ...
- ubuntu10.4的更新源因过期无法更新的解决方法
转载自:http://blog.csdn.net/suquan629/article/details/52333769 ubuntu10.4到2016年早已停止了更新支持,ubuntu也不再维护了.官 ...
- X-001 FriendlyARM Tiny4412 uboot移植前奏
版权声明:本文为博主原创文章,转载请注明出处 开发环境:win7 64位 + VMware12 + Ubuntu14.04 64位 工具链:linaro提供的gcc-linaro-6.1.1-2016 ...
- 【转】我是怎么找到电子书的 – IT篇
多读书,提高自己 电子出版物 IT-ebooks http://it-ebooks.info/ 上万本英文原版电子书,大多数为apress和o'relly的.全都是文字版,体积小又清楚.适合懂英文的人 ...
- 游标使用 和sp_executesql动态sql
/****** Script for SelectTopNRows command from SSMS ******/ declare @oid int declare @cid int declar ...