当前位置: 代码迷 >> Sql Server >> 除了相同行,显示多列
  详细解决方案

除了相同行,显示多列

热度:15   发布时间:2016-04-24 09:40:55.0
去掉相同行,显示多列
请教去掉相同行,显示多列



表proinfo内容,id,pname,picname
在pname中查询符合条件的行,并去掉pname相同行,同时显示id,pname,picname

------解决思路----------------------
--你的描述不清楚,相同的pname行,其它列要取哪一条
SELECT MAX(id)id,pname,MAX(picname)picname FROM proinfo
WHERE pname='条件' GROUP BY pname

------解决思路----------------------
select
   id,pname,picname
from
    proinfo as a
where not exists(select 1 from proinfo where pname=a.pname and id<t.id)

------解决思路----------------------
----------------------------------------------------------------
-- Author  :DBA_HuangZJ(發糞塗牆)
-- Date    :2014-11-26 13:51:59
-- Version:
--      Microsoft SQL Server 2014 (CTP1) - 11.0.9120.5 (X64) 
-- Jun 10 2013 20:09:10 
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition: Core-based Licensing (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
--
----------------------------------------------------------------
--> 测试数据:[proinfo]
if object_id('[proinfo]') is not null drop table [proinfo]
go 
create table [proinfo]([ID] int,[PNAME] varchar(4),[PICNAME] varchar(5))
insert [proinfo]
select 1,'aa','a.pic' union all
select 2,'bb','b.pic' union all
select 3,'aa','a.pic' union all
select 4,'cc','c.pic' union all
select 5,'aaaa','a.pic'
--------------开始查询--------------------------
select min(id)id,pname,picname
from (
select *
from proinfo
where pname like '%aa%')a
group by pname,picname

----------------结果----------------------------
/* 
id          pname picname
----------- ----- -------
1           aa    a.pic
5           aaaa  a.pic
*/
  相关解决方案