question

SarathkumarReddySannadi-9823 avatar image
0 Votes"
SarathkumarReddySannadi-9823 asked SarathkumarReddySannadi-9823 commented

how to combine two tables and make one created date column

Hi,

I am trying to combine two notifications tables, posts and comments and order them by created date, and display all notifications in a list, i tried joining them and able to pull the data but i want a common created date so that i can order them, please let me know how can i do that

 SELECT nc.id, nc.CommentId, c.CommentText, nc.CreatedOn, np.Id,np.PostId,p.Post, np.CreatedOn, m.NotificationType,m.Description, m.Id as notificationid
 FROM tbl_notifications_master AS m
 left JOIN tbl_notifications_comment AS nc ON nc.NotificationId = m.Id
 left JOIN tbl_comments AS c ON c.CommentId = nc.CommentId
 left JOIN tbl_notifications_post AS np on np.NotificationId = m.Id
 left JOIN tbl_posts AS p ON p.Id = np.PostId

SQLResult


sql-server-generalsql-server-transact-sql
image.png (74.5 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Please keep in mind while that people who answer questions here know SQL well, we don't know your tables. When you say i want a common created date, I need to confess that I don't understand what you mean.

For this type of questions, it is often a good idea that you post CREATE TABLE statements for your tables, together with INSERT statements with sample data, enough to illustrate all angles of the problem, and to this we also we need to have the expected result given the test data. Finally, we need a brief description of the business rules, so that we understand why you want that result.

0 Votes 0 ·

Sorry for posting a short question, i thought it would be easy to answer for a DB expert, i will try to post the whole set for upcoming questions

0 Votes 0 ·

1 Answer

tonyfaull avatar image
0 Votes"
tonyfaull answered

You can use the COALESCE function to combine the dates.

 ORDER BY COALESCE(nc.CreatedOn, np.CreatedOn)

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.