How To display Multiple Rows on Single Row
I am working on a query which contains multiple rows per a particular id and im looking to display them all on one line.
The query I currently have is :
SELECT
person_id,
car_name,
car_year,
car_value,
car_mileage
FROM tbl_motor_vehicles
This returns the below:
person_id car_name car_year car_value car_mileage
---------------------------------------------------------------------
1 TOYOTA YARIS 2017 5000 10000
1 Nissan Micra 2001 1000 15000
2 Golf 15000 700000
2 Fiat Punto 2002 2500 60000
I need this to display on one line per id with each car getting its own column like below :
Person ID Car_1_NAME Car_1_year car1_value car1_mileage Car_2_NAME Car_2_year car2_value car2_mileage
--------------------------------------------------------------------------------------------------------------------------------------
1 TOYOTA YARIS 2017 5000 10000 Nissan Micra 2001 1000 15000
2 Golf 2007 15000 700000 Fiat Punto 2002 2500 60000
Can anyone advise me how to go about this ?
sql sql-server grouping
|
show 4 more comments
I am working on a query which contains multiple rows per a particular id and im looking to display them all on one line.
The query I currently have is :
SELECT
person_id,
car_name,
car_year,
car_value,
car_mileage
FROM tbl_motor_vehicles
This returns the below:
person_id car_name car_year car_value car_mileage
---------------------------------------------------------------------
1 TOYOTA YARIS 2017 5000 10000
1 Nissan Micra 2001 1000 15000
2 Golf 15000 700000
2 Fiat Punto 2002 2500 60000
I need this to display on one line per id with each car getting its own column like below :
Person ID Car_1_NAME Car_1_year car1_value car1_mileage Car_2_NAME Car_2_year car2_value car2_mileage
--------------------------------------------------------------------------------------------------------------------------------------
1 TOYOTA YARIS 2017 5000 10000 Nissan Micra 2001 1000 15000
2 Golf 2007 15000 700000 Fiat Punto 2002 2500 60000
Can anyone advise me how to go about this ?
sql sql-server grouping
Will each person have at most 2 cars?
– Henning Koehler
Nov 14 '18 at 11:24
welcome to SO. please have a look at how-to-ask What you are looking for is PIVOT. Please use the search for PIVOT, you will find MANY answers
– swe
Nov 14 '18 at 11:24
1
I wouldn't use SQL for this. Use the programming language of your Website or app to care about the grid layout.
– Thorsten Kettner
Nov 14 '18 at 11:25
No , there is no set amount of cars . Also , there is no fixed amount of person ids either so cant be hardcoded
– Farrea69
Nov 14 '18 at 11:30
2
No , there is no set amount of cars . Also , there is no fixed amount of person ids either so cant be hardcoded - It's a fairly terrible idea to do this in SQL. One of the basic tenets of SQL query outputs is that row count is variable, column count is fixed. To do this in SQL you'll have to write a dynamic pivoting sql that has all the possible columns at the time of the query.. And dynamic sql is also a terrible idea
– Caius Jard
Nov 14 '18 at 11:44
|
show 4 more comments
I am working on a query which contains multiple rows per a particular id and im looking to display them all on one line.
The query I currently have is :
SELECT
person_id,
car_name,
car_year,
car_value,
car_mileage
FROM tbl_motor_vehicles
This returns the below:
person_id car_name car_year car_value car_mileage
---------------------------------------------------------------------
1 TOYOTA YARIS 2017 5000 10000
1 Nissan Micra 2001 1000 15000
2 Golf 15000 700000
2 Fiat Punto 2002 2500 60000
I need this to display on one line per id with each car getting its own column like below :
Person ID Car_1_NAME Car_1_year car1_value car1_mileage Car_2_NAME Car_2_year car2_value car2_mileage
--------------------------------------------------------------------------------------------------------------------------------------
1 TOYOTA YARIS 2017 5000 10000 Nissan Micra 2001 1000 15000
2 Golf 2007 15000 700000 Fiat Punto 2002 2500 60000
Can anyone advise me how to go about this ?
sql sql-server grouping
I am working on a query which contains multiple rows per a particular id and im looking to display them all on one line.
The query I currently have is :
SELECT
person_id,
car_name,
car_year,
car_value,
car_mileage
FROM tbl_motor_vehicles
This returns the below:
person_id car_name car_year car_value car_mileage
---------------------------------------------------------------------
1 TOYOTA YARIS 2017 5000 10000
1 Nissan Micra 2001 1000 15000
2 Golf 15000 700000
2 Fiat Punto 2002 2500 60000
I need this to display on one line per id with each car getting its own column like below :
Person ID Car_1_NAME Car_1_year car1_value car1_mileage Car_2_NAME Car_2_year car2_value car2_mileage
--------------------------------------------------------------------------------------------------------------------------------------
1 TOYOTA YARIS 2017 5000 10000 Nissan Micra 2001 1000 15000
2 Golf 2007 15000 700000 Fiat Punto 2002 2500 60000
Can anyone advise me how to go about this ?
sql sql-server grouping
sql sql-server grouping
asked Nov 14 '18 at 11:22
Farrea69Farrea69
81
81
Will each person have at most 2 cars?
– Henning Koehler
Nov 14 '18 at 11:24
welcome to SO. please have a look at how-to-ask What you are looking for is PIVOT. Please use the search for PIVOT, you will find MANY answers
– swe
Nov 14 '18 at 11:24
1
I wouldn't use SQL for this. Use the programming language of your Website or app to care about the grid layout.
– Thorsten Kettner
Nov 14 '18 at 11:25
No , there is no set amount of cars . Also , there is no fixed amount of person ids either so cant be hardcoded
– Farrea69
Nov 14 '18 at 11:30
2
No , there is no set amount of cars . Also , there is no fixed amount of person ids either so cant be hardcoded - It's a fairly terrible idea to do this in SQL. One of the basic tenets of SQL query outputs is that row count is variable, column count is fixed. To do this in SQL you'll have to write a dynamic pivoting sql that has all the possible columns at the time of the query.. And dynamic sql is also a terrible idea
– Caius Jard
Nov 14 '18 at 11:44
|
show 4 more comments
Will each person have at most 2 cars?
– Henning Koehler
Nov 14 '18 at 11:24
welcome to SO. please have a look at how-to-ask What you are looking for is PIVOT. Please use the search for PIVOT, you will find MANY answers
– swe
Nov 14 '18 at 11:24
1
I wouldn't use SQL for this. Use the programming language of your Website or app to care about the grid layout.
– Thorsten Kettner
Nov 14 '18 at 11:25
No , there is no set amount of cars . Also , there is no fixed amount of person ids either so cant be hardcoded
– Farrea69
Nov 14 '18 at 11:30
2
No , there is no set amount of cars . Also , there is no fixed amount of person ids either so cant be hardcoded - It's a fairly terrible idea to do this in SQL. One of the basic tenets of SQL query outputs is that row count is variable, column count is fixed. To do this in SQL you'll have to write a dynamic pivoting sql that has all the possible columns at the time of the query.. And dynamic sql is also a terrible idea
– Caius Jard
Nov 14 '18 at 11:44
Will each person have at most 2 cars?
– Henning Koehler
Nov 14 '18 at 11:24
Will each person have at most 2 cars?
– Henning Koehler
Nov 14 '18 at 11:24
welcome to SO. please have a look at how-to-ask What you are looking for is PIVOT. Please use the search for PIVOT, you will find MANY answers
– swe
Nov 14 '18 at 11:24
welcome to SO. please have a look at how-to-ask What you are looking for is PIVOT. Please use the search for PIVOT, you will find MANY answers
– swe
Nov 14 '18 at 11:24
1
1
I wouldn't use SQL for this. Use the programming language of your Website or app to care about the grid layout.
– Thorsten Kettner
Nov 14 '18 at 11:25
I wouldn't use SQL for this. Use the programming language of your Website or app to care about the grid layout.
– Thorsten Kettner
Nov 14 '18 at 11:25
No , there is no set amount of cars . Also , there is no fixed amount of person ids either so cant be hardcoded
– Farrea69
Nov 14 '18 at 11:30
No , there is no set amount of cars . Also , there is no fixed amount of person ids either so cant be hardcoded
– Farrea69
Nov 14 '18 at 11:30
2
2
No , there is no set amount of cars . Also , there is no fixed amount of person ids either so cant be hardcoded - It's a fairly terrible idea to do this in SQL. One of the basic tenets of SQL query outputs is that row count is variable, column count is fixed. To do this in SQL you'll have to write a dynamic pivoting sql that has all the possible columns at the time of the query.. And dynamic sql is also a terrible idea
– Caius Jard
Nov 14 '18 at 11:44
No , there is no set amount of cars . Also , there is no fixed amount of person ids either so cant be hardcoded - It's a fairly terrible idea to do this in SQL. One of the basic tenets of SQL query outputs is that row count is variable, column count is fixed. To do this in SQL you'll have to write a dynamic pivoting sql that has all the possible columns at the time of the query.. And dynamic sql is also a terrible idea
– Caius Jard
Nov 14 '18 at 11:44
|
show 4 more comments
1 Answer
1
active
oldest
votes
A SQL query has a fixed number of columns. So, you can only show a fixed set of cars for each person. With that caveat, you can use conditional aggregation:
SELECT person_id,
MAX(CASE WHEN seqnum = 1 THEN car_name END) as car_name_1,
MAX(CASE WHEN seqnum = 1 THEN car_year END) as car_year_1,
MAX(CASE WHEN seqnum = 1 THEN car_value END) as car_value_1,
MAX(CASE WHEN seqnum = 1 THEN car_mileage END) as car_mileage_1,
MAX(CASE WHEN seqnum = 2 THEN car_name END) as car_name_2,
MAX(CASE WHEN seqnum = 2 THEN car_year END) as car_year_2,
MAX(CASE WHEN seqnum = 2 THEN car_value END) as car_value_2,
MAX(CASE WHEN seqnum = 2 THEN car_mileage END) as car_mileage_2
FROM (SELECT mv.*, ROW_NUMBER() OVER (PARTITION BY person_id ORDER BY (SELECT NULL)) as seqnum
FROM tbl_motor_vehicles mv
) mv
GROUP BY person_id;
this worked how I wanted , cheers
– Farrea69
Nov 14 '18 at 12:16
add a comment |
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',
autoActivateHeartbeat: false,
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53299060%2fhow-to-display-multiple-rows-on-single-row%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
A SQL query has a fixed number of columns. So, you can only show a fixed set of cars for each person. With that caveat, you can use conditional aggregation:
SELECT person_id,
MAX(CASE WHEN seqnum = 1 THEN car_name END) as car_name_1,
MAX(CASE WHEN seqnum = 1 THEN car_year END) as car_year_1,
MAX(CASE WHEN seqnum = 1 THEN car_value END) as car_value_1,
MAX(CASE WHEN seqnum = 1 THEN car_mileage END) as car_mileage_1,
MAX(CASE WHEN seqnum = 2 THEN car_name END) as car_name_2,
MAX(CASE WHEN seqnum = 2 THEN car_year END) as car_year_2,
MAX(CASE WHEN seqnum = 2 THEN car_value END) as car_value_2,
MAX(CASE WHEN seqnum = 2 THEN car_mileage END) as car_mileage_2
FROM (SELECT mv.*, ROW_NUMBER() OVER (PARTITION BY person_id ORDER BY (SELECT NULL)) as seqnum
FROM tbl_motor_vehicles mv
) mv
GROUP BY person_id;
this worked how I wanted , cheers
– Farrea69
Nov 14 '18 at 12:16
add a comment |
A SQL query has a fixed number of columns. So, you can only show a fixed set of cars for each person. With that caveat, you can use conditional aggregation:
SELECT person_id,
MAX(CASE WHEN seqnum = 1 THEN car_name END) as car_name_1,
MAX(CASE WHEN seqnum = 1 THEN car_year END) as car_year_1,
MAX(CASE WHEN seqnum = 1 THEN car_value END) as car_value_1,
MAX(CASE WHEN seqnum = 1 THEN car_mileage END) as car_mileage_1,
MAX(CASE WHEN seqnum = 2 THEN car_name END) as car_name_2,
MAX(CASE WHEN seqnum = 2 THEN car_year END) as car_year_2,
MAX(CASE WHEN seqnum = 2 THEN car_value END) as car_value_2,
MAX(CASE WHEN seqnum = 2 THEN car_mileage END) as car_mileage_2
FROM (SELECT mv.*, ROW_NUMBER() OVER (PARTITION BY person_id ORDER BY (SELECT NULL)) as seqnum
FROM tbl_motor_vehicles mv
) mv
GROUP BY person_id;
this worked how I wanted , cheers
– Farrea69
Nov 14 '18 at 12:16
add a comment |
A SQL query has a fixed number of columns. So, you can only show a fixed set of cars for each person. With that caveat, you can use conditional aggregation:
SELECT person_id,
MAX(CASE WHEN seqnum = 1 THEN car_name END) as car_name_1,
MAX(CASE WHEN seqnum = 1 THEN car_year END) as car_year_1,
MAX(CASE WHEN seqnum = 1 THEN car_value END) as car_value_1,
MAX(CASE WHEN seqnum = 1 THEN car_mileage END) as car_mileage_1,
MAX(CASE WHEN seqnum = 2 THEN car_name END) as car_name_2,
MAX(CASE WHEN seqnum = 2 THEN car_year END) as car_year_2,
MAX(CASE WHEN seqnum = 2 THEN car_value END) as car_value_2,
MAX(CASE WHEN seqnum = 2 THEN car_mileage END) as car_mileage_2
FROM (SELECT mv.*, ROW_NUMBER() OVER (PARTITION BY person_id ORDER BY (SELECT NULL)) as seqnum
FROM tbl_motor_vehicles mv
) mv
GROUP BY person_id;
A SQL query has a fixed number of columns. So, you can only show a fixed set of cars for each person. With that caveat, you can use conditional aggregation:
SELECT person_id,
MAX(CASE WHEN seqnum = 1 THEN car_name END) as car_name_1,
MAX(CASE WHEN seqnum = 1 THEN car_year END) as car_year_1,
MAX(CASE WHEN seqnum = 1 THEN car_value END) as car_value_1,
MAX(CASE WHEN seqnum = 1 THEN car_mileage END) as car_mileage_1,
MAX(CASE WHEN seqnum = 2 THEN car_name END) as car_name_2,
MAX(CASE WHEN seqnum = 2 THEN car_year END) as car_year_2,
MAX(CASE WHEN seqnum = 2 THEN car_value END) as car_value_2,
MAX(CASE WHEN seqnum = 2 THEN car_mileage END) as car_mileage_2
FROM (SELECT mv.*, ROW_NUMBER() OVER (PARTITION BY person_id ORDER BY (SELECT NULL)) as seqnum
FROM tbl_motor_vehicles mv
) mv
GROUP BY person_id;
answered Nov 14 '18 at 11:58
Gordon LinoffGordon Linoff
784k35310415
784k35310415
this worked how I wanted , cheers
– Farrea69
Nov 14 '18 at 12:16
add a comment |
this worked how I wanted , cheers
– Farrea69
Nov 14 '18 at 12:16
this worked how I wanted , cheers
– Farrea69
Nov 14 '18 at 12:16
this worked how I wanted , cheers
– Farrea69
Nov 14 '18 at 12:16
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53299060%2fhow-to-display-multiple-rows-on-single-row%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Will each person have at most 2 cars?
– Henning Koehler
Nov 14 '18 at 11:24
welcome to SO. please have a look at how-to-ask What you are looking for is PIVOT. Please use the search for PIVOT, you will find MANY answers
– swe
Nov 14 '18 at 11:24
1
I wouldn't use SQL for this. Use the programming language of your Website or app to care about the grid layout.
– Thorsten Kettner
Nov 14 '18 at 11:25
No , there is no set amount of cars . Also , there is no fixed amount of person ids either so cant be hardcoded
– Farrea69
Nov 14 '18 at 11:30
2
No , there is no set amount of cars . Also , there is no fixed amount of person ids either so cant be hardcoded - It's a fairly terrible idea to do this in SQL. One of the basic tenets of SQL query outputs is that row count is variable, column count is fixed. To do this in SQL you'll have to write a dynamic pivoting sql that has all the possible columns at the time of the query.. And dynamic sql is also a terrible idea
– Caius Jard
Nov 14 '18 at 11:44