sql compact 3.5,从每个组中选择前n行

时间:2012-06-05 21:35:31

标签: sql sql-server-ce

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

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

/ *  *将fH.InvoiceNumber添加到我的查询中以获得下面的结果。   /     select tH。,t.CustomerNumber,c.CustomerName,fH.Status,fH.InvoiceNumber     来自tenderHeader tH     加入任务t ON tH.TaskActivityID = t.ActivityID     join finalizeTicketHeader fH ON tH.FinalizeTicketTaskActivityID = fH.TaskActivityID     加入客户c ON t.CustomerNumber = c.CustomerNumber     其中fH.Status<> '3'和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不支持带有子查询的标量值。

1 个答案:

答案 0 :(得分:2)

以下示例说明了从每个组中选择前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 ordered

 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