选择具有不同别名和不同条件的相同列

时间:2016-09-10 13:01:35

标签: c# mysql crystal-reports

所以我有一个员工工作时间表,我想在Crystal Report Visual Studio 2012 C#中选择员工ID到日期名称进行报告。

数据库

  Employee_ID  |   DayName   | WorkStart 
    EM0001     |   Monday    |  Morning  
    EM0002     |   Monday    |  Morning  
    EM0003     |   Monday    |  Noon     
    EM0004     |   Monday    |  Noon     
    EM0003     |   Thursday  |  Morning  
    EM0004     |   Thursday  |  Morning  
    EM0001     |   Thursday  |  Noon     
    EM0002     |   Thursday  |  Noon     

我已经尝试了

SELECT Employee_ID as EmMonday FROM table WHERE dayName = 'Monday'
UNION
SELECT Employee_ID as EmThursday FROM table WHERE dayName = 'Thursday';

但它刚刚展示EmMonday,以及EmThursday如何展示?

如下所示

 EmMonday | EmThursday 
  EM0001  |   EM0003   
  EM0002  |   EM0004   
  EM0003  |   EM0001   
  EM0004  |   EM0002   

2 个答案:

答案 0 :(得分:1)

FOR MYSQL

SET @rank1=0;
SET @rank2=0;

select t1.Employee_ID as EmMonday, t2.Employee_ID as EmThursday from 
(SELECT @rank1 := @rank1+1  as id, Employee_ID FROM T WHERE dayName = 'Monday' ) as t2
left join 
(SELECT @rank2 := @rank2+1  as id, Employee_ID FROM T WHERE dayName = 'Thursday') t1 on t1.id = t2.id

Demo is here.

FOR MSSQL:

select t1.Employee_ID as EmMonday, t2.Employee_ID as EmThursday from 
(SELECT ROW_NUMBER() OVER (ORDER BY Employee_ID) as id, Employee_ID FROM T WHERE dayName = 'Monday' ) as t1
left join 
(SELECT ROW_NUMBER() OVER (ORDER BY Employee_ID) as id, Employee_ID FROM T WHERE dayName = 'Thursday') t2 on t1.id = t2.id

rankRowNumber()用于表示我们将在星期一和星期四加入的行数。

T是您的表名。

答案 1 :(得分:0)

您可以在一个查询中执行此操作:

SELECT Employee_ID as EmMonday, Employee_ID as EmThursday 
FROM table 
WHERE dayName = 'Monday' or dayName = 'Thursday'