不确定这是否是一个简单的查询,但是由于某种原因无法弄清楚……
我有状态记录的降序记录(从最旧到最新):
#include <iostream>
#include <string>
using namespace std;
class StackNode
{
public:
StackNode * topPtr = NULL;
StackNode* next;
string item;
bool push( string newItem) {
// create a new node
StackNode *newPtr = new StackNode;
// set data portion of new node
newPtr->item = newItem;
// insert the new node
newPtr->next = topPtr;
topPtr = newPtr;
return true;
}
bool pop() {
if (topPtr == NULL)
return false;
// stack is not empty; delete top
else{
StackNode *temp = topPtr;
topPtr = topPtr->next;
// return deleted node to system
temp->next = NULL; // safeguard
delete temp;
return true;
}
}
int ope(string op, string val1,string val2)
{
int vaL1 = stoi(val1);
int vaL2 = stoi(val2);
int res = 0;
if( op == "*")
res = vaL1 * vaL2;
if( op == "/")
res = vaL1 / vaL2;
if( op == "-")
res = vaL1 - vaL2;
if( op == "+")
res = vaL1 + vaL2;
return res;
}
int cal(string pre_exp[],int len)
{
int numb = 0;
for(int i = len -1;i>=0;i--)
{
if ( pre_exp[i] == "*" || pre_exp[i] == "/" || pre_exp[i] == "+" || pre_exp[i] == "-")
{
string op1 = topPtr->item;
pop();
string op2 = topPtr->item;
pop();
numb = numb + ope(pre_exp[i],op1,op2);
}
else
{
push( (pre_exp[i]));
}
}
return numb;
}
int main()
{
StackNode nbr;
string eyoo[] = {"+","-","2","3","9"};
cout<< nbr.cal(eyoo,5)<<endl;
return 0;
}
但是我只想将唯一更改返回到 Name Status Time
A Up 2018-06-21 00:07:00.000000
A Up 2018-05-21 00:07:00.000000
A Down 2018-04-21 00:07:00.000000
A Down 2018-03-21 00:07:00.000000
A Up 2018-02-21 00:07:00.000000
A Down 2018-01-21 00:07:00.000000
列,因此为我提供了状态更改的时间列表。因此答案将是这样:
Status
请注意结果是如何返回3月和5月而不是4月和6月的条目的,因为那是状态从不同状态更改的时间。
我的查询当前正在进行一些联接,所以我也不确定如何/在哪里将其适合查询:
Name Status Time
A Up 2018-05-21 00:07:00.000000
A Down 2018-03-21 00:07:00.000000
A Up 2018-02-21 00:07:00.000000
A Down 2018-01-21 00:07:00.000000
答案 0 :(得分:2)
理想情况下,您需要lag()
并从中进行选择。让我假设您是MySQL的v8之前的版本。
一种方法是相关子查询:
select t.*
from (select t.*,
(select t2.status
from t t2
where t2.time < t.time
order by t2.time desc
limit 1
) as prev_status
from t
) t
where prev_status is null or prev_status <> status;
在较新版本的MySQL中,您只需执行以下操作即可:
select t.*
from (select t.*,
lag(status) over (order by time) as prev_status
from t
) t
where prev_status is null or prev_status <> status;
答案 1 :(得分:0)
我对外部查询通过 public class KeyValueClass
{
public int Age { get; set; }
public string Name { get; set; }
}
private void DoTheJob()
{
var myList = new List<KeyValueClass>
{
new KeyValueClass {Age = 21, Name = "Carl"},
new KeyValueClass {Age = 23, Name = "Vladimir"},
new KeyValueClass {Age = 25, Name = "Bob"},
new KeyValueClass {Age = 21, Name = "Olivia"},
new KeyValueClass {Age = 21, Name = "Carl"},
new KeyValueClass {Age = 30, Name = "Jacob"},
new KeyValueClass {Age = 23, Name = "Vladimir"},
};
var myDistinctList = myList.GroupBy(x => new { x.Age, x.Name })
.Select(c => c.First()).ToList();
}
逐行匹配子查询的结果进行了一些通用的尝试,原因是输出是行未按某种方式排序的方式。可以检查一下是否适合您。
#include <fstream>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int main() {
ifstream file("data.txt");
string str = "";
string::size_type sz;
uint32_t line_number = 4;
while (std::getline(file, str)) {
cout << str << endl;
istringstream buf(str);
istream_iterator<string> beg(buf), end;
vector<string> tokens(beg, end);
for (auto &s : tokens)
cout << atof(s.c_str()) << " " << flush;
cout << endl;
}
}
答案 2 :(得分:0)
另一种方法使用伪临时表(由于MySql不允许对一个子表发出多个子查询,因此不能使用实际的临时表),该表是原始表,并在其中附加了“行号”作为主键: / p>
SET @row_number = 0;
create table temp as
SELECT (@row_number:=@row_number + 1) AS row_num, t.* FROM t order by time;
ALTER TABLE temp ADD PRIMARY KEY (row_num);
然后查询变得简单:
select name, status, time from temp temp1
where temp1.row_num = 1
or temp1.status <>
(select temp2.status from temp temp2 where temp2.row_num = temp1.row_num - 1);
drop table temp; /* no longer need this table */