Solved

How to get several rows in a table into one row in my result set?

Posted on 2008-06-10
11
167 Views
Last Modified: 2011-10-19
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
0
Comment
Question by:kimfink
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 21749438
Hello kimfink,

SELECT a.* FROM urTable a
WHERE A.TestDateTime = (SELECT MAX(TestDateTime) from urTable WHERE ConfigurationID = a.ConfigurationID )


GOD Bless,
Aneesh R.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21749441
if you only want the select ConfigurationID column, it's easy (see below).

now, what about the other columns?
select ConfigurationID
   from yourtable
group by ConfigurationID

Open in new window

0
 

Author Comment

by:kimfink
ID: 21749491
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.
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21749506
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.
0
 
LVL 5

Expert Comment

by:ursangel
ID: 21749520
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
0
 

Author Comment

by:kimfink
ID: 21749536
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
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21749563
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)?
0
 

Author Comment

by:kimfink
ID: 21749603
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...
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21749662
please!
post the FINAL requested output, in data (not in words).
0
 

Author Comment

by:kimfink
ID: 21749688
See my last attachement in my previous post. Row 2 is what i want!!!
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 21749745
... ok...
can you show us the query you used to produce that result?


anyhow, it might be like this (for the data)
if that is ok for the output, replace the ESD.* and PSD.* by the column names with the alias names you want, eg.

SELECT ESD.ConfigurationID
, ESD.Type ESD, ESD.TestTemperature ESD_TestTemperature
, PSD.Type PSD, PSD.TestTemperature PSD_TestTemperature
...
 from yourtable ESD
  join yourtable PSD
    ON ESD.configurationID = PSD.ConfigurationID
   AND PSD.Type = 'PSD'
  WHERE ESD.Type = 'ESD'

select ESD.*, PSD.*
  from yourtable ESD
  join yourtable PSD
    ON ESD.configurationID = PSD.ConfigurationID
   AND PSD.Type = 'PSD'
  WHERE ESD.Type = 'ESD' 

Open in new window

0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question