1

我正在编写一个查询以从每个组中选择前 1 条记录。请记住,我正在使用 sql compact 3.5,因此无法使用 rank 函数。我很确定我的查询不正确,但我不确定如何从每个组中选择顶部。有人有任何想法吗?

这是我试图开始工作的查询

/* * 将 fH.InvoiceNumber 添加到我的查询中以进一步获取下面的结果。 / 选择 tH。, t.CustomerNumber, c.CustomerName, fH.Status, fH.InvoiceNumber fromtenderHeader tH join task t ON tH.TaskActivityID = t.ActivityID join finalizeTicketHeader fH ON tH.FinalizeTicketTaskActivityID = fH.TaskActivityID join customer c ON t.CustomerNumber = c .CustomerNumber 其中 fH.Status <> '3' AND t.TripID = '08ea6982-6efd-46fa-9753-0fd8b076f24c';

这是我的表的样子:

customer table:

|------------------------------------------------|
| CustomerNumber | CustomerName | Address1 | ... |
|------------------------------------------------|
| 0012084737     | Customer A   | 150 Rd A | ... |
|------------------------------------------------|
| 0012301891     | Customer B   | 152 Rd A | ... |
|------------------------------------------------|

task table

|-----------------------------------------------------------------|
| ActivityID | TripID    | TaskTypeName | Status | CustomerNumber | 
|-----------------------------------------------------------------|
| 4967f6cc   | 08ea6982  | Payment      | 2      | 0012084737     |
|-----------------------------------------------------------------|
| e96469a1   | 08ea6982  | Payment      | 2      | 0012301891     |
|-----------------------------------------------------------------|

finalizeTicketHeader table

|---------------------------------------------------|
| TaskActivityID | InvoiceNumber | Amount  | Status |
|---------------------------------------------------|
| 916082c8       | 1000          | 563.32  | 3      |
|---------------------------------------------------|
| 916082c8       | 1001          | -343.68 | 0      |
|---------------------------------------------------|
| 4b38bf60       | 1002          | 152.29  | 0      |
|---------------------------------------------------|
| 4b38bf60       | 1003          | -35.80  | 0      |
|---------------------------------------------------|

tenderHeader table

|-------------------------------------------------------------------------------------|
| TaskActivityID | InvoiceNumber | PastDue | TodaysDue | FinalizeTicketTaskActivityID |
|-------------------------------------------------------------------------------------|
| 4967f6cc       | 1234567891    | 23.55   | 219.64    | 916082c8                     |
|-------------------------------------------------------------------------------------|
| e96469a1       | 1234567893    | 0.00    | 116.49    | 4b38bf60                     |
|-------------------------------------------------------------------------------------|

我遇到的问题是重复。

像这样:

|------------------------------------------------------------------------------------------------------------------------------------|
| TaskActivityID | InvoiceNumber | PastDue | TodaysDue | FinalizeTicketTaskActivityID | CustomerNumber | CustomerName | InvoiceNumber |
|------------------------------------------------------------------------------------------------------------------------------------|
| 4967f6cc       | 1234567891    | 23.55   | 219.64    | 916082c8                     | 0012084737     | Customer A   | 1001         |
|------------------------------------------------------------------------------------------------------------------------------------|
| e96469a1       | 1234567893    | 0.00    | 116.49    | 4b38bf60                     | 0012301891     | Customer B   | 1002         |              
|------------------------------------------------------------------------------------------------------------------------------------|
| e96469a1       | 1234567893    | 0.00    | 116.49    | 4b38bf60                     | 0012301891     | Customer B   | 1003         |
|------------------------------------------------------------------------------------------------------------------------------------|

我已经像这样重写了查询,但是我需要从子查询中获取特定的列。

select tH.* from tenderHeader th
inner join task t on tH.TaskActivityID = t.ActivityID
inner join (
    select k.TaskActivityID from finalizeTicketHeader k group by k.TaskActivityID
) as fH on tH.FinalizeTicketTaskActivityID = fH.TaskActivityID
inner join customer c on t.CustomerNumber = c.CustomerNumber

我需要从 fH 获取状态。关于如何做到这一点的任何想法?

select tH.*, fH.Status from tenderHeader th
inner join task t on tH.TaskActivityID = t.ActivityID
inner join finalizeTicketHeader fH on tH.FinalizeTicketTaskActivityID = tH.TaskActivityID
inner join customer c on t.CustomerNumber = c.CustomerNumber
where tH.FinalizeTicketTaskActivityID = (
    select top (1) k.TaskActivityID from finalizeTicketHeader k 
);

但似乎 sql compact 3.5 在 where 原因中不支持带有子查询的标量值。

4

1 回答 1

1

这是一个示例,展示了从每个组中选择前 1 名的方法

id|time
--------
2 | 1:10
2 | 0:45 
2 | 1:45
2 | 1:30
1 | 1:00
1 | 1:10

该表称为table_1;我们按 id 分组并假设时间应该是 desc 排序的

 select table_1.* from table_1
 inner join (
     select id, max(time) as max_time from table_1
     group by id
 ) as t
 on t.max_time = table_1.time and table_1.id = t.id
 order by table_1.id

我们得到的结果是

id|time
--------
1 | 1:10
2 | 1:45
于 2012-06-05T23:37:52.777 回答