如何将SQL Server列转换为行?

时间:2019-02-22 15:00:01

标签: sql sql-server tsql pivot

我需要帮助来切换SQL中带有行的列。需要打开这个:

Private Sub CommandButton2_Click()
    Dim wb1 As Workbook         ' first workbook
    Dim wb2 As Workbook         ' second workbook
    Dim wsCheck As Worksheet    ' sheet in the first workbook
    Dim wsOrderlog As Worksheet ' sheet in the second workbook

    ' address the first workbook and its sheet
    ' if this VBA-code is in the frist workbook, it's "ThisWorkbook"
    Set wb1 = ThisWorkbook
    Set wsCheck = wb1.Worksheets("Worksheet")

    ' check, if second workbook is already open
    For Each wb2 In Workbooks
        If wb2.Name = "Protective Packaging Order Log.xlsm" Then Exit For
    Next wb2

    ' if not already open, then open it, and address its sheet also
    If wb2 Is Nothing Then
        Set wb2 = Workbooks.Open( _
            Filename:="G:\General\COVERSHEET_Protective\Protective Packaging Order Log.xlsm", _
            Password:="PP", _
            WriteResPassword:="PP")
    End If
    Set wsOrderlog = wb2.Worksheets("orderlog")

    ' search a value from the first workbook's sheet within second workbook's sheet
    Dim FoundRow As Variant
    FoundRow = Application.Match(wsCheck.Range("G3").Value, wsOrderlog.Range("A:A"), 0)
    If IsNumeric(FoundRow) Then ' if found
        ' please adapt to your needs:
        wsOrderlog.Cells(FoundRow, 1).Font.Size = 14
        wsOrderlog.Cells(FoundRow, 9).Value = wsCheck.Range("I10").Value
        wsOrderlog.Cells(FoundRow, 10).Value = wsCheck.Range("I11").Value
    Else
        MsgBox "Sorry, the value in cell G3" & vbLf & _
            wsCheck.Range("G3").Value & vbLf & _
            "could not be found in orderlog column A."
    End If

    ' close the second workbook (Excel will ask, if to save)
    wb2.Close
End Sub

对此:

+------------+------------+-------------+------+
|    Date    | Production | Consumption | .... |
+------------+------------+-------------+------+
| 2017-01-01 |        100 |        1925 |      |
| 2017-01-02 |        200 |        2005 |      |
| 2017-01-03 |        150 |        1998 |      |
| 2017-01-04 |        250 |        2200 |      |
| 2017-01-05 |         30 |         130 |      |
|...         |            |             |      |
+------------+------------+-------------+------+

有人可以帮助我吗?我应该使用+------------+------------+------------+------------+------------+-----+ | 01-01-2017 | 02-01-2017 | 03-01-2017 | 04-01-2017 | 05-01-2017 | ... | +------------+------------+------------+------------+------------+-----+ | 100 | 200 | 150 | 250 | 30 | | | 1925 | 2005 | 1998 | 2200 | 130 | | +------------+------------+------------+------------+------------+-----+ 吗?

编辑:我尝试使用一些建议,例如PIVOTPIVOT,但是我无法达到预期的结果。

我尝试过:

UNPIVOT

但是通过上面的查询,我只能设法得到一些所需的信息,

SELECT *
FROM (
    SELECT date, Consumption
    FROM Energy
    where date < '2017-02-01'
) r
pivot (sum(Consumption) for date in ([2017-01-01],[2017-01-02],[2017-01-03]....)) c
order by 1

我需要在同一查询中同时拥有生产和消费,但是我只能得到其中之一。

是否可以在+------------+------------+------------+------------+------------+-----+ | 01-01-2017 | 02-01-2017 | 03-01-2017 | 04-01-2017 | 05-01-2017 | ... | +------------+------------+------------+------------+------------+-----+ | 100 | 200 | 150 | 250 | 30 | | +------------+------------+------------+------------+------------+-----+ 中放置多个列?我已经尝试过,但是没有成功。

0 个答案:

没有答案