Select 年概览喜欢专栏

Select year overview like columns

如何创建一个 select 查询 select 每个月和每个列的年列,所以结果应该是这样的: 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

当然,格式、颜色……都不重要。

有什么想法吗?问题清楚了吗?

<?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>

这就是你可以做到的;

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 都是对 table 返回月份的查询,每个月份都有 where month=[1...12]。无论你得到什么结果,你得到的列数与你输入的内部连接数(或外部左连接数......)一样多。

其中 12 个,你涵盖了全年...

我找到了

    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'))  

做到了!

您可以使用 PIVOT 来排列数据并使用计数 table 来计算它:

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