SQL - 如何将列转换为行

时间:2015-05-21 01:16:29

标签: sql sql-server

我有以下SQL脚本:

<div>
<img src="dg8.gif" name="hr1"><img 
src="dg8.gif" name="hr2"><img 
src="dgc.gif"><img 
src="dg8.gif" name="mn1"><img 
src="dg8.gif" name="mn2"><img 
src="dgc.gif"><img 
src="dg8.gif" name="se1"><img 
src="dg8.gif" name="se2"><img 
src="dgam.gif" name="ampm"></div>

<script type="text/javascript">
// (c) 2000-2014 ricocheting.com
dg = new Array();
dg[0]=new Image();dg[0].src="dg0.gif";
dg[1]=new Image();dg[1].src="dg1.gif";
dg[2]=new Image();dg[2].src="dg2.gif";
dg[3]=new Image();dg[3].src="dg3.gif";
dg[4]=new Image();dg[4].src="dg4.gif";
dg[5]=new Image();dg[5].src="dg5.gif";
dg[6]=new Image();dg[6].src="dg6.gif";
dg[7]=new Image();dg[7].src="dg7.gif";
dg[8]=new Image();dg[8].src="dg8.gif";
dg[9]=new Image();dg[9].src="dg9.gif";
dgam=new Image();dgam.src="dgam.gif";
dgpm=new Image();dgpm.src="dgpm.gif";

function dotime(){ 
    var d=new Date();
    var hr=d.getHours(),mn=d.getMinutes(),se=d.getSeconds();

    // set AM or PM
    document.ampm.src=((hr<12)?dgam.src:dgpm.src);

    // adjust from 24hr clock
    if(hr==0){hr=12;}
    else if(hr>12){hr-=12;}

    document.hr1.src = getSrc(hr,10);
    document.hr2.src = getSrc(hr,1);
    document.mn1.src = getSrc(mn,10);
    document.mn2.src = getSrc(mn,1);
    document.se1.src = getSrc(se,10);
    document.se2.src = getSrc(se,1);
}

function getSrc(digit,index){
    return dg[(Math.floor(digit/index)%10)].src;
}

window.onload=function(){
    dotime();
    setInterval(dotime,1000);
}

</script>

此脚本生成此表:

enter image description here

但我希望得到一个这样的清单:

enter image description here

有任何线索吗?

3 个答案:

答案 0 :(得分:0)

试试这个:

WeekDay   Date Month                          Year
--------- ---- ------------------------------ -----------
Sunday         May                            2015
Monday         May                            2015
Tuesday        May                            2015
Wednesday      May                            2015
Thursday       May                            2015
Friday    1    May                            2015
Saturday  2    May                            2015
Sunday    3    May                            2015
Monday    4    May                            2015
Tuesday   5    May                            2015
Wednesday 6    May                            2015
Thursday  7    May                            2015
Friday    8    May                            2015
Saturday  9    May                            2015
Sunday    10   May                            2015
Monday    11   May                            2015
Tuesday   12   May                            2015
Wednesday 13   May                            2015
Thursday  14   May                            2015
Friday    15   May                            2015
Saturday  16   May                            2015
Sunday    17   May                            2015
Monday    18   May                            2015
Tuesday   19   May                            2015
Wednesday 20   May                            2015
Thursday  21   May                            2015
Friday    22   May                            2015
Saturday  23   May                            2015
Sunday    24   May                            2015
Monday    25   May                            2015
Tuesday   26   May                            2015
Wednesday 27   May                            2015
Thursday  28   May                            2015
Friday    29   May                            2015
Saturday  30   May                            2015
Sunday    31   May                            2015
Monday         May                            2015
Tuesday        May                            2015
Wednesday      May                            2015
Thursday       May                            2015
Friday         May                            2015
Saturday       May                            2015

根据需要产生:

with open("pantry.txt","r") as f:
    for line in f:
        (val, key) = line.split(" ")
        pantry[key] = int(val)

答案 1 :(得分:0)

这是一个SQL小提琴,它以您想要的格式生成任意日期序列。我将UNION的所有NULL结果作为练习留给读者。

http://sqlfiddle.com/#!3/9eecb7/275

答案 2 :(得分:0)

这是另一个版本:

DECLARE @d DATE = '20150501';

WITH 
m AS(SELECT 1 AS d UNION ALL SELECT d+1 FROM m WHERE d < datediff(d, @d, dateadd(m, 1, @d))),
dt AS(SELECT YEAR(@d) y, DATENAME(m, @d) m, d, DATENAME(dw, DATEADD(dd, d-1, @d)) wd FROM m ),
wk AS (SELECT * FROM (VALUES (1, 'Monday'), (2, 'Tuesday'),
                     (3, 'Wednesday'), (4, 'Thursday'), (5, 'Friday'),
                     (6, 'Saturday'), (7, 'Sunday')) w(d, n))

SELECT 1 o, y, m, d, wd FROM dt
UNION ALL
SELECT 0, YEAR(@d), DATENAME(m, @d), NULL, w.n FROM wk w
WHERE d < (SELECT d FROM wk WHERE n = (SELECT TOP 1 wd FROM dt ORDER BY d))
UNION ALL
SELECT 2, YEAR(@d), DATENAME(m, @d), NULL, w.n FROM wk w
WHERE d > (SELECT d FROM wk WHERE n = (SELECT TOP 1 wd FROM dt ORDER BY d DESC))
ORDER BY o , d

输出:

o   y       m   d       wd
0   2015    May NULL    Monday
0   2015    May NULL    Tuesday
0   2015    May NULL    Wednesday
0   2015    May NULL    Thursday
1   2015    May 1       Friday
1   2015    May 2       Saturday
1   2015    May 3       Sunday
1   2015    May 4       Monday
1   2015    May 5       Tuesday
1   2015    May 6       Wednesday
1   2015    May 7       Thursday
1   2015    May 8       Friday
1   2015    May 9       Saturday
1   2015    May 10      Sunday
1   2015    May 11      Monday
1   2015    May 12      Tuesday
1   2015    May 13      Wednesday
1   2015    May 14      Thursday
1   2015    May 15      Friday
1   2015    May 16      Saturday
1   2015    May 17      Sunday
1   2015    May 18      Monday
1   2015    May 19      Tuesday
1   2015    May 20      Wednesday
1   2015    May 21      Thursday
1   2015    May 22      Friday
1   2015    May 23      Saturday
1   2015    May 24      Sunday
1   2015    May 25      Monday
1   2015    May 26      Tuesday
1   2015    May 27      Wednesday
1   2015    May 28      Thursday
1   2015    May 29      Friday
1   2015    May 30      Saturday
1   2015    May 31      Sunday