kimfink
asked on
How to get several rows in a table into one row in my result set?
Hi,
I have a table with several rows. One of the columns is an "id" column (in the attached picture this is the ConfigurationId column). Those rows (it can be 1 row or it could be 3, or x number of rows) who have the same value in the ConfigurationId column I want to be on one row in my query result.
How can I accomplish this?
database-snapshot-sql-main-repor.bmp
I have a table with several rows. One of the columns is an "id" column (in the attached picture this is the ConfigurationId column). Those rows (it can be 1 row or it could be 3, or x number of rows) who have the same value in the ConfigurationId column I want to be on one row in my query result.
How can I accomplish this?
database-snapshot-sql-main-repor.bmp
if you only want the select ConfigurationID column, it's easy (see below).
now, what about the other columns?
now, what about the other columns?
select ConfigurationID
from yourtable
group by ConfigurationID
ASKER
Hi again, let me precise my question.
Those rows who have the same value in the ConfigurationId column, I want to be joined into on one row and exactly one row in my query result.
Those rows who have the same value in the ConfigurationId column, I want to be joined into on one row and exactly one row in my query result.
yes, but which of the rows?
see, for each of the other columns, have different values.
so, what is the criteria for those values.
aneeshattingal posted the query to get the "latest" record based on the datetime column.
however, that might not be what you are looking for.
if you don't "see" what we are asking for, post the requested output.
see, for each of the other columns, have different values.
so, what is the criteria for those values.
aneeshattingal posted the query to get the "latest" record based on the datetime column.
however, that might not be what you are looking for.
if you don't "see" what we are asking for, post the requested output.
hi,
I agree with Angelll's reply. If it is just the ConfigurationID column you want to select, them grouping by that coulmn will give you the result.
But if you want the other coulmn like TYPE along with the select, then it will defnitely give you 2 rows.
For eg:- TYPE has 2 values for the CONFIGURATIONID = 1.
TYPE = ESD and PSD.
So in this case, you will have to go for looping and get the result set in a comma seperated value or something like that.
Please do let us know ur requirement
I agree with Angelll's reply. If it is just the ConfigurationID column you want to select, them grouping by that coulmn will give you the result.
But if you want the other coulmn like TYPE along with the select, then it will defnitely give you 2 rows.
For eg:- TYPE has 2 values for the CONFIGURATIONID = 1.
TYPE = ESD and PSD.
So in this case, you will have to go for looping and get the result set in a comma seperated value or something like that.
Please do let us know ur requirement
ASKER
I want all the different rows with the same ConfigurationId, "joined" into exactly one row with all columns for the different rows with the same ConfigurationId.
As you can see in the attachment, but here I have used a regular join, so I also got rows that I don't want.
database-snapshot-sql-main-repor.bmp
As you can see in the attachment, but here I have used a regular join, so I also got rows that I don't want.
database-snapshot-sql-main-repor.bmp
kimfink:
let me repeat: in the 2 rows, you have 1 row with TYPE = ESD and 1 row with TYPE = PSD.
so, what do you want to have in the column TYPE in the resulting row?
* ESD
* PSD
* ESD, PSD
in case you don't want to have both values, what will be the rule to choose ESD over PSD (or vice-versa)?
let me repeat: in the 2 rows, you have 1 row with TYPE = ESD and 1 row with TYPE = PSD.
so, what do you want to have in the column TYPE in the resulting row?
* ESD
* PSD
* ESD, PSD
in case you don't want to have both values, what will be the rule to choose ESD over PSD (or vice-versa)?
ASKER
In my result set I would like a colum for both ESD and PSD, and if I could alias the columnames in the query, that would be great. This alos yields for the other columns in the two rows. I don't want the values in a comma seperated list in one colum.
E.g. what i want is to join these two rows (as you could see in my last attachment), but when I join these two rows, I get 4 rows, and it is only one of these rows that I am intrested in, and that I want in my result set. This may not be possible...
E.g. what i want is to join these two rows (as you could see in my last attachment), but when I join these two rows, I get 4 rows, and it is only one of these rows that I am intrested in, and that I want in my result set. This may not be possible...
please!
post the FINAL requested output, in data (not in words).
post the FINAL requested output, in data (not in words).
ASKER
See my last attachement in my previous post. Row 2 is what i want!!!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SELECT a.* FROM urTable a
WHERE A.TestDateTime = (SELECT MAX(TestDateTime) from urTable WHERE ConfigurationID = a.ConfigurationID )
GOD Bless,
Aneesh R.