如何在sql server中的行中显示sql头列

时间:2015-07-31 08:09:07

标签: mysql sql sql-server

我想在特定的月份和年份列显示这些记录,如下表格式

int ith(unsigned long x, int i)
{
    int count=0;
    unsigned long y = x;

    do
    {
        y /= 10;
        count++;
    } while (y > 0);
    if (i > count)
        return -1;
    count -= i;
    while (count > 0)
    {
        x /= 10;
        count--;
    }
    return x % 10;

}

以下是样本日期

count==54353453

1 个答案:

答案 0 :(得分:0)

在Sql Server中,您可以Cross ApplyTabled Valued Constructor一起使用以取消数据

SELECT cs.Source,
       cs.Total
FROM   tbl_leads
       CROSS apply (VALUES ('Organic',Organic),
                           ('Paid',Paid),
                           ('EmailCampaign',EmailCampaign),
                           ('Total',Total)) cs(Source, Total)
WHERE  Month = 'Aug'
       AND Year = '2015' 

或Generic Sql解决方案

SELECT 'Organic' AS Source,
       Organic   AS Total
FROM   tbl_leads
UNION ALL
SELECT 'Paid',
       Paid
FROM   tbl_leads
UNION ALL
SELECT 'EmailCampaign',
       EmailCampaign
FROM   tbl_leads
UNION ALL
SELECT 'Total',
       Total
FROM   tbl_leads