我需要以“spreadheety”类型的方式显示数据库中的数据,其中值垂直显示而不是水平显示为行,通常就是这种情况。
网格需要正好是16列乘12行。在正常情况下,它将是两列,最多96行。
我从ClearLogic的答案中得出的查询:
Is there a SQL statement that will break what would be 2 long columns into several pairs of columns?
..非常接近,但是它产生了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 = //查询结果为OracleDataTable
搞定了。见Is it possible to populate a DataGridView with alternating vertical columns?
答案 0 :(得分:1)
我认为显示错误原因的答案源于您的数据。如果您的行具有Val1的每个可能值,对于您当前没有val1的行,Val2为null,我希望显示正确。我认为问题的出现是因为不存在需要(在我看来)存在的行才能使格式化正确。
我很欣赏这可能过于积极,因为你可能并不总是拥有val1的所有值 - 但是有一种解决方法:
如果你创建一个从1到96的整数表,让我们调用这个tblArray,然后你的from子句可以是:
FROM tblArray t LEFT JOIN PlatypusAvailability p ON t.ID = p.Val1
&安培;应该做的工作。但我建议先在第一段中测试我的想法。