是否可以在几周内从数据库中选择数据? 我使用cshtml(不是MVC)和webmatrix,如果这有任何区别。
var dbQueryAllVariants = "SELECT * FROM Test WHERE exercise = " + exercise + " AND date >= '" + fromDate + "' AND date <= '" + toDate + "'";
所以现在我正在使用这个,我输入了一个开始日期(例如2016-11-01)和结束日期(例如2016-11-30)(yyyy-mm-dd cuz north europe)。这会在这些日期之间显示数据库中的所有数据,但由于数据库中的所有行只有一天作为日期,因此要查看的结果将采用日期形式,我想如果它可能需要数周,在这种情况下,从头到尾11月作为一个例子将是aprox 4周,这可能吗?此外,数据库中的数据是int值,因此我希望能够添加这些数据,以显示在有意义的情况下显示的总周数。
例如。
column 1 column 2 column 3
5 . . . . 6 . . 2016-11-13
8 . . . . 10 . . 2016-11-15
6 . . . . 3 . . 2016-11-17
截至目前,它将显示3天,第1天为11,第2天为18,第3天为9,但以星期显示为11 + 18 + 9 = 38,举个例子。这可能甚至不可能开始,但我想知道如果可能的话如何做到这一点!
如果这不是一个可能的解决方案,有没有办法喜欢选择日期形式的所有数据,将其放入数组或其他任何数据,并从那里发送它根据周数分组为每周总数年(例如,11月包含44-48周)这样的事情?我想说的是,如果最终结果是我想要的,那么它的完成方式并不重要。
@{
//Calls for my website layout.
Layout = "~/_SiteLayout.cshtml";
//Browser title of the specific page.
Page.Title = "TEST";
//Opens database.
var db = Database.Open("SmallBakery");
//Variables.
var exercise = Request.Form["Exercise"];
var fromDate = Request.Form["fromDate"];
var toDate = Request.Form["toDate"];
var exerVariName = "";
var exerVariNameS = "";
var exerVariNameB = "";
var exerVariNameD = "";
//Defaults to show data between these
//dates if user dont choose any dates.
var noStartDate = "1970-01/01";
var noEndDate = "2099-12/31";
//If user does not choose eiter/any start/end date
//this will end up showing all results possible.
if (fromDate == "") {
fromDate = noStartDate;
}
if (toDate == "") {
toDate = noEndDate;
}
//Takes exerVariName from different dropdowns
//depending on which exercise is selected due to
//the fact that only one dropdown is visible at any time.
if (exercise == "1") {
exerVariName = Request.Form["exerVariNameS"];
} else if (exercise == "2") {
exerVariName = Request.Form["exerVariNameB"];
} else {
exerVariName = Request.Form["exerVariNameD"];
}
//Gets exercise variants to the dropdown menu.
var getSVariName = "SELECT * FROM exerciseVariants WHERE exerVariNameID = 1 ORDER BY exerVariName";
var getBVariName = "SELECT * FROM exerciseVariants WHERE exerVariNameID = 2 ORDER BY exerVariName";
var getDVariName = "SELECT * FROM exerciseVariants WHERE exerVariNameID = 3 ORDER BY exerVariName";
var getData = "SELECT * FROM Test";
//Gets the date.
var getDate = "SELECT date FROM Test";
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
<!-- Form for posting. -->
<form method="post" action="">
<!-- Radio buttons to select which data to show. -->
<div>
<label>Squat</label>
<input type="radio" name="Exercise" id="hej1" value="1" />
</div>
<div>
<label>Benchpress</label>
<input type="radio" name="Exercise" id="hej2" value="2" />
</div>
<div>
<label>Deadlift</label>
<input type="radio" name="Exercise" id="hej3" value="3" />
</div>
<div>
<!-- Dropdown menu with squat-variant-names. -->
<select id="exerVariNameS" name="exerVariNameS">
<option value="all">All</option>
<option value="Comp">Competition</option>
@foreach (var get in db.Query(getSVariName)) {
//Gets the exercise variation names from
//the database and puts them in a dropdown.
<option value="@get.exerVariName">@get.exerVariName</option>
}
</select>
<!-- Dropdown menu with bench-variant-names. -->
<select id="exerVariNameB" name="exerVariNameB">
<option value="all">All</option>
<option value="Comp">Competition</option>
@foreach (var get in db.Query(getBVariName)) {
//Gets the exercise variation names from
//the database and puts them in a dropdown.
<option value="@get.exerVariName">@get.exerVariName</option>
}
</select>
<!-- Dropdown menu with deadlift-variant-names. -->
<select id="exerVariNameD" name="exerVariNameD">
<option value="all">All</option>
<option value="Comp">Competition</option>
@foreach (var get in db.Query(getDVariName)) {
//Gets the exercise variation names from
//the database and puts them in a dropdown.
<option value="@get.exerVariName">@get.exerVariName</option>
}
</select>
</div>
<div>
<!-- Date calendar. -->
<input placeholder="From date..." type="text" class="datepicker" name="fromDate" value="@fromDate">
</div>
<div>
<!-- Date calendar. -->
<input placeholder="To date..." type="text" class="datepicker" name="toDate" value="@toDate">
</div>
<!-- The submit button. -->
<input type="submit" value="Show" class="submit" />
</form>
<!-- Displays database value on submit click based on choosen radiobutton from form-post above. -->
@if (IsPost) {
//When I select ALL in the dropdown it runs
//this line because there is no filter for 'exerVariName'.
// var dbQueryAllVariants = "SELECT * FROM Test WHERE exercise = " + exercise + " AND date >= '" + fromDate + "' AND date <= '" + toDate + "'";
//When I select a specific exercise variation.
var dbQuerySingleVariant = "SELECT * FROM Test WHERE exercise = " + exercise + " AND exerVariName = '" + exerVariName + "' AND date >= '" + fromDate + "' AND date <= '" + toDate + "'";
//This is what the problem is....
var dbQueryAllVariants = "SELECT DATEPART(week, date) AS weekNumber, sum(kg)+sum(sett) AS grandTotalPerWeek FROM Test WHERE Exercise = " + exercise + " AND DATEPART(week, date) BETWEEN DATEPART(week, " + fromDate + ") AND DATEPART(week, " + toDate + ") GROUP BY DATEPART(week, date)";
var dbQuery = "";
//If dropdown = select all, it does, else, it show the one I pick.
if (exerVariName == "all") {
dbQuery = dbQueryAllVariants;
} else {
dbQuery = dbQuerySingleVariant;
}
//Foreach to write out all the data from db.
var sumTotalWeight = 0;
foreach (var get in db.Query(dbQuery)) {
<a>Weight: </a>
<a>@get.Kg kg</a>
<a> Sets: </a>
<a>@get.Sett</a>
<a> Reps: </a>
<a>@get.Rep</a>
<a> Total reps: </a>@(get.Sett * get.Rep)
<a> @get.date</a>
var totalWeight = @get.Kg * @get.Sett * @get.Rep;
sumTotalWeight += totalWeight;
<a> @totalWeight</a>
<br>
}
@sumTotalWeight
}
</body>
答案 0 :(得分:0)
从您的评论中可以得出结论,您正在使用MSSQL Server Compact Edition。
如果这是正确的,那么您可以使用DATEPART
功能提取每个日期的一年中的一周,然后按周分组并将每个日期的所有结果相加。
这样的事情:
SELECT
sum(column1)+sum(column2) AS grandTotalPerWeek
FROM Test
WHERE Exercise = {Exercise}
AND DATEPART(week, date) = {weekNumber}
{Exercise}和{weekNumber}是要替换的变量。
或者像这样,如果您需要一次请求多个星期:
SELECT
DATEPART(week, date) AS weekNumber,
sum(column1)+sum(column2) AS grandTotalPerWeek
FROM Test
WHERE Exercise = {Exercise}
AND DATEPART(week, date) IN ({listOfWeekNumbers})
GROUP BY DATEPART(week, date)
{Exercise}和{listOfWeekNumbers}是要替换的变量。
在任何一种情况下,当我们谈论周数时,我们都谈论整数值。 1表示一年中的第一周,第二周表示2,...
示例:获取一月的值
SELECT
DATEPART(week, date) AS weekNumber,
sum(kg)+sum(sett) AS grandTotalPerWeek
FROM Test
WHERE Exercise = 1
AND DATEPART(week, date) IN (1,2,3,4)
GROUP BY DATEPART(week, date)
因此,要使用此SQL,您需要将日期转换为周数。如果您只有dateFrom
和dateTo
,那么您可以尝试这样的事情:
SELECT
DATEPART(week, date) AS weekNumber,
sum(kg)+sum(sett) AS grandTotalPerWeek
FROM Test
WHERE Exercise = 1
AND DATEPART(week, date) BETWEEN DATEPART(week, {dateFrom}) AND DATEPART(week, {dateTo})
GROUP BY DATEPART(week, date)
MSDN上的