如何使用SQL Server 2012获取以下输出

时间:2016-08-09 13:28:52

标签: sql-server pivot

我的表格模式是ID,年份,城市,人口

数据如下所示

ID  |Year    |Chennai   |Salem    
--- |------- |------    | ----
1   | 2016   | 1200000  | 120000
2   | 2015   | 1100000  | 200000

我希望输出如下

pre {
    overflow: auto;
    display: block;
    position: relative;
    // word-break: break-all;
    // word-wrap: break-word;
    // white-space: pre-wrap;
    @include font-stack-code;
    // padding: 1rem 1rem 2rem;
    // border: $border;
    font-size: 0.75em;
    //background-color: #fafafa;
    color: #fff;
    background-color: #333;
    .hll {
            background-color: #49483e;
    }
    .c {
            color: #75715e;
    }
    /* Comment */
    .err {
            color: #960050;
            background-color: #1e0010;
    }
    /* Error */
    .k {
            color: #66d9ef;
    }
    /* Keyword */
    .l {
            color: #ae81ff;
    }
    /* Literal */
    .n {
            color: #f8f8f2;
    }
    /* Name */
    .o {
            color: #f92672;
    }
    /* Operator */
    .p {
            color: #f8f8f2;
    }
    /* Punctuation */
    .cm {
            color: #75715e;
    }
    /* Comment.Multiline */
    .cp {
            color: #75715e;
    }
    /* Comment.Preproc */
    .c1 {
            color: #75715e;
    }
    /* Comment.Single */
    .cs {
            color: #75715e;
    }
    /* Comment.Special */
    .ge {
            font-style: italic;
    }
    /* Generic.Emph */
    .gs {
            font-weight: bold;
    }
    /* Generic.Strong */
    .kc {
            color: #66d9ef;
    }
    /* Keyword.Constant */
    .kd {
            color: #66d9ef;
    }
    /* Keyword.Declaration */
    .kn {
            color: #f92672;
    }
    /* Keyword.Namespace */
    .kp {
            color: #66d9ef;
    }
    /* Keyword.Pseudo */
    .kr {
            color: #66d9ef;
    }
    /* Keyword.Reserved */
    .kt {
            color: #66d9ef;
    }
    /* Keyword.Type */
    .ld {
            color: #e6db74;
    }
    /* Literal.Date */
    .m {
            color: #ae81ff;
    }
    /* Literal.Number */
    .s {
            color: #e6db74;
    }
    /* Literal.String */
    .na {
            color: #a6e22e;
    }
    /* Name.Attribute */
    .nb {
            color: #f8f8f2;
    }
    /* Name.Builtin */
    .nc {
            color: #a6e22e;
    }
    /* Name.Class */
    .no {
            color: #66d9ef;
    }
    /* Name.Constant */
    .nd {
            color: #a6e22e;
    }
    /* Name.Decorator */
    .ni {
            color: #f8f8f2;
    }
    /* Name.Entity */
    .ne {
            color: #a6e22e;
    }
    /* Name.Exception */
    .nf {
            color: #a6e22e;
    }
    /* Name.Function */
    .nl {
            color: #f8f8f2;
    }
    /* Name.Label */
    .nn {
            color: #f8f8f2;
    }
    /* Name.Namespace */
    .nx {
            color: #a6e22e;
    }
    /* Name.Other */
    .py {
            color: #f8f8f2;
    }
    /* Name.Property */
    .nt {
            color: #f92672;
    }
    /* Name.Tag */
    .nv {
            color: #f8f8f2;
    }
    /* Name.Variable */
    .ow {
            color: #f92672;
    }
    /* Operator.Word */
    .w {
            color: #f8f8f2;
    }
    /* Text.Whitespace */
    .mf {
            color: #ae81ff;
    }
    /* Literal.Number.Float */
    .mh {
            color: #ae81ff;
    }
    /* Literal.Number.Hex */
    .mi {
            color: #ae81ff;
    }
    /* Literal.Number.Integer */
    .mo {
            color: #ae81ff;
    }
    /* Literal.Number.Oct */
    .sb {
            color: #e6db74;
    }
    /* Literal.String.Backtick */
    .sc {
            color: #e6db74;
    }
    /* Literal.String.Char */
    .sd {
            color: #e6db74;
    }
    /* Literal.String.Doc */
    .s2 {
            color: #e6db74;
    }
    /* Literal.String.Double */
    .se {
            color: #ae81ff;
    }
    /* Literal.String.Escape */
    .sh {
            color: #e6db74;
    }
    /* Literal.String.Heredoc */
    .si {
            color: #e6db74;
    }
    /* Literal.String.Interpol */
    .sx {
            color: #e6db74;
    }
    /* Literal.String.Other */
    .sr {
            color: #e6db74;
    }
    /* Literal.String.Regex */
    .s1 {
            color: #e6db74;
    }
    /* Literal.String.Single */
    .ss {
            color: #e6db74;
    }
    /* Literal.String.Symbol */
    .bp {
            color: #f8f8f2;
    }
    /* Name.Builtin.Pseudo */
    .vc {
            color: #f8f8f2;
    }
    /* Name.Variable.Class */
    .vg {
            color: #f8f8f2;
    }
    /* Name.Variable.Global */
    .vi {
            color: #f8f8f2;
    }
    /* Name.Variable.Instance */
    .il {
            color: #ae81ff;
    }
    /* Literal.Number.Integer.Long */
    .gh {}
    /* Generic Heading & Diff Header */
    .gu {
            color: #75715e;
    }
    /* Generic.Subheading & Diff Unified/Comment? */
    .gd {
            color: #f92672;
    }
    /* Generic.Deleted & Diff Deleted */
    .gi {
            color: #a6e22e;
    }
    /* Generic.Inserted & Diff Inserted */
}

是否可以获得如上所述的输出

1 个答案:

答案 0 :(得分:-1)

如果您只需要查询这两个城镇,可以使用:

SELECT *
FROM 
(
  SELECT ROW_NUMBER() OVER (partition by City Order by City) As [ID], [Year], [City], [Population]
  FROM YourTable
) src
PIVOT
(
  SUM([Population])
  for [City] in (Chennai, Salem)
) piv
ORDER BY ID

如果您需要它是动态的,您可以将其转换为表格值函数,并用字符串参数替换城镇。

如果你需要它是动态的并且总是显示所有城市,那么你可以这样做:

DECLARE @City nvarchar(255), @Cities nvarchar(max), @SQL nvarchar(max)

SET @Cities = ''

DECLARE cursor1 CURSOR FOR
SELECT DISTINCT City
FROM YourTable
OPEN cursor1
FETCH NEXT FROM cursor1 INTO @City

WHILE @@FETCH_STATUS = 0 BEGIN

    SET @Cities += @City + ', '

    FETCH NEXT FROM cursor1 INTO @City
END
CLOSE cursor1
DEALLOCATE cursor1

SET @Cities = SUBSTRING(@Cities, 0, LEN(@Cities))

SET @SQL = 'SELECT *
FROM 
(
  SELECT ROW_NUMBER() OVER (partition by City Order by City) As [ID], [Year], [City], [Population]
  FROM YourTable
) src
PIVOT
(
  SUM([Population])
  for [City] in ('+@Cities+')
) piv
ORDER BY ID'

EXEC(@SQL)

(之前没有使用过Pivot所以我认为解决这个问题很有意思:))