所以我有这样的逻辑,其中查询中的@1
来自i
for loop
for (var i = 1; i <= 12; i++) {
var getPR = "SELECT top 1 kg, rep, date FROM Test WHERE exerVariName = @0 AND rep = @1 order by kg desc";
foreach (var c in query stuff) {
// Display both results and 0 if no result
}
}
因此,数据库的设置方式是输入千克值(kg)以及一些集合和代表以及练习名称和日期。该查询将查看每个运动名称,并查看每个代表在1-12之间的最高千克的位置,因此对于第一运动,检查代表1的最高千克,然后检查2,依此类推,然后进行下一次运动,并从1-但是,如果例如某个练习不在数据库中且设置了5次重复,那么总是没有匹配的结果,所以它会跳转到6并再次检查。
我想要的是,如果数据库中没有匹配项,请在c.kg
中将0
设置为foreach
,以便仍然存在值,因为我想要设置一个包含12行的html表,我想要所有这些内容,可以这样做吗?
使用razor(cshtml),而不是MVC和SQL Server compact edition。
我的代码:
<table class="prTable">
<tr>
<th>Exercise</th>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
<th>5</th>
<th>6</th>
<th>7</th>
<th>8</th>
<th>9</th>
<th>10</th>
<th>11</th>
<th>12</th>
</tr>
<!-- Loops out all the different exercise names for deadlifts -->
@foreach (var d in db.Query(getVariD))
{
<tr class="prTableRow">
<td class="prExerVariNameTD">@d.exerVariName</td>
@* Gives me a result from each rep range, var i goes from 1 up to choosen rep, and puts
that into the sql string to get each rep range. *@
@for (var i = 1; i <= 12; i++)
{
//Selects the row with the highest weight from each exercise AND from each rep range
//up to choosen value in the for loop.
var getPR = "SELECT top 1 kg, rep, date FROM Test WHERE exerVariName = @0 AND rep = @1 order by kg desc";
//Loops out the database values.
foreach (var c in db.Query(getPR, d.exerVariName, i))
{
DateTime Date = c.date;
var finalDate = "- " + Date.ToString("MMM dd, yyyy");
var weight = c.kg + "kg";
var rep = "x " + c.rep;
<td class="prTableCell" title="@finalDate">@weight</td>
}
}
</tr>
}
</table>
答案 0 :(得分:2)
使用row_number()
你可以为每个代表获得更大的公斤数。我认为您的查询应该是:
SELECT kg, rep, date
FROM (
SELECT kg, rep, date,
ROW_NUMBER() OVER (PARTITION BY rep
ORDER BY kg DESC) as rn
FROM Test
WHERE exerVariName = @0
) as T
WHERE T.rn = 1
现在,如果你想为每个练习得到12个结果,你需要提供一个基表
SELECT base.rep,
COALESCE (res.kg, 0) as kg,
COALESCE (res.date, '') as date
FROM (VALUES (1), (2), (3), (4), (5), (6),
(7), (8), (9), (10), (11), (12)
) as base(rep)
LEFT JOIN (
SELECT kg, rep, date
FROM (
SELECT kg, rep, date,
ROW_NUMBER() OVER (PARTITION BY rep
ORDER BY kg DESC) as rn
FROM Test
WHERE exerVariName = @0
) as T
WHERE T.rn = 1
) res
ON base.rep = res.rep
ORDER BY base.rep
测试分开的每个查询,以便了解带来的内容。我认为你可以处理所有12个结果的最终显示。
SQL CE编辑
SELECT base.rep,
COALESCE (res.kg, 0) as kg,
COALESCE (res.date, '') as date
FROM (VALUES (1), (2), (3), (4), (5), (6),
(7), (8), (9), (10), (11), (12)
) as base(rep)
LEFT JOIN (
SELECT T1.rep, T1.kg, T1.date
FROM Test T1
JOIN ( SELECT exerVariName, rep, max(kg) as kg
FROM Test
GROUP BY exerVariName, rep
) as T
ON T1.rep = T.rep
AND T1.kg = T.kg
AND T1.exerVariName = T.exerVariName
WHERE exerVariName = @0
-- This is in case you can have duplicate kg for same rep.
-- select the latest date
AND NOT EXIST ( SELECT 1 FROM
FROM Test T2
WHERE T2.exerVariName = T1.exerVariName
AND T2.rep = T1.rep
AND T2.kg = T1.kg
AND T2.date > T1.date )
) res
ON base.rep = res.rep
ORDER BY base.rep