title: Getting likes count by post using sql query
body: <p>Can you please help me building the sql query to get all the like for a particular post and i need to do the same for all the posts
  and the second option i would required is, if the current logged in user has already liked that particular post, if so i need have a true or false Boolean value.</p>
  
  <p>i want to join both the tables and get total count of likes for a particular post and also if the current user liked the post or not</p>
  
  <p><strong>Post likes table</strong></p>
  
  <pre><code>    Id                                  PostId                                  LikedBy        LikedOn
  9B0B2DEB-47CE-4F6E-B892-35D2F2D3F543    480149F2-BCF4-40F4-3C19-08D7DFC7C616    0F8FAD5B-D9CB-469F-A165-70867728950E    2020-04-15 23:08:26.567
  40E7C5F4-AD42-4A22-8BC0-5B0C9F125427    480149F2-BCF4-40F4-3C19-08D7DFC7C616    0F8FAD5B-D9CB-469F-A165-70867728950E    2020-04-15 23:08:10.530
  C132E64E-C9A2-471E-9DD9-CB9A342D2DBE    6B989E93-10FE-4131-38B4-08D7DC6856B4    0F8FAD5B-D9CB-469F-A165-70867728950E    2020-04-16 11:43:46.730
  32099959-DB24-42F0-A629-F0991163509A    480149F2-BCF4-40F4-3C19-08D7DFC7C616    0F8FAD5B-D9CB-469F-A165-70867728950E    2020-04-15 22:49:44.557
  AA606CFF-EAC0-4010-A673-FF682D46AB80    480149F2-BCF4-40F4-3C19-08D7DFC7C616    0F8FAD5B-D9CB-469F-A165-70867728950E    2020-04-16 11:43:54.587
  </code></pre>
  
  <p><strong>All Posts</strong></p>
  
  <pre><code>Id                                      Title                    UserId                          Post
  6B989E93-10FE-4131-38B4-08D7DC6856B4    great                   0F8FAD5B-D9CB-469F-A165-70867728950E    this is great app to build
  480149F2-BCF4-40F4-3C19-08D7DFC7C616    What is Lorem Ipsum?    0F8FAD5B-D9CB-469F-A165-70867728950E    Lorem Ipsum is simply dummy 
  </code></pre>
  
  <p><em>here post.userid = postlike.likedby
  and post.id = postlike.postid</em></p>
  
  <p>i tried this but the query keeps executing with no output</p>
  
  <pre><code>    DROP TABLE IF EXISTS #Tempposts
  create table #Tempposts
  (
      PostID varchar(50), 
      UserID varchar(50),
      TotalLikes int,
      IsLikedByUser bit
  )
  Declare @TempPostID varchar(50)
  Declare @TempUserID varchar(50)
  Declare @TempTotalLikes Int
  Declare @TempIsLikedByUser bit
  Declare @Id uniqueidentifier
  Declare @UserId uniqueidentifier
  
  DECLARE load_cursor CURSOR FOR 
      SELECT Id, UserId
      FROM tbl_posts
  
  OPEN load_cursor 
  FETCH NEXT FROM load_cursor INTO @Id, @UserId
  WHILE @@FETCH_STATUS = 0 
      BEGIN 
          SET @TempPostID = @Id
          SET @TempUserID = @UserId
          SET @TempTotalLikes = (select count(*) from tbl_postlikes where PostId = @Id)
          SET @TempIsLikedByUser = (select count(*) from tbl_postlikes where PostId = @Id and LikedBy = @UserId)
          insert into #Tempposts values(@TempPostID, @TempUserID, @TempTotalLikes, @TempIsLikedByUser)
          FETCH NEXT FROM load_cursor INTO @Id, @UserId
      End
  Close load_cursor
  DeAllocate load_cursor
  select * from #Tempposts
  Drop table #Tempposts
  </code></pre>
  
  <p><strong>my table definitions</strong></p>
  
  <pre><code>CREATE TABLE [dbo].[tbl_postlikes](
      [Id] [uniqueidentifier] NOT NULL,
      [PostId] [uniqueidentifier] NULL,
      [LikedBy] [uniqueidentifier] NULL,
      [LikedOn] [datetime] NULL,
  PRIMARY KEY CLUSTERED 
  (
      [Id] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  ) ON [PRIMARY]
  GO
  
  CREATE TABLE [dbo].[tbl_posts](
      [Id] [uniqueidentifier] NULL,
      [Title] [varchar](max) NULL,
      [Post] [varchar](max) NULL,
      [UserId] [uniqueidentifier] NULL,
      [Created] [datetime] NULL,
      [Updated] [datetime] NULL,
      [IsVisible] [bit] NULL,
      [CreatedBy] [uniqueidentifier] NULL,
      [UpdatedBy] [uniqueidentifier] NULL,
      [HasComments] [bit] NULL
  ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
  GO
  </code></pre>