选择年份概览,如列

时间:2015-05-05 11:45:45

标签: sql sql-server calendar

如何创建选择查询以选择每列的每个月和每年列,因此结果应如下所示: http://www.google.de/url?source=imglanding&ct=img&q=http://www.calenweb.com/png/en/2015/2015-yearly-calendar.png&sa=X&ei=Ma1IVeHLLtHjavfsgfAI&ved=0CAkQ8wc&usg=AFQjCNHw0gwp3G9-bSGGNspsZJjMqZbjBA

当然,格式,颜色......并不重要。

有什么想法吗?问题是否清楚?

4 个答案:

答案 0 :(得分:1)

<?php
$monthNames = Array("January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December");
?>

<?php
if (!isset($_REQUEST["month"])) $_REQUEST["month"] = date("n");
if (!isset($_REQUEST["year"])) $_REQUEST["year"] = date("Y");
?>

<?php
$cMonth = $_REQUEST["month"];
$cYear = $_REQUEST["year"];

$prev_year = $cYear;
$next_year = $cYear;
$prev_month = $cMonth-1;
$next_month = $cMonth+1;

if ($prev_month == 0 ) {
    $prev_month = 12;
    $prev_year = $cYear - 1;
}
if ($next_month == 13 ) {
    $next_month = 1;
    $next_year = $cYear + 1;
}
?>

<table width="200">
<tr align="center">
<td bgcolor="#999999" style="color:#FFFFFF">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="50%" align="left">  <a href="<?php echo $_SERVER["PHP_SELF"] . "?month=". $prev_month . "&year=" . $prev_year; ?>" style="color:#FFFFFF">Previous</a></td>
<td width="50%" align="right"><a href="<?php echo $_SERVER["PHP_SELF"] . "?month=". $next_month . "&year=" . $next_year; ?>" style="color:#FFFFFF">Next</a>  </td>
</tr>
</table>
</td>
</tr>
<tr>
<td align="center">
<table width="100%" border="0" cellpadding="2" cellspacing="2">
<tr align="center">
<td colspan="7" bgcolor="#999999" style="color:#FFFFFF"><strong><?php echo $monthNames[$cMonth-1].' '.$cYear; ?></strong></td>
</tr>
<tr>
<td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>S</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>M</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>T</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>W</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>T</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>F</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>S</strong></td>
</tr>

<?php
$timestamp = mktime(0,0,0,$cMonth,1,$cYear);
$maxday = date("t",$timestamp);
$thismonth = getdate ($timestamp);
$startday = $thismonth['wday'];
for ($i=0; $i<($maxday+$startday); $i++) {
    if(($i % 7) == 0 ) echo "<tr>n";
    if($i < $startday) echo "<td></td>n";
    else echo "<td align='center' valign='middle' height='20px'>". ($i - $startday + 1) . "</td>n";
    if(($i % 7) == 6 ) echo "</tr>n";
}
?>
</table>
</td>
</tr>
</table>

答案 1 :(得分:1)

这是你可以做到的;

select 
    A,
    B,
    C
from
    (select 1 as A) tblA
    inner join (select 2 as B) tblB on 1=1
    inner join (select 3 as C) tblC on 1=1

想象一下,每个(select 1 as X) tblX都是对表格的查询,每个表格都会返回where month=[1...12]个月。无论你得到什么结果,你都会得到更多的列,就像你输入更多的内连接(或左外连接......)。

其中12个,你整整一年......

答案 2 :(得分:1)

我找到了

    SELECT
CASE WHEN format(DATEADD(Day,Number,'01.01.2015'),'MM') = '01' then format(DATEADD(Day,Number,'01.01.2015'),'dd.MM.yyyy') else null end  as Januar,
CASE WHEN format(DATEADD(Day,Number,'01.02.2015'),'MM') = '02' then format(DATEADD(Day,Number,'01.02.2015'),'dd.MM.yyyy') else null end  as Februar,
CASE WHEN format(DATEADD(Day,Number,'01.03.2015'),'MM') = '03' then format(DATEADD(Day,Number,'01.03.2015'),'dd.MM.yyyy') else null end  as März,
CASE WHEN format(DATEADD(Day,Number,'01.04.2015'),'MM') = '04' then format(DATEADD(Day,Number,'01.04.2015'),'dd.MM.yyyy') else null end  as April,
CASE WHEN format(DATEADD(Day,Number,'01.05.2015'),'MM') = '05' then format(DATEADD(Day,Number,'01.05.2015'),'dd.MM.yyyy') else null end  as Mai,
CASE WHEN format(DATEADD(Day,Number,'01.06.2015'),'MM') = '06' then format(DATEADD(Day,Number,'01.06.2015'),'dd.MM.yyyy') else null end  as Juni,
CASE WHEN format(DATEADD(Day,Number,'01.07.2015'),'MM') = '07' then format(DATEADD(Day,Number,'01.07.2015'),'dd.MM.yyyy') else null end  as Juli,
CASE WHEN format(DATEADD(Day,Number,'01.08.2015'),'MM') = '08' then format(DATEADD(Day,Number,'01.08.2015'),'dd.MM.yyyy') else null end  as August,
CASE WHEN format(DATEADD(Day,Number,'01.09.2015'),'MM') = '09' then format(DATEADD(Day,Number,'01.09.2015'),'dd.MM.yyyy') else null end  as September,
CASE WHEN format(DATEADD(Day,Number,'01.10.2015'),'MM') = '10' then format(DATEADD(Day,Number,'01.10.2015'),'dd.MM.yyyy') else null end  as Oktober,
CASE WHEN format(DATEADD(Day,Number,'01.11.2015'),'MM') = '11' then format(DATEADD(Day,Number,'01.11.2015'),'dd.MM.yyyy') else null end  as November,
CASE WHEN format(DATEADD(Day,Number,'01.12.2015'),'MM') = '12' then format(DATEADD(Day,Number,'01.12.2015'),'dd.MM.yyyy') else null end  as Dezember
FROM  master..spt_values 
WHERE Type='P'
AND DATEADD(day,Number,'01.01.2015') <= DATEADD(dd, -DAY(DATEADD(mm, 1, '01.01.2015')), DATEADD(mm, 1, '01.01.2015'))  

做到了!

答案 3 :(得分:0)

您可以使用PIVOT来排列数据和计算表来计算它:

DECLARE @year CHAR(4) = 2015

;WITH N1 (N) AS 
(SELECT 1 FROM (VALUES (1), (1), (1), (1), (1), (1), (1), (1)) n (N)),
N2 (N) AS 
(SELECT 
   ROW_NUMBER() OVER(ORDER BY N1.N)-1 FROM N1 AS N1 
 CROSS JOIN N1 AS N2 CROSS JOIN N1 N3),
CTE as
(
  SELECT 
    month(dateadd(d, n, @year)) mon, 
    day(dateadd(d, n, @year)) monthday,
    convert(char(2), dateadd(d, n, @year), 5)+ ' ' 
      + left(datename(weekday, dateadd(d, n, @year)), 1) day
  FROM n2
  WHERE 
    @year < dateadd(year, 1, @year) - n
)
SELECT 
  [1] JAN, [2] FEB, [3] MAR, [4] APR, [5] MAY, [6] JUN, 
  [7] JUL, [8] AUG, [9] NOV, [10] OCT, [11] NOV, [12] [DEC]
FROM CTE
PIVOT
(min([day])  
FOR mon in([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])  
)AS p 
ORDER BY 1

Fiddle