这是我的原始Db的样子:
public bool inStringAll(string needle, string haystack)
{
needle = Regex.Replace(needle.ToLower(), @"\r\n", string.Empty);
haystack = Regex.Replace(haystack.ToLower(), @"\r\n", string.Empty);
Console.WriteLine($"looking for {needle} in {haystack");
string[] needleArr = needle.Split(' ');
string[] haystackArr = haystack.Split(' ');
for(int j = 0; j < needleArr.Length; j++)
Console.WriteLine(needleArr[j]);
int percent = 0;
int increment;
bool decision = false;
if (needleArr.Length > 3)
increment = 100 / 3;
else increment = 100 / needleArr.Length;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < haystackArr.Length; j++)
{
Console.WriteLine("looking for " + needle[i] + " in " + haystack[j]);
if (needleArr[i] == haystackArr[j])
{
percent += increment;
}
}
}
Console.WriteLine($"Percent: {percent}%");
if (percent >= 50)
decision = true;
return decision;
}
这是我用来获得当前结果的代码:
+-------------+----------+---------------------+------+
| StationName | State | Start_time | Time |
+-------------+----------+---------------------+------+
| P3 | Downtime | 2018-02-14 13:57:39 | 83 |
+-------------+----------+---------------------+------+
| P4 | Downtime | 2018-02-14 13:57:39 | 118 |
+-------------+----------+---------------------+------+
| P3 | Downtime | 2018-02-15 06:52:51 | 6 |
+-------------+----------+---------------------+------+
| P4 | Downtime | 2018-02-15 06:52:51 | 11 |
+-------------+----------+---------------------+------+
| P2 | Downtime | 2018-02-15 07:04:16 | 60 |
+-------------+----------+---------------------+------+
| P8 | Downtime | 2018-02-15 07:04:16 | 7 |
+-------------+----------+---------------------+------+
我当前的结果是:
SELECT * FROM(
SELECT CAST([start_time] as DATE) AS [DATE],
State,
Sum(Time) AS Downtime,
STATIONNAME
FROM A6K_Events
Where StationName Like 'P%' AND State='Downtime' AND (Dep != 'Event Log
Error' OR Dep IS NULL)
GROUP BY [start_time],STATIONNAME, State
) AS S
PIVOT (sum(Downtime) FOR STATIONNAME IN([P1],[P2],[P3],[P4],[P5],[P6],[P7],
[P8],[P9],[P10])) AS PT
我尝试了许多不同的展示位置和方法来将其转换为分钟。我最终遇到了一系列错误,而且我不确定自己在做什么错。下面是我将秒转换为小时数分钟的方法。我不确定这是否与我使用的格式不兼容,或者是否将其放在正确的位置或现在。下面的此Convert函数中的列引用也可能不准确。
+------------+----------+------+------+----+-----+------+------+------+------+------+------+
| DATE | State | P1 | P2 | P3 | P4 | P5 | P6 | P7 | P8 | P9 | P10 |
+------------+----------+------+------+----+-----+------+------+------+------+------+------+
| 2018-02-14 | Downtime | NULL | NULL | 83 | 118 | NULL | NULL | NULL | NULL | NULL | NULL |
+------------+----------+------+------+----+-----+------+------+------+------+------+------+
| 2018-02-15 | Downtime | NULL | 60 | 6 | 11 | NULL | NULL | NULL | 7 | NULL | NULL |
+------------+----------+------+------+----+-----+------+------+------+------+------+------+
有人知道实现我的目标的好方法吗,该目标以小时,分钟,秒为单位显示每天每个进程的停机时间?我的“当前结果”粘贴的文本表正是我需要的,而不是秒格式。我将不胜感激,谢谢。
编辑:这被标记为与透视无关的帖子副本。这篇文章是关于在使用数据透视表时进行适当的转换,而不仅仅是一般的转换时间。我的帖子也没有包含毫秒。如果将此内容标记为不重复,将不胜感激,因为这是我在努力理解本帖子被标记为重复的概念的同时所处的另一种情况。
我的预期输出是我的当前结果,但以H:mm:ss
为MR编辑。标签:使用帖子中标记他的副本的方法重做尝试和错误
(CONVERT(varchar(6), sum(time)/3600) + ':' +
RIGHT('0' + CONVERT(varchar(2), (sum(time) % 3600) / 60), 2)+ ':' +
RIGHT('0' + CONVERT(varchar(2), sum(time) % 60), 2)) AS DowntimeMinSec,
SELECT CONVERT(varchar, DATEADD(ms, sum(downtime), 0), 108), * FROM(
SELECT CAST([start_time] as DATE) AS [DATE],
State,
Sum(Time) AS Downtime,
STATIONNAME
FROM A6K_Events
Where StationName Like 'P%' AND State='Downtime' AND (Dep != 'Event Log
Error' OR Dep IS NULL)
GROUP BY [start_time],STATIONNAME, State
) AS S
PIVOT (sum(Downtime) FOR STATIONNAME IN([P1],[P2],[P3],[P4],[P5],[P6],[P7],
[P8],[P9],[P10])) AS PT
GatewayException: Invalid column name 'downtime'.
caused by SQLServerException: Invalid column name 'downtime'.
SELECT * FROM(
SELECT CAST([start_time] as DATE) AS [DATE],
State,
Sum(Time) AS Downtime,
STATIONNAME
FROM A6K_Events
Where StationName Like 'P%' AND State='Downtime' AND (Dep != 'Event Log
Error' OR Dep IS NULL)
GROUP BY [start_time],STATIONNAME, State
) AS S
PIVOT (CONVERT(varchar, DATEADD(ms, sum(downtime), 0), 108)) FOR STATIONNAME
IN([P1],[P2],[P3],[P4],[P5],[P6],[P7],[P8],[P9],[P10])) AS PT
GatewayException: Incorrect syntax near the keyword 'CONVERT'.
caused by SQLServerException: Incorrect syntax near the keyword 'CONVERT'.
答案 0 :(得分:1)
您可以尝试以下查询吗?
SELECT * FROM(
SELECT CAST([start_time] as DATETIME2) AS [DATE],
State,
Sum(Time) AS Downtime,
STATIONNAME
FROM A6K_Events
Where StationName Like 'P%' AND State='Downtime'
AND (Dep != 'Event LogError' OR Dep IS NULL)
GROUP BY [start_time],STATIONNAME, State
) AS S
PIVOT (sum(Downtime) FOR STATIONNAME IN([P1],[P2],[P3],[P4],[P5],[P6],[P7],
[P8],[P9],[P10])) AS PT
输出:
答案 1 :(得分:0)
罗斯·布什(Ross Bush)的评论向我指出了我要去的方向,谢谢大家的答复。在我的外部查询中,我需要引用转换时已旋转的列,而不是引用该列的先前名称。