SSMS - How to convert multiple rows for a client into fields for a column











up vote
0
down vote

favorite












This is my first question, so apologies if I am missing any etiquette.



I have a SSMS qry which pulls together 3 databases from 2 servers. all working ok, but I have been asked to add another field which surprise surprise is in a different table.



The problem I am having, is that this new table has multiple rows for a client, so as an example. I Have in my data, Policy Number, Driver Name, Car, Reg.
I am joining the new table via policy number, but the new table has additional drivers, so for each additional driver there is a new row (so a row with the same policy number for each add driver). I want 1 field from this new table ("relationship to Driver") to be shown in columns on the main table I have. So add driver 1, add driver 2 etc, there is a field in each row which identifies if the row is first, second, third driver etc.



Is this something anyone can help with at all?



Thanks










share|improve this question
























  • Please provide the database scheme and some sample data.
    – mypetlion
    Nov 7 at 17:13















up vote
0
down vote

favorite












This is my first question, so apologies if I am missing any etiquette.



I have a SSMS qry which pulls together 3 databases from 2 servers. all working ok, but I have been asked to add another field which surprise surprise is in a different table.



The problem I am having, is that this new table has multiple rows for a client, so as an example. I Have in my data, Policy Number, Driver Name, Car, Reg.
I am joining the new table via policy number, but the new table has additional drivers, so for each additional driver there is a new row (so a row with the same policy number for each add driver). I want 1 field from this new table ("relationship to Driver") to be shown in columns on the main table I have. So add driver 1, add driver 2 etc, there is a field in each row which identifies if the row is first, second, third driver etc.



Is this something anyone can help with at all?



Thanks










share|improve this question
























  • Please provide the database scheme and some sample data.
    – mypetlion
    Nov 7 at 17:13













up vote
0
down vote

favorite









up vote
0
down vote

favorite











This is my first question, so apologies if I am missing any etiquette.



I have a SSMS qry which pulls together 3 databases from 2 servers. all working ok, but I have been asked to add another field which surprise surprise is in a different table.



The problem I am having, is that this new table has multiple rows for a client, so as an example. I Have in my data, Policy Number, Driver Name, Car, Reg.
I am joining the new table via policy number, but the new table has additional drivers, so for each additional driver there is a new row (so a row with the same policy number for each add driver). I want 1 field from this new table ("relationship to Driver") to be shown in columns on the main table I have. So add driver 1, add driver 2 etc, there is a field in each row which identifies if the row is first, second, third driver etc.



Is this something anyone can help with at all?



Thanks










share|improve this question















This is my first question, so apologies if I am missing any etiquette.



I have a SSMS qry which pulls together 3 databases from 2 servers. all working ok, but I have been asked to add another field which surprise surprise is in a different table.



The problem I am having, is that this new table has multiple rows for a client, so as an example. I Have in my data, Policy Number, Driver Name, Car, Reg.
I am joining the new table via policy number, but the new table has additional drivers, so for each additional driver there is a new row (so a row with the same policy number for each add driver). I want 1 field from this new table ("relationship to Driver") to be shown in columns on the main table I have. So add driver 1, add driver 2 etc, there is a field in each row which identifies if the row is first, second, third driver etc.



Is this something anyone can help with at all?



Thanks







sql join ssms






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 7 at 19:37









jarlh

27.9k52137




27.9k52137










asked Nov 7 at 17:01









Aimone111

1




1












  • Please provide the database scheme and some sample data.
    – mypetlion
    Nov 7 at 17:13


















  • Please provide the database scheme and some sample data.
    – mypetlion
    Nov 7 at 17:13
















Please provide the database scheme and some sample data.
– mypetlion
Nov 7 at 17:13




Please provide the database scheme and some sample data.
– mypetlion
Nov 7 at 17:13

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53194292%2fssms-how-to-convert-multiple-rows-for-a-client-into-fields-for-a-column%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53194292%2fssms-how-to-convert-multiple-rows-for-a-client-into-fields-for-a-column%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini