34,575
社区成员
发帖
与我相关
我的任务
分享
USE yjh
IF OBJECT_ID('test') IS NOT NULL DROP TABLE test
CREATE TABLE test
(c_key int identity,column1 varchar(40),column2 varchar(40))
INSERT INTO test
SELECT 'c11','c21' UNION ALL
SELECT 'c11','c22' UNION ALL
SELECT 'c12','c21' UNION ALL
SELECT 'c12','c21' UNION ALL
SELECT 'c12','c22'
SELECT column1,column2
FROM
(SELECT DISTINCT(column2),column1 FROM test) a
ORDER BY column1
/*结果
c11 c21
c11 c22
c12 c21
c12 c22
*/
use PracticeDB
go
if exists (select 1 from sysobjects where name='tb')
drop table tb
go
create table tb (c_key int,column1 varchar(10),column2 varchar(10))
insert into tb
select 1 ,'c11' ,'c21' union all
select 2 ,'c11' ,'c22' union all
select 3 ,'c12' ,'c21' union all
select 4 ,'c12' ,'c21' union all
select 5 ,'c12' ,'c22'
;with t
as
(
select distinct column2,column1
from tb a
)
select column1,column2
from t
order by column1
column1 column2
c11 c21
c11 c22
c12 c21
c12 c22
select distinct column1 cl1, column2 cl2
from a;
-- 或是?
select column1 cl1, count(column2) cl2
from a
group by column1;
-- 楼主没表述清楚!