0

我需要以“电子表格”类型的方式显示数据库中的数据,值垂直显示,而不是像通常那样水平显示为行。

网格需要正好是 16 列乘 12 行。在正常情况下,它将是两列,最多 96 行。

我从 ClearLogic 的答案中得出的查询:

是否有一条 SQL 语句可以将 2 个长列分成几对列?

..非常接近,但它生成了 15 列,并添加了两次值。它看起来足够近,让我想继续沿着这条路走下去。

注意:Val1 始终是 1..96 之间的值

我的 sql(产生 16 列乘 15 行的结果集)是:

Select Val1 AS col1, Val2 AS col2,NULL AS Col3,NULL AS Col4 ,NULL AS 
Col5,NULL AS Col6,NULL AS col7,NULL AS Col8, NULL AS Col9, NULL AS Col10, NULL AS 
Col11, NULL AS Col12, NULL AS Col13, NULL AS Col14, NULL AS Col15, NULL AS Col16 
from PLATYPUSAVAILABILITY where Val1 < 13 and PLATYPUSID = 42
AND AVAILABLEDATE = to_date('20120823', 'yyyymmdd')

UNION select NULL AS col1,NULL AS col2,Val1 AS Col3, Val2 AS Col4,NULL 
AS Col5,NULL AS Col6,NULL AS col7,NULL AS Col8, NULL AS Col9, NULL AS Col10, NULL AS
Col11, NULL AS Col12, NULL AS Col13, NULL AS Col14, NULL AS Col15, NULL AS Col16 
from PLATYPUSAVAILABILITY where Val1 > 12 and Val1 < 25 and 
PLATYPUSID = 42 AND AVAILABLEDATE = to_date('20120823', 'yyyymmdd')

UNION select NULL AS col1,NULL AS col2 ,NULL AS Col3, NULL AS Col4,Val1 AS 
Col5, Val2 AS Col6,NULL AS col7,NULL AS Col8, NULL AS Col9, NULL AS Col10, NULL 
AS Col11, NULL AS Col12, NULL AS Col13, NULL AS Col14, NULL AS Col15, NULL AS Col16 
from PLATYPUSAVAILABILITY where Val1 > 24 and Val1 < 37 and 
PLATYPUSID = 42 AND AVAILABLEDATE = to_date('20120823', 'yyyymmdd')

UNION select NULL AS col1,NULL AS col2 ,NULL AS Col3, NULL AS Col4, NULL AS 
Col5, NULL AS Col6,Val1 AS col7,Val2 AS Col8, NULL AS Col9, NULL AS 
Col10, NULL AS Col11, NULL AS Col12, NULL AS Col13, NULL AS Col14, NULL AS Col15, 
NULL AS Col16 from PLATYPUSAVAILABILITY where Val1 > 36 and Val1 
< 49 and PLATYPUSID = 42 AND AVAILABLEDATE = to_date('20120823', 'yyyymmdd')

UNION select NULL AS col1,NULL AS col2 ,NULL AS Col3, NULL AS Col4,Val1 AS 
Col5,Val2 AS Col6,NULL AS col7,NULL AS Col8, Val1 AS Col9, Val2 AS 
Col10, NULL AS Col11, NULL AS Col12, NULL AS Col13, NULL AS Col14, NULL AS Col15, 
NULL AS Col16 from PLATYPUSAVAILABILITY where Val1 > 48 and Val1 
< 61 and PLATYPUSID = 42 AND AVAILABLEDATE = to_date('20120823', 'yyyymmdd')

UNION select NULL AS col1,NULL AS col2 ,NULL AS Col3, NULL AS Col4,NULL AS 
Col5,NULL AS Col6,NULL AS col7,NULL AS Col8, NULL AS Col9, NULL AS Col10, 
Val1 AS Col11, Val2 AS Col12, NULL AS Col13, NULL AS Col14, NULL AS 
Col15, NULL AS Col16 from PLATYPUSAVAILABILITY where Val1 > 60 and 
Val1 < 73 and PLATYPUSID = 42    AND AVAILABLEDATE = to_date('20120823', 'yyyymmdd')

UNION select NULL AS col1,NULL AS col2 ,NULL AS Col3, NULL AS Col4,NULL AS 
Col5,NULL AS Col6,NULL AS col7,NULL AS Col8, NULL AS Col9, NULL AS Col10, NULL AS 
Col11, NULL AS Col12, Val1 AS Col13, Val2 AS Col14, NULL AS Col15, NULL 
AS Col16 from PLATYPUSAVAILABILITY where Val1 > 72 and Val1 < 85 
and PLATYPUSID = 42 AND AVAILABLEDATE = to_date('20120823', 'yyyymmdd') 

UNION select NULL AS col1,NULL AS col2 ,NULL AS Col3, NULL AS Col4,NULL AS 
Col5,NULL AS Col6,NULL AS col7,NULL AS Col8, NULL AS Col9, NULL AS Col10, NULL AS 
Col11, NULL AS Col12, NULL AS Col13, NULL AS Col14, Val1 AS Col15, Val2 
AS Col16 from PLATYPUSAVAILABILITY where Val1 > 84 and PLATYPUSID = 42 
AND AVAILABLEDATE = to_date('20120823', 'yyyymmdd')

我怎样才能让它自己限制在 12 行而不是溢出到 15 行?

我在 TOAD 中运行此查询后看到的屏幕截图可以在以下位置看到:

http://warbler.posterous.com/so-close-and-yet-so-far-away-sqlizers-lament

应该只有 12 行,并且值应该从第 9 列和第 10 列的顶部开始,并溢出到第 11 列和第 12 列的一行(顶部)。

我使用 Datagridview 作为标签,因为这是数据的最终目标:datagridview1.DataSource = //query result as OracleDataTable

更新

得到它的工作。请参阅是否可以使用交替的垂直列填充 DataGridView?

4

1 回答 1

1

我认为为什么显示错误的问题的答案源于您的数据。如果您有所有可能值为 Val1 的行,而当前没有 val1 的行的 Val2 为 null,我希望显示是正确的。我认为问题的出现是因为不存在需要(在我看来)存在以使格式正确的行。

我很欣赏这可能不会过于积极,因为无论如何您可能并不总是拥有 val1 的所有值 - 但有一种方法可以解决这个问题:

如果你创建一个从 1 到 96 的整数表,让我们调用这个 tblArray,那么你的 from 子句可以是:

 FROM tblArray t LEFT JOIN PlatypusAvailability p ON t.ID = p.Val1

&这应该可以完成工作。但我建议先在第一段中测试我的想法。

于 2012-08-23T03:32:32.503 回答