2011-09-30 68 views
0

我的SQL查询,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 
+9

这是一个表示层问题,而不是数据库... –

+0

然后SQL可能不是你想要的。 –

+0

我同意这是演示问题,而不是SQL的错误 – Matt

回答

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来完成,但这并不意味着它应该是...

+0

嗨,真是太神奇了......真正我想要的是在这里(1解决方案)..非常感谢...一个小的请求,在同一个表中我有部门ID列..我会通过部门ID作为参数,所以根据输出应来.. – RobinHood

+0

见OVER为ROWNUM, 客户名称,ProjectDesc,专案编号,DepartmentID的, 从 修改到第二查询示例... – Sparky

+0

选择ROW_NUMBER()(ORDER BY SORTKEY)( 选择。 CustomerName,''作为ProjectID,''作为ProjectID,''作为DepartmentID, a.CustomerName +'1'作为SortKey from dbo.projects a 在b.CustomerName = a.customerName上加入dbo.projects b其中a。 DepartmentID ='52' union(b.ProjectID as varchar(20)),cast(b.DepartmentID as varchar(20)), a.customerName + '99' from dbo.projects a join dbo。选择'',b.ProjectDesc, b.CustomerName = a.customerName 其中b.DepartmentID ='52' )xx order by 1 – RobinHood

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(@[email protected]) 
     begin 
      if(@count=0) 
      begin 
       set @[email protected]; 
       set @[email protected]+1; 
      end   
      set @[email protected]; 
      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