MSSQL查询从DB检索数据

时间:2014-02-13 06:16:32

标签: sql sql-server sql-server-2008

我需要在以下形式的UI中使用数据网格

        | Col1 | Col2 | Col3 | Col4 |
--------------------------------------
        |      |      |      |      |
 Row1   |  10  |  20  |  30  |  40  |
        |      |      |      |      |
 Row2   |  50  |  60  |  70  |  80  |
        |      |      |      |      |
 Row3   |  90  |  100 |  110 |  120 |
        |      |      |      |      |
 Row4   |  130 |  140 |  150 |  160 |

上述DataGrid的架构以

的形式存储在数据库中

表T1

  ID  |  Description  |   DimensionType
-------------------------------------------
 101  |     Row1     |        1
 102  |     Row2     |        1
 103  |     Row3     |        1
 104  |     Row4     |        1
 105  |     Col1     |        2
 106  |     Col2     |        2
 107  |     Col3     |        2
 108  |     Col4     |        2

在上表中DimensionType表示描述是行还是列。 DimensionType = 1表示rowDimensionType = 2表示column

存储在DB中的值如下

表T2

  ID  |  T1R   |  T1C    |   Value
----------------------------------
 1001 |  101   |   105   |    10
 1002 |  101   |   106   |    20
 1003 |  101   |   107   |    30
 1004 |  101   |   108   |    40
 1005 |  102   |   105   |    50
 1006 |  102   |   106   |    60
 1007 |  102   |   107   |    70
 1008 |  102   |   108   |    80 
 .
 .
 .
 an so on.

我希望以下列形式检索数据。

   Row  |    C1    |  Value  |    C2    |  Value  |    C3    |  Value  |    C4    |  Value  |
 --------------------------------------------------------------------------------------------
        |          |         |          |         |          |         |          |         |
   Row1 |   Col1   |    10   |   Col2   |    20   |   Col3   |    30   |   Col4   |    40   |
   Row2 |   Col1   |    50   |   Col2   |    60   |   Col3   |    70   |   Col4   |    80   |
   Row3 |   Col1   |    90   |   Col2   |   100   |   Col3   |   110   |   Col4   |   120   |
   Row4 |   Col1   |   130   |   Col2   |   140   |   Col3   |   150   |   Col4   |   160   |
        |          |         |          |         |          |         |          |         |

需要编写一个可以打印上述格式数据的查询(在MSSQL中)。如果可以进一步优化检索,那么它将更有用,即形式

  Row  |    Col1  |  Col2   |  Col3   |  Col4    |
--------------------------------------------------
       |          |         |         |          |
  Row1 |     10   |    20   |    30   |    40    |
  Row2 |     50   |    60   |    70   |    80    |
  Row3 |     90   |   100   |   110   |   120    |
  Row4 |    130   |   140   |   150   |   160    |
       |          |         |         |          |

提前致谢!!

1 个答案:

答案 0 :(得分:0)

未经测试,但试试这个(我面前没有MS SQL,至少一年没用过,所以这段代码是盲目写的):

select t2id, t2t1r, t2t1c, rdesc, 
  STUFF((SELECT ', ' + Value from t ttemp where ttemp.t2t1r = t.t2t1r FOR XML PATH('')), 1, 2, '')  as ConcatenatedValues, 
  STUFF((SELECT ', ' + cdesc from t ttemp where ttemp.t2t1r = t.t2t1r FOR XML PATH('')), 1, 2, '')  as cdescs, 
from
(select T2.ID as t2id, T2.T1R as t2t1r, T2.T1C as t2t1c, Value, row.Description as rdesc, col.Description as cdesc
from T1 join T2 row
on T1.ID = row.T1R and row.DimensionType = 1
join T2 col
on T1.ID = col.T1C and col.DimensionType = 2) t