我有一张表<index>
<letter>
<title>A</title>
<mainTerm>
<title>Some description</title>
<code>Q87.1</code>
</mainTerm>
<mainTerm>
<title>Another description<nemod>(-some) (detail)</nemod></title>
<code>F44.4</code>
</mainTerm>
<mainTerm>
<title>A more detailed term</title>
<seeAlso>similar terms</seeAlso>
<term level="1">
<title>acute</title>
<code>R10.0</code>
</term>
<term level="1">
<title>angina</title>
<code>K55.1</code>
</term>
</mainTerm>
</letter>
.....
</index>
,其中包含这样的数据
$(document).find("letter").each(function(){
// Define letters variable
$letters = $(this).find('> title').text();
//Find descendants of each letter
$codes = ($(this).find('code').text());
$desc = ($(this)).find('mainTerm title').text();
//Build table
$("#content").append(
'<table border="1"><tr><th>Code</th><th>Description</th></tr><tr><td> '+$codes+' </td><td> '+$desc+' </td></tr></table>'
);
(end brackets for the above)
我想用查询
创建一个这样的表delay
我搜索了给定的问题。但是有些答案与表中类似值的计数有关,但没有一个对数据进行分类。
请帮忙
答案 0 :(得分:0)
<强> SQL DEMO 强>
with ranges as (
SELECT 'On time' r UNION ALL
SELECT 'Late < 1 min' UNION ALL
SELECT 'Late by 1-2 min' UNION ALL
SELECT 'Late by 2-3 min' UNION ALL
SELECT 'Late by 3-4 min' UNION ALL
SELECT 'Late by 4-5 min' UNION ALL
SELECT 'Late by 5-6 min' UNION ALL
SELECT 'Late by 6-7 min' UNION ALL
SELECT 'Late by 7-8 min' UNION ALL
SELECT 'Late >8 minutes'
), setup as (
SELECT CASE WHEN [Delay] = 0 THEN 'On time'
WHEN [Delay] < 1 THEN 'Late < 1 min'
WHEN [Delay] < 2 THEN 'Late by 1-2 min'
WHEN [Delay] < 3 THEN 'Late by 2-3 min'
WHEN [Delay] < 4 THEN 'Late by 3-4 min'
WHEN [Delay] < 5 THEN 'Late by 4-5 min'
WHEN [Delay] < 6 THEN 'Late by 5-6 min'
WHEN [Delay] < 7 THEN 'Late by 6-7 min'
WHEN [Delay] < 8 THEN 'Late by 7-8 min'
ELSE 'Late >8 minutes'
END as [Range],
[Delay]
FROM Table1
)
SELECT t.r as Range, COALESCE(SUM(s.[Delay]),0) as Value, COUNT(s.[Delay]) as Freq
FROM ranges t
LEFT JOIN setup s
ON t.r = s.[Range]
GROUP BY t.r
<强>输出强>
答案 1 :(得分:0)
使用CASE
或IIF
可以解决此问题,但我建议使用函数使代码更具可读性和可扩展性。
用户功能 - ufRange
CREATE FUNCTION dbo.ufRange (@Delay INT)
RETURNS INT
AS
BEGIN
DECLARE @Ret INT
IF @Delay <= 0 SET @Ret = 0 -- not late/ early
ELSE IF @Delay > 320 SET @Ret = 9 -- more than 8 minutes late
ELSE SET @Ret = @Delay / 60 -- late <=1 to <=8 minutes
RETURN @Ret
END
然后将range
的详细信息放在支持表LateRange
中,就像在excel中一样,如果您熟悉vlookup
用于此
支持表 - LateRange
idrange Range Values
0 On time 0
1 Late < 1 min 60
2 Late by 1-2 min 120
3 Late by 2-3 min 180
4 Late by 3-4 min 240
5 Late by 4-5 min 300
6 Late by 5-6 min 360
7 Late by 6-7 min 420
8 Late by 7-8 min 480
9 Late >8 minutes
行。最后假设delay
已保存的表名为LateList
,查询为
SELECT Range, Values,
(SELECT COUNT(*) FROM LateList WHERE dbo.ufRange([delay]) = idrange) Frequency
FROM LateRange
或者,如果您愿意,可以使用INNER JOIN
和子查询进行修改。