需要SQL查询(基于列值,应加载行值)

时间:2011-09-30 03:39:34

标签: sql sql-server-2005

我的Sql查询是,

Select CustomerName,ProjectID,ProjectDesc from dbo.projects

The Output is:

CustomerName                     ProjectID   ProjectDesc

A3 Consulting FZ, LLC.          277         A3
A3 Consulting FZ, LLC.          278         A3 - Chef
Anesthesia Business Consultant  279         Astra
ARI Network Services, Inc.      280         Mira

我想要的是,项目设计应该按照客户排列。

CustomerName               ProjectDesc     ProjectID   

A3 Consulting FZ, LLC.                   

                                A3            277    
                                A3 - Chef     278    

Anesthesia Business Consultant  

                                Astra         279

ARI Network Services, Inc.

                                Mira          280

2 个答案:

答案 0 :(得分:1)

我同意这些评论,这不是查询问题,而是显示问题。但如果您真的想在SQL中执行此操作,请尝试此操作。

Select a.CustomerName,'' as ProjectDesc,'' as ProjectID,
       a.CustomerName+'1' as SortKey
from dbo.projects a
join dbo.projects b on b.CustomerName=a.customerName
union 
Select '' ,b.ProjectDesc,cast(b.ProjectID as varchar(20)),
       a.customerName+'99'
from dbo.projects a
join dbo.projects b on b.CustomerName=a.customerName
order by 4

它会给你你想要的东西,但是你的前端需要摆脱一个额外的排序键列......

如果你想隐藏那个键,这是另一种方法

select ROW_NUMBER() OVER(ORDER BY SortKey) as RowNum,
       CustomerName, ProjectDesc, ProjectID
from
(
Select a.CustomerName,'' as ProjectDesc,'' as ProjectID ,
       a.CustomerName+'1' as SortKey
from dbo.projects a
join dbo.projects b on b.CustomerName=a.customerName
where departmentID = @DeptID
union 
Select '' ,b.ProjectDesc,cast(b.ProjectID as varchar(20)),
       a.customerName+'99'
from dbo.projects a
join dbo.projects b on b.CustomerName=a.customerName
where departmentID = @DeptID
) xx
order by 1

这种方法会将丑陋的密钥转换为一系列连续的数字。但仅仅因为这可以在SQL中完成,并不意味着它应该......

答案 1 :(得分:0)

在sql server 2005中使用此代码。我认为这肯定会有用......

   create table #projects1 
    (
    CustomerName varchar(max),
    ProjectDesc varchar(max),
    ProjectID varchar(max)
    );


declare @count int;
declare @lastname varchar(max),@CustomerName varchar(max),@ProjectID varchar(max),@ProjectDesc varchar(max);
set @count=0;
set @lastname='';
declare pro_cursor CURSOR for
select CustomerName, ProjectDesc, ProjectID from projects
open pro_cursor
fetch next from pro_cursor into @CustomerName,@ProjectDesc,@ProjectID
    while(@@fetch_status=0)
    begin
        if(@lastname!=@CustomerName)
        begin
            if(@count=0)
            begin
                set @lastname=@CustomerName;
                set @count=@count+1;
            end         
            set @lastname=@CustomerName;
            insert into #projects1 values (@CustomerName,'','');
            insert into #projects1 values ('',@ProjectDesc,@ProjectID);
        end     
        else
        begin
            insert into #projects1 values ('',@ProjectDesc,@ProjectID)
        end
    fetch next from pro_cursor into @CustomerName,@ProjectDesc,@ProjectID
    end
select * from #projects1
close pro_cursor;
deallocate pro_cursor
truncate table #projects1