I have data that, in a “normal” query, would appear like this:
Val1 Val2
---- ----
1 2
2 [blank]
3 2
4
5 1
6 3
..
96 1
What I want, though, is something like this (I need to limit the number of rows to 12):
Val1 Val2 Val1 Val2 Val1 Val2 ... Val1 Val2
---- ---- ---- ---- ---- ----
1 2 13 1 25 [blank] ... 85 1
2 [blank] 14 1 26 3 ... 86 [blank]
.. ... ... ... ... ... ... ... ...
12 1 24 [blank] 36 2 ... 96 3
Is there a select statement that would give me that? I’m no SQL expert, but I’m thinking something (semantically) along these lines:
select (select val1, val2 from dbtable where val1 < 13),
(select val1, val2 from dbtable where val1 > 12 and val1 < 25),
...
(select val1, val2 from dbtable where val1 > 84)
from dbtable
UPDATE
In response to dfb’s sql example:
When I do this:
SELECT t1.Val1, t1.Val2 FROM
(SELECT Val1, Val2, rownum() as rownum FROM dbTable) t1
INNER JOIN (SELECT Val1, Val2, rownum() as rownum FROM dbTable) t2
ON t1.rownum/2 == t2.rownum/2
…I get “FROM keyword not found where expected”
And when I do this (remove the “rownum()” stuff):
SELECT t1.Val1, t1.Val2 FROM
(SELECT Val1, Val2 FROM dbTable) t1
INNER JOIN (SELECT Val1, Val2 FROM dbTable) t2
ON t1.rownum/2 == t2.rownum/2
…I get “ORA-01747: invalid user.table.column, table.column, or column
specification”
UPDATE 2
Sully’s example came the closest, although I wish the UNION SQL would work – it would be better if it could be done without pushing down the valid values. As it is, I have the right layout but the vals are not appearing just where I need them to within that 16X12 layout. At any rate, for posterity’s sake, here’s how the Rows and Columns are dynamically created (not as shown in the code below, and not identical to each other):
//prebuild 12 rows in outputDt
int iRows = 12;
while (iRows > 0)
{
DataRow row = outputDt.NewRow();
outputDt.Rows.Add(row);
iRows -= 1;
}
//prebuild 16 cols in outputDt
int iCols = 16;
while (iCols > 0) {
DataColumn col = new DataColumn();
outputDt.Columns.Add(col);
iCols -= 1;
}
FINAL UPDATE
Got it working. See Is it possible to populate a DataGridView with alternating vertical columns?
Bonus to this is if you end up with more data, it’ll just build more horizontal columns as needed but never go over 12 rows of data. The “in-SQL” way will require code changes if you ever need to display more data.
Disclaimer: This is totally off-the-cuff (C# as that’s what I’m used to). There are probably much better ways to do this (Linq?) The logic should be pretty close, but this gives you the flexibility to use that list of data for other purposes than this very narrowly focused display.
ALTERNATE version: For requirement that val1 == 48 goes in cell 48 (see comments)