如何在sql中使用self join?

时间:2013-12-04 10:19:36

标签: sql sql-server select join

我在sql中有2个表

1. SELECT TOP 1000 [UserId]
      ,[UserName]
      ,[Password]
      ,[FirstName]
      ,[LastName]
      ,[Password_Expiry]
      ,[Modified]
      ,[ModifiedBy]
      ,[CreatedBy]
      ,[Type]
      ,[Privileges]
  FROM [Paramount].[dbo].[UserProfile]



2. SELECT TOP 1000 [Id]
      ,[UserId]
      ,[ClockInClockOutTypeId]
      ,[CreatedDate]
  FROM [Paramount].[dbo].[ClockInClockOut]

我需要从两个表中检索数据,如 -

[UserName]| Date [CreatedDate]|[CreatedDate](just time in) as TimeIn | [CreatedDate](just Time Out) as time out 
Mr.abc xyz| 4/12/13           | 1:30 Pm                              | 3:30Pm

我试过这个但没有得到所需的结果: -

SELECT u.[username], 
       c.[clockinclockouttypeid], 
       c.[createddate] 
FROM   [Paramount].[dbo].[clockinclockout] AS c 
       JOIN [Paramount].[dbo].[userprofile] AS u 
         ON c.userid = u.[userid] 
WHERE  c.[clockinclockouttypeid] = 1 

SELECT u.[username], 
       c.[clockinclockouttypeid], 
       c.[createddate] 
FROM   [Paramount].[dbo].[clockinclockout] AS c 
       JOIN [Paramount].[dbo].[userprofile] AS u 
         ON c.userid = u.[userid] 
WHERE  c.[clockinclockouttypeid] = 2 rProfile] as u on c.UserId = u.[UserId] where c.[ClockInClockOutTypeId]=2

任何帮助将不胜感激,提前感谢!

2 个答案:

答案 0 :(得分:1)

尝试这样的UNION

select [UserName], MAX([Date]) as [Date], MAX(InTime) as InTime, MAX(OutTime) as OutTime
FROM (
    select u.[UserName],
           CONVERT(VARCHAR(20), c.[CreatedDate], 101) as [Date],
           CONVERT(VARCHAR(20), c.[CreatedDate], 108) as InTime,
           null as OutTime
    from [ClockInClockOut] as c 
    inner join [UserProfile] as u on c.UserId = u.[UserId] 
    where c.[ClockInClockOutTypeId]=1 

    UNION 
    select u.[UserName],
           CONVERT(VARCHAR(20), c.[CreatedDate], 101)  as [Date],
           null as InTime,
           CONVERT(VARCHAR(20), c.[CreatedDate], 108)as OutTime
    from [ClockInClockOut] as c 
    inner join [UserProfile] as u on c.UserId = u.[UserId] 
    where c.[ClockInClockOutTypeId]=2
) tt
GROUP BY [UserName]

<强> SQL Demo 1

更新:

您也可以使用以下情况:

select [UserName], MAX([Date]) as [Date], MAX(InTime) as InTime, MAX(OutTime) as OutTime
FROM
(
   select u.[UserName],
          CONVERT(VARCHAR(20), c.[CreatedDate], 101) as [Date],
          CASE WHEN c.[ClockInClockOutTypeId]=1 THEN CONVERT(VARCHAR(20), c.[CreatedDate], 108) ELSE null END as InTime,
          CASE WHEN c.[ClockInClockOutTypeId]=2 THEN CONVERT(VARCHAR(20), c.[CreatedDate], 108) ELSE null END as OutTime
   from [ClockInClockOut] as c 
   inner join [UserProfile] as u on c.UserId = u.[UserId] 
) tt
GROUP BY [UserName]

<强> SQL Demo 2

答案 1 :(得分:1)

SQL连接子句(对应于关系代数中的联接操作)将来自关系数据库中一个或多个表的列组合在一起。我们知道SQL中的内部联接,左联接,自然联接等。但是大多数人不知道什么是自我加入以及它如何运作?现在我们将要了解什么是自我联接及其作用方式。当表本身引用数据时,我们可以使用自连接。为了清楚地了解,请参见下表。 enter image description here

看,这是我们的Employee表,我们必须找出谁在经理下工作。索纳姆(Sonam)是经理,拉胡尔(Rahul)和杰伊(Jay)在索纳姆(Sonam)的工作下。此后,Sonam的经理是Kunal。此外,Ram的经理是Rani,而Rani没有任何经理。这意味着您可以说她是首席执行官。希望你能理解。因此,让我们学习如何编写此查询。参见他们在经理领导下的作品的最终报告。从这里阅读全文。什么是SQL中的自我联接。从这里阅读全文Self join in sql