How to get data from one row to be returned in one row when queried in DataGridView.

Hi all,

Sorry if this is in the wrong forum...wasn't really sure where to put it. Also, sorry for the poor title...I wasn't sure how to describe this succinctly. :\

Anyway, I have a database that, among others, has these two tables (I hope I'm explaining this well enough):

Table: Statuses
Row: ID, StatusName
i.e.:
1, TBD
2, In Progress

Table: CurrentTestStatus
Row: ID, TestSet_1, Status_ID_1, TestSet_2, Status_ID_2
i.e.: 1, SQL, 1, Pro-ISAM, 2

I also have a web page, with a DataGridView that displays CurrentTestStatus...Like this:
ID, TestSet_1, StatusName, TestSet_2, StatusName
i.e.: 1, SQL, TBD, Pro-ISAM, In Progress

The problem I'm having is:
If Status_ID_1 and Status_ID_2 match, my data for that TestStuatus ID is returned in one row.
i.e.: 1, SQL, TBD, Pro-ISAM, TBD
If Status_ID_1 and Status_ID_2 do not match, my data is returned in two rows.
i.e.:
1, SQL, TBD
1, Pro-ISAM, TBD

No matter what tinkering I do with the queries, I always get two rows when the Status ID's don't match.
How can I get the data to be returned in 1 row every time?

THANK YOU!!!
Chris
September 11th, 2015 2:02pm

When you ask SQL question, it will help when you use DDL for the tables and present your sample data in the form of INSERTs and your expected result. Thanks.
Free Windows Admin Tool Kit Click here and download it now
September 11th, 2015 2:07pm

I'm sorry...I'm new here, and to this. I don't really understand what you mean. :(
How can I change this so that it is proper?
September 11th, 2015 2:09pm

select T.*, St1.[StatusName] as StatusName1, St2.[StatusName] as StatusName2

from CurrentTestStatus t

OUTER APPLY (select StatusName from Statuses s where s.Id = t.Status_ID_1) St1

OUTER APPLY (select StatusName from Statuses s1 where s1.Id = t.Status_Id_2) St2

Free Windows Admin Tool Kit Click here and download it now
September 11th, 2015 2:10pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics