我有這個查詢工作在一個捏,但它需要更好。我正在學習sqlsrv,並且正在努力解決時間問題。在sql server語句中添加時間變量組
基本上我需要拉所有獨特的到來(這是schtm +持續時間),並顯示每小時期望有多少。有人能指引我朝着正確的方向嗎?
此查詢顯示每個獨特的時間像5:30,5:45,6:00,6:15等我需要它按小時分組,因此它會顯示5 - 6 - 7 - 8等在它下面的計數。
<?PHP
$currentdate = date('mdy');
$this_time = date ("D, M j, Y H:i");
$current_date = date('n-d-y');
$this_time = date ("M j, Y H:i");
echo"<table width=\"100%\" border=\"1\"><tr>";
$sql = "SELECT DISTINCT cast(cast(SCHTM as datetime)+cast(DURATION as datetime) as time) as Arrivals FROM admissions.patientinfo WHERE SCHDT ='$currentdate' GROUP BY cast(cast(SCHTM as datetime)+cast(DURATION as datetime) as time)";
$stmt = sqlsrv_query($conn, $sql);
while($row = sqlsrv_fetch_array($stmt , SQLSRV_FETCH_ASSOC)) {
$Arrivals=$row['Arrivals']->format('H:i');
echo"<td align=\"center\">$Arrivals ";
$sql = "SELECT DISTINCT PATNUM as Count FROM admissions.patientinfo WHERE SCHDT ='$currentdate' AND cast(cast(SCHTM as datetime)+cast(DURATION as datetime) as time) = '$Arrivals:00' ";
$query = sqlsrv_query($conn, $sql, array(), array("Scrollable" => 'static'));
$row_count = sqlsrv_num_rows($query);
echo"<br />$row_count</td>";
}
echo"</tr></table>"
?>
SCHTM和DURATION是否不作爲日期時間存儲?這裏需要鑄造什麼? – Simon
他們都是時間(0)字段。鑄造是我能夠實現它的唯一方法。 –