Results 1 to 9 of 9

Thread: [RESOLVED] I want to change the view of the query.

  1. #1

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Resolved [RESOLVED] I want to change the view of the query.

    Hi all
    Please look the table query.

    SELECT [Post_ID]
    ,[Post_Category_ID]
    ,[Post_Title]
    ,[Post_Description]
    ,[Is_Email_Notification]
    ,[Attachment_URL]
    ,[User_ID]
    ,[Is_Delete]
    ,[Is_Move]
    ,[Is_Close]
    ,[Total_Views]
    FROM [Cfamla].[dbo].[Post_Detail]


    SELECT [Post_Reply_ID]
    ,[Last_Modification_Date]
    ,[Post_ID]
    ,[Reply_Title]
    ,[Reply_Description]
    ,[Is_Email_Notification]
    ,[Attachment_URL]
    ,[User_ID]
    ,[Is_Delete]
    FROM [Cfamla].[dbo].[Post_Reply]

    Above are two table I want to data in the next format!

    [Post_ID] ,[Post_Title]
    [Post_Reply_ID],[Reply_Title]

    mean In the first Row of the select statement it will show me the Post_Id that is always
    After the next row... It will so all the reply of that post.

    Hope I explain the question correctly.

    Thanks

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: I want to change the view of the query.

    That needs to be done with a UNION ALL.

    I'm going to assume that the Post_Id and Post_Reply_Id are "related" - being the same value - right?

    Code:
    Select Post_Id, Post_Title, 0
          From Post_Detail
    
    Union All
    
    Select Post_Reply_Id, Reply_Title, 1
          From Post_Reply
    
    Order by 1,3
    You need the 3rd column just to keep things in order.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: I want to change the view of the query.

    The two tables can be linked using the Post_Id table.

    Select PD.Post_Id, PD.Post_Title, PR.Post_Reply_Id, PR.Post_Detail
    From Post_Detail PD Inner Join Post_Reply PR On PR.Post_Id = PD.Post_Id

    mean In the first Row of the select statement it will show me the Post_Id that is always
    After the next row... It will so all the reply of that post.
    I am not sure I fully understand what you mean.

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: I want to change the view of the query.

    I interpretted this

    [Post_ID] ,[Post_Title]
    [Post_Reply_ID],[Reply_Title]
    to mean that two rows would be returned for each post_id - first row the post_title and second row the reply_title. And so on and so on...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: I want to change the view of the query.

    Right, that makes sense.

    But if there can be multiple replies, ie

    [Post_ID] ,[Post_Title]
    [Post_Reply_ID],[Reply_Title]
    [Post_Reply_ID],[Reply_Title]
    [Post_Reply_ID],[Reply_Title]

    [Post_ID] ,[Post_Title]
    [Post_Reply_ID],[Reply_Title]

    [Post_ID] ,[Post_Title]
    [Post_Reply_ID],[Reply_Title]
    [Post_Reply_ID],[Reply_Title]

    Would a Union still work without including the Post_Reply.Post_Id field in the selection list.

  6. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: I want to change the view of the query.

    Quote Originally Posted by szlamany
    I'm going to assume that the Post_Id and Post_Reply_Id are "related" - being the same value - right?
    oops - I did not see that POST_ID was in the reply table as well.

    I guess it would be more like

    Code:
    Select Post_Id, Post_Title, Post_Id, 0
          From Post_Detail
    
    Union All
    
    Select Post_Reply_Id, Reply_Title, Post_Id, 1
          From Post_Reply
    
    Order by 3,4,1
    I added Post_Id as the 3rd column in each query - ordering by that column first - then the 0/1 column - and then I added an order by the first column in case dup Post_Reply_Id's need to be ordered.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  7. #7

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Smile Re: I want to change the view of the query.

    Thanks for the reply, but I think you not get the question.
    Here is the example

    Post_Detail Table
    Code:
    Post_ID         Post_Title
    1                  Basic Problem
    [Post_Reply] Table
    Code:
    [Post_Reply_ID]     [Post_ID],   [Reply_Title]
    11                          1                 This is the solution of the problem
    12                          1                 I got it. Thanks
    13                          1                 I have no Idea
    If we use inner join then it will show only three record!

    But I want record in the following format! for post Id=1

    Code:
    [ID]       [Title]
    1           Basic Problem
    11         This is the solution of the problem
    12         I got it. Thanks
    13         I have no Idea
    Hope you got the exact problem
    Thanks
    Last edited by shakti5385; Nov 28th, 2007 at 01:52 AM.

  8. #8
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: I want to change the view of the query.

    Did you try my query from post #6?

    It returns those two columns of information exactly as you proposed.

    But in order to keep the "order straight" you have no choice but to create bogus columns for sorting on.

    Try it please.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  9. #9

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: I want to change the view of the query.

    Thanks szlamany

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width