Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to do this? Part-2


How to do this? Part-2

Author
Message
ganeshkumar005
ganeshkumar005
SSC Journeyman
SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)

Group: General Forum Members
Points: 90 Visits: 186
I have a table A:

StudID RollNo
Alpha D1234
beta A1122
charlie D1234
bravo C1342
tom B1964
harry A1122

and table B:

RollNo SubjectCode
D1234 1001
A1122 4001
D1234 2001
C1342 5001
B1964 6001
A1122 3001

I want to join the table A and B. For studID Alpha from table A i want the subjectcode 1001 only from table B and not want what other subjectcodes as you can see 'D1234' is repeated in table B.
Phil Parkin
Phil Parkin
SSCrazy Eights
SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)

Group: General Forum Members
Points: 8381 Visits: 19501
The same comment applies as from your last thread. There is no guaranteed row order in SQL tables, therefore you cannot ask for 'the first occurrence' unless you define an order explicitly.


Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
Cadavre
Cadavre
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2596 Visits: 8437
ganeshkumar005 (8/24/2012)
I have a table A:

StudID RollNo
Alpha D1234
beta A1122
charlie D1234
bravo C1342
tom B1964
harry A1122

and table B:

RollNo SubjectCode
D1234 1001
A1122 4001
D1234 2001
C1342 5001
B1964 6001
A1122 3001

I want to join the table A and B. For studID Alpha from table A i want the subjectcode 1001 only from table B and not want what other subjectcodes as you can see 'D1234' is repeated in table B.


I'm going to assume you want the lowest SubjectCode.

--== SAMPLE DATA
SELECT StudID, RollNo
INTO #tableA
FROM (VALUES('Alpha','D1234'),('beta','A1122'),('charlie','D1234'),('bravo','C1342'),
('tom','B1964'),('harry','A1122'))a(StudID, RollNo);

--== SAMPLE DATA
SELECT RollNo, SubjectCode
INTO #tableB
FROM (VALUES('D1234',1001),('A1122',4001),('D1234',2001),('C1342',5001),('B1964',6001),
('A1122',3001))a(RollNo, SubjectCode);

--== ACTUAL SOLUTION
SELECT tblA.StudID, tblA.RollNo, subQuery.SubjectCode
FROM #tableA tblA
CROSS APPLY (SELECT TOP 1 RollNo, SubjectCode
FROM #tableB tblB
WHERE tblA.RollNo = tblB.RollNo) subQuery;



Also, take a look at this link about SQL Server ordering.


Forever trying to learn

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better, quicker answers on SQL Server performance related questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/



If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock


Craig Wilkinson - Software Engineer
LinkedIn
Lowell
Lowell
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14967 Visits: 38985
both these posts looked way to much like homework;

I don't think the OP will learn anything if we do the work for him, without letting him learn the techniques.

Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

ganeshkumar005
ganeshkumar005
SSC Journeyman
SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)

Group: General Forum Members
Points: 90 Visits: 186
Lowell - there could be always some lateral learnings. You write a script, I see how to write it. I am not into T-SQl by the way. And thanks for your help, everybody.
Divine Flame
Divine Flame
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1571 Visits: 2801
You can do it using CTE also.


--========= Student table

IF OBJECT_ID('tempdb..#Student') IS NOT NULL
BEGIN
DROP TABLE #Student
END
GO
CREATE TABLE #Student
(
StudID VARCHAR(100),
RollNo VARCHAR(100)
)
GO
INSERT INTO #Student VALUES('Alpha', 'D1234')
INSERT INTO #Student VALUES('beta', 'A1122')
INSERT INTO #Student VALUES('charlie', 'D1234')
INSERT INTO #Student VALUES('bravo', 'C1342')
INSERT INTO #Student VALUES('tom', 'B1964')
INSERT INTO #Student VALUES('harry', 'A1122')
Go

--========= StudentSubjects table
IF OBJECT_ID('tempdb..#StudentSubjects') IS NOT NULL
BEGIN
DROP TABLE #StudentSubjects
END
GO
CREATE TABLE #StudentSubjects
(
RollNo VARCHAR(100),
SubjectCode VARCHAR(100),
)
GO
INSERT INTO #StudentSubjects VALUES ('D1234', 1001)
INSERT INTO #StudentSubjects VALUES ('A1122', 4001)
INSERT INTO #StudentSubjects VALUES ('D1234', 2001)
INSERT INTO #StudentSubjects VALUES ('C1342', 5001)
INSERT INTO #StudentSubjects VALUES ('B1964', 6001)
INSERT INTO #StudentSubjects VALUES ('A1122', 3001)
Go

--======= Solution:
;WITH MyCTE (RollNo, LowestSubjectCode)
AS
(
SELECT RollNo, MIN (SubjectCode) AS LowestSubjectCode FROM #StudentSubjects GROUP BY RollNo
)
SELECT S.StudId,S.RollNo,MC.LowestSubjectCode
FROM #Student S INNER JOIN MyCTE MC
ON S.RollNo = MC.RollNo




Sujeet Singh
Lowell
Lowell
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14967 Visits: 38985
i've put more than a few posts on the board here on SSC, so I've helped an aweful lot of people.
;
the best way to learn is to try. if you posted a query that wasn't working, but showed effort, we could help you learn.

I'm really afraid you'll just take the solutions here, hand them in, but not understand them;
when the next question comes that is similar, you'll be stuck, because you didn't get a handle on hte original.

Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

ganeshkumar005
ganeshkumar005
SSC Journeyman
SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)

Group: General Forum Members
Points: 90 Visits: 186
Agree with you.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search