我有一个像这样的表mysql:
static void Main(string[] args)
{
bool exit = false;
List<float> grades = new List<float>();
do
{
Console.WriteLine("1. Enter Grades");
Console.WriteLine("2. Get Average");
Console.WriteLine("3. My program");
Console.WriteLine("4. exit");
Console.WriteLine("");
string input = Console.ReadLine();
Console.WriteLine("");
if (input == "1")
{
int totalGrades = 0;
//User Input
Console.WriteLine("How many grades do you want to enter? ");
while (true)
{
try
{
totalGrades = Convert.ToInt32(Console.ReadLine());
break;
}
catch (FormatException)
{
Console.WriteLine("This is not a valid number");
continue;
}
}
Console.WriteLine("");
while (totalGrades > 0)
{
while (true)
{
try
{
grades.Add(Convert.ToInt32(Console.ReadLine()));
totalGrades--;
break;
}
catch (FormatException)
{
Console.WriteLine("This is not a valid number");
continue;
}
}
}
Console.WriteLine("");
}
else if (input == "2")
{
double average = grades.Average();
if (average >= 90)
{
Console.WriteLine($"The average is a {average} which is an A.");
}
else if (average >= 80)
{
Console.WriteLine($"The average is a {average} which is an B.");
}
else if (average >= 70)
{
Console.WriteLine($"The average is a {average} which is an C.");
}
else if (average >= 60)
{
Console.WriteLine($"The average is a {average} which is an D.");
}
else
{
Console.WriteLine($"The average is a {average} which is an E.");
}
Console.WriteLine("");
}
else if (input == "4")
{
exit = true;
} else
{
Console.WriteLine("This is not an option");
}
}
while (exit == false);
}
对于每个工厂,我试图从最新的“ date_op”中选择具有最大“线”的行。所以从这个:
CREATE TABLE prelevement (
line int,
facture varchar(30),
date_op varchar(30),
code_op varchar(30)
);
insert into prelevement
(line,facture,date_op,code_op)
values
(1,'F1','2019-02-20','PREL'),
(2,'F1','2019-02-20','CART'),
(3,'F1','2019-02-20','REJE'),
(8,'F1','2019-02-19','PREL'),
(2,'F2','2019-02-15','PREL'),
(1,'F2','2017-01-25','PREL'),
(1,'F3','2018-02-25','REJ'),
(2,'F3','2018-02-25','CART');
我正在尝试获得以下结果:
| line | facture | date_op | code_op |
| ---- | ------- | ---------- | ------- |
| 1 | F1 | 2019-02-20 | PREL |
| 2 | F1 | 2019-02-20 | CART |
| 3 | F1 | 2019-02-20 | REJE |
| 8 | F1 | 2019-02-19 | PREL |
| 2 | F2 | 2019-02-15 | PREL |
| 1 | F2 | 2017-01-25 | PREL |
| 1 | F3 | 2018-02-25 | REJ |
| 2 | F3 | 2018-02-25 | CART |
我来过这里,但是我不确定从这里去哪里:
| line | facture | date_op | code_op |
| ---- | ------- | ---------- | ------- |
| 3 | F1 | 2019-02-20 | REJE |
| 2 | F2 | 2019-02-15 | PREL |
| 2 | F3 | 2018-02-25 | CART |
答案 0 :(得分:1)
在MySQL中,您可以使用:
select p.*
from prelevement p
where (date_op, line) = (select date_op, line
from prelevement p2
where p2.facture = p.facture
order by date_op desc, line desc
limit 1
);
您还可以通过两个相关的子查询来执行此操作:
select p.*
from prelevement p
where p.date_op = (select max(p2.date_op)
from prelevement p2
where p2.facture = p.facture
) and
p.line = (select max(p2.line)
from prelevement p2
where p2.facture = p.facture and p2.date_op = p.date_op
) ;
在MySQL 8.0中,您可以使用窗口函数:
select p.*
from (select p.*, row_number() over (partition by facture order by date_op desc, line desc) as seqnum
from prelevement p
) p
where seqnum = 1;
Here是所有三种解决方案的db <>小提琴。
答案 1 :(得分:0)
这将起作用:
CREATE TABLE prelevement ( line int, facture varchar(30), date_op varchar(30), code_op varchar(30) );
✓
insert into prelevement (line,facture,date_op,code_op) values (1,'F1','2019-02-20','PREL'), (2,'F1','2019-02-20','CART'), (3,'F1','2019-02-20','REJE'), (8,'F1','2019-02-19','PREL'), (2,'F2','2019-02-15','PREL'), (1,'F2','2017-01-25','PREL'), (1,'F3','2018-02-25','REJ'), (2,'F3','2018-02-25','CART');
✓
select * from prelevement pp where facture in ( select facture from ( select * from prelevement p where facture in ( select facture from prelevement pin group by facture having max(pin.date_op) = p.date_op ) ) t group by facture having max(t.line) = pp.line )
line | facture | date_op | code_op ---: | :------ | :--------- | :------ 3 | F1 | 2019-02-20 | REJE 2 | F2 | 2019-02-15 | PREL 2 | F3 | 2018-02-25 | CART
db <>提琴here