我有一个包含少量列[ShipAmt]
,[Cdate]
,....
我希望按特定月份的每周汇总列并显示它。
本周被认为是从1月底到月底。
如果一周内没有ProductOrders
,我想将其总和显示为0.00
示例
Week 1 ---> 2016/03/01 - 2016/03/07
Week 2 ---> 2016/03/08 - 2016/03/14
Week 3 ---> 2016/03/15 - 2016/03/21
Week 4 ---> 2016/03/22 - 2016/03/28
Week 5 ---> 2016/03/29 - 2016/03/31
表格
create table ProductOrders
(
[ID] [int] IDENTITY(1,1) NOT NULL,
[ShipAmt] float not null,
[Total] float not null,
[Cdate] datetime not null
)
值
insert into ProductOrders ([ShipAmt], [Total], [Cdate])
values (26.99 , 40.00, '2016-03-04 20:26:54.000');
insert into ProductOrders ([ShipAmt], [Total], [Cdate])
values (45.00 , 70.88, '2016-03-04 20:28:41.000');
insert into ProductOrders ([ShipAmt], [Total], [Cdate])
values (75.00 ,125.00, '2016-03-05 05:39:03.000');
insert into ProductOrders ([ShipAmt], [Total], [Cdate])
values (49.00 , 71.99, '2016-02-26 11:21:02.000');
现在我要总结3rd
个月的所有列,我尝试了这个。
Select
Sum([ShipAmt]) [ShipAmt],
Sum([Total]) [Total]
from
(
SELECT
[ShipAmt],
[Total],
[Cdate],
DATEDIFF(week, '2016-03-01', '2016-03-31') AS WeekNumber
FROM ProductOrders
)
as t1
GROUP BY
WeekNumber
输出
ShipAmt Total
195.99 307.87
所需的输出
ShipAmt Total
146.99 235.88
0.00 0.00
0.00 0.00
0.00 0.00
答案 0 :(得分:1)
您的查询应该像
<?php
session_start();
include("config.php");
?>
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Register Form</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Place favicon.ico and apple-touch-icon.png in the root directory -->
<link rel="stylesheet" href="css/main.css">
<link href='http://fonts.googleapis.com/css?family=Roboto:400,300,500' rel='stylesheet' type='text/css'>
<link href="//netdna.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet">
<script src="js/jquery-1.8.2.min.js"></script>
<script src="js/jquery.validate.min.js"></script>
<script src="js/main.js"></script>
</head>
<body>
<form method="POST" action="register.php">
<td>Name</td><td> <input type="text" name="name"></td>
</tr>
<tr>
<td>Email</td><td> <input type="text" name="email"></td>
</tr>
<tr>
<td>Phone </td><td><input type="text" name="phone"></td>
</tr>
<tr>
<td>Password</td><td> <input type="password" name="password"></td>
</tr>
<tr>
<td><input id="button" type="submit" name="submit" value="Sign-Up"></td>
</tr>
</form>
</table>
</fieldset>
</div>
<?php
if("submit"){
function NewUser()
{
$email = $_POST['email'];
$password = $_POST['password'];
$name = $_POST['name'];
$phone = $_POST['phone'];
$query = "INSERT INTO php_users_login (email,password,name,phone) VALUES ('$email','$password','$name','$phone')";
$data = mysql_query ($query)or die(mysql_error());
if($data)
{
echo "YOUR REGISTRATION IS COMPLETED...";
}
}
function SignUp()
{
if(!empty($_POST['email'])) //checking the 'user' name which is from Sign-Up.html, is it empty or have some text
{
$query = mysql_query("SELECT * FROM php_users_login WHERE `email` = '$_POST[email]' AND `password` = '$_POST[password]'") or die(mysql_error());
if(!$row = mysql_fetch_array($query) or die(mysql_error()))
{
newuser();
}
else
{
echo "SORRY...YOU ARE ALREADY REGISTERED USER...";
}
}
}
if(isset($_POST['submit']))
{
SignUp();
}
}
else{
echo "didn't work.";
}
?>
<强>输出:强>
答案 1 :(得分:1)
试试这个
`
答案 2 :(得分:0)
Plz尝试这样的事情
DECLARE @V_DATE DATE = '01-01-2016'
,@V_MDATE DATE
SET @V_MDATE = DATEADD(DAY,-1,(DATEADD(MONTH,1,@V_DATE)))
;WITH CTE_PRD
AS
( SELECT 1 AS WNO,@V_DATE AS SDATE,DATEADD(DAY,6,@V_DATE) AS EDATE
UNION ALL SELECT WNO+1,DATEADD(DAY,1,EDATE),DATEADD(DAY,7,EDATE) FROM CTE_PRD WHERE EDATE <= @V_MDATE
)
SELECT WNO ,ISNULL(SUM([ShipAmt]),0) [ShipAmt],ISNULL(SUM([Total]),0) [Total]
FROM CTE_PRD
INNER JOIN ProductOrders ON [Cdate] BETWEEN SDATE AND (CASE WHEN EDATE < @V_MDATE THEN EDATE ELSE @V_MDATE END)
GROUP BY WNO