简体   繁体   English

如何在SQL中使用自我加入?

[英]how to work with self join in sql?

I have 2 tables in sql 我在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]

i need to retrieve data form both tables like- 我需要从两个表中检索数据,例如-

[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

i have tried this but not getting the required result:- 我已经尝试过了,但是没有得到所需的结果:

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

any help will be appreciated, thanks in advance! 任何帮助将不胜感激,在此先感谢!

Try UNION like this 像这样尝试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 SQL演示1

Update : 更新:

You can also use CASE WHEN like below : 您还可以使用CASE WHEN,如下所示:

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 SQL演示2

An SQL join clause - corresponding to a join operation in relational algebra - combines columns from one or more tables in a relational database . SQL连接子句(对应于关系代数中的联接操作)组合来自关系数据库中一个或多个表的列。 We know about inner join , left join , natural join etc in SQL . 我们知道SQL中的内部联接,左联接,自然联接等。 But most of them don't know what is self join and how does it work ? 但是他们中的大多数人都不知道什么是自我连接,它是如何工作的? Now we are going to know what is self join and how does it work . 现在我们将要了解什么是自我联接及其作用方式。 We can use a self join when a table references data in itself. 当表本身引用数据时,我们可以使用自连接。 To clearly understand see the following table . 为了清楚地了解,请参见下表。 在此处输入图片说明

See , This is our Employee table and we have to find out who works under the manager . 瞧,这是我们的Employee表,我们必须找出谁在经理下工作。 Here Sonam is the manager and Rahul and Jay works under Sonam . 索纳姆(Sonam)是经理,拉胡尔(Rahul)和杰伊(Jay)在索纳姆(Sonam)的工作下。 Thereafter The manager of Sonam is Kunal . 此后,Sonam的经理是Kunal。 Furthermore The manager of Ram is Rani and Rani does'nt have any manager . 而且Ram的经理是Rani,Rani没有任何经理。 That mean you can say She is CEO. 这意味着您可以说她是首席执行官。 Hope you will understand. 希望你能理解。 So let's learn how to write this query . 因此,让我们学习如何编写此查询。 See the final report of their works under manager. 参见他们在经理领导下的作品的最终报告。 read full article from here What is self join in SQL . 从这里阅读全文。什么是SQL中的自我联接。 read full article from here Self join in sql 阅读全文从这里自我在SQL JOIN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM