Click here to Skip to main content
15,913,773 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
CREATE TABLE [dbo].[tbl_ticket](
[nid] [int] IDENTITY(1,1) NOT NULL,
[ticketno] [bigint] NULL,
[memberid] [varchar](50) NULL,
[name] [varchar](200) NULL,
[category] [varchar](50) NULL,
[ticketstatus] [varchar](50) NULL CONSTRAINT [DF_tbl_ticket_ticketstatus]  DEFAULT ('Open'),
[bstatus] [bit] NULL CONSTRAINT [DF_tbl_ticket_bstatus]  DEFAULT ((1))
)

CREATE TABLE [dbo].[tbl_ticket_message](
[nid] [int] IDENTITY(1,1) NOT NULL,
[ticketno] [bigint] NULL,
[subject] [varchar](500) NULL,
[message] [varchar](2000) NULL,
[attachment] [varchar](50) NULL,
[messageType] [varchar](50) NULL CONSTRAINT [DF_tbl_ticket_message_messageby]  DEFAULT ('Question'),
[messageby] [varchar](200) NULL,
[bstatus] [bit] NULL CONSTRAINT [DF_tbl_ticket_message_bstatus]  DEFAULT ((1))
)

These are two table, I want to select only ticketno, subject, message, attachment columns.

condition:
first I select ticket no form tbl_ticket where memberid is equels to given memberid then then select subject message attachment form tbl_ticket_message where ticketno is equals to select form previous query.

In this result ticketno may repeat but I want ticketno not not repeat return only single record of each ticketno.

Please help me.
Posted
Updated 21-Nov-10 18:31pm
v3
Comments
Manfred Rudolf Bihy 22-Nov-10 0:37am    
Ok, if there can be more than one entry for one ticket number in tbl_ticket_message, which one shall it be?
You'll have to be a little more detailed in your request.

you can use Join Concept two get value from two table.

as I getting your question, you want to get data of two table based on single common matching value.
try this code
SQL
select e1.ticketno , e1.memberid,
e2.subject, e2.message,e2.attachment
from tbl_ticket e1 join
tbl_ticket_message e2
on e1.ticketno = e2.ticketno
 
Share this answer
 
v2
Comments
Manfred Rudolf Bihy 22-Nov-10 0:45am    
That doesn't help him since tbl_ticket_message may contain more than record per ticketno, but as he stated in his request he wants only one. He has yet to elaborate which one of those it should be. Go figure! ;)
RaviRanjanKr 22-Nov-10 0:50am    
Thanks for your suggestion, but as I know two get value from two table based on single column we use Join concept. that's why I answered this. and also I check this code it works better.
If you want only one you could do this:

SELECT TOP 1 ticketno, subject, message, attachment columns
FROM tbl_ticket_message tm
WHERE tm.ticketno = @whatEverYouSelectedInYourFirstSelectForTicketNo


But that seems a bit random to me. It really dependes on what message is most important to you, if you only want one of possibly many to be returned.

Cheers

Manfred
 
Share this answer
 
SQL
CREATE PROCEDURE dbo.GetResult
@MemberID       VARCHAR(50)
AS
BEGIN
    SELECT DISTINCT ([ticketno]),
            [subject],
            [message],
            [attachment]
    FROM    [tbl_ticket_message]
    WHERE   [ticketno] = (SELECT [ticketno] FROM [dbo].[tbl_ticket] WHERE [memberid] = @MemberID)
END


Hope this will help you.
All the best.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900