SQL Server 2000 行编号

发布于 2024-11-01 18:32:20 字数 1828 浏览 3 评论 0原文

我正在尝试获得连续的行编号,但无论我尝试什么都不起作用。这是我的查询

select
    l.seq, l.mn_no as mn_no, l.sb_no as sb_no,
    l.dp_no as dp_no,
    sum(costprice) as amt
from 
    dbo.mac_pur_tempdetail d
inner join 
    dbo.mac_pur_tempheader h on d.header_id = h.header_id
                              and h.ref = 'SAH1FIHC'
inner join 
    dbo.mac_actlocmap l on l.loc_main = d.loc_id
                         and l.description = 'PUR'
group by  
    l.seq, l.mn_no, l.sb_no, l.dp_no

查询的结果

1   4110        30          0000        17.5000
4   4110        20          0000        3.6000
6   4110        40          0000        6.0000
7   4110        10          0000        1.8000
14  4110        25          0000        3.6000
15  4110        50          0000        1.8000

这是我尝试的

select
    (select count(seq)  
     from dbo.mac_actlocmap s
     where s.seq <= a.seq and a.mn_no = s.mn_no) as new_seq,
    * 
from 
    (select
         l.seq, l.mn_no as mn_no,
         l.sb_no as sb_no, l.dp_no as dp_no,
         sum(costprice) as amt
     from 
         dbo.mac_pur_tempdetail d
     inner join 
         dbo.mac_pur_tempheader h on d.header_id = h.header_id
                                  and h.ref = 'SAH1FIHC'
     inner join 
         dbo.mac_actlocmap l on l.loc_main = d.loc_id
                              and l.description = 'PUR'
     group by  
         l.seq, l.mn_no, l.sb_no, l.dp_no) a

但结果是

1   1   4110        30          0000        17.5000
2   4   4110        20          0000        3.6000
3   6   4110        40          0000        6.0000
4   7   4110        10          0000        1.8000
7   14  4110        25          0000        3.6000
8   15  4110        50          0000        1.8000

I am trying to obtain a sequential row numbering, but whatever I am trying is not working. Here is my query

select
    l.seq, l.mn_no as mn_no, l.sb_no as sb_no,
    l.dp_no as dp_no,
    sum(costprice) as amt
from 
    dbo.mac_pur_tempdetail d
inner join 
    dbo.mac_pur_tempheader h on d.header_id = h.header_id
                              and h.ref = 'SAH1FIHC'
inner join 
    dbo.mac_actlocmap l on l.loc_main = d.loc_id
                         and l.description = 'PUR'
group by  
    l.seq, l.mn_no, l.sb_no, l.dp_no

Here is the result of that query

1   4110        30          0000        17.5000
4   4110        20          0000        3.6000
6   4110        40          0000        6.0000
7   4110        10          0000        1.8000
14  4110        25          0000        3.6000
15  4110        50          0000        1.8000

I tried

select
    (select count(seq)  
     from dbo.mac_actlocmap s
     where s.seq <= a.seq and a.mn_no = s.mn_no) as new_seq,
    * 
from 
    (select
         l.seq, l.mn_no as mn_no,
         l.sb_no as sb_no, l.dp_no as dp_no,
         sum(costprice) as amt
     from 
         dbo.mac_pur_tempdetail d
     inner join 
         dbo.mac_pur_tempheader h on d.header_id = h.header_id
                                  and h.ref = 'SAH1FIHC'
     inner join 
         dbo.mac_actlocmap l on l.loc_main = d.loc_id
                              and l.description = 'PUR'
     group by  
         l.seq, l.mn_no, l.sb_no, l.dp_no) a

But the result is

1   1   4110        30          0000        17.5000
2   4   4110        20          0000        3.6000
3   6   4110        40          0000        6.0000
4   7   4110        10          0000        1.8000
7   14  4110        25          0000        3.6000
8   15  4110        50          0000        1.8000

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

零度℉ 2024-11-08 18:32:20

您的计数是对 dbo.mac_actlocmap 中未聚合未过滤行进行计数。但是您正在与带有聚合和过滤器的子查询进行比较。

由于这种复杂性,请使用临时表。对于在 SQL Server 2000 上模拟 ROW_NUMBER 所需的三角连接,这比使用两次完全相同的查询更容易

select
l.seq,
l.mn_no as mn_no,
l.sb_no as sb_no,
l.dp_no as dp_no,
sum(costprice) as amt

INTO #foo

from dbo.mac_pur_tempdetail d
inner join dbo.mac_pur_tempheader h
on d.header_id = h.header_id
and h.ref = 'SAH1FIHC'
inner join dbo.mac_actlocmap l
on l.loc_main = d.loc_id
and l.description = 'PUR'
group by  l.seq,l.mn_no,l.sb_no,l.dp_no

select
(select count(seq) from #foo s
where s.seq <= a.seq and a.mn_no = s.mn_no) as new_seq,
* from #foo a

Your count is counting unaggregated and unfiltered rows in dbo.mac_actlocmap. But you are comparing to a subquery with an aggregate and filter.

With this complexity, use a temp table. It's just easier than having exactly the same query twice for the triangular join needed to emulate ROW_NUMBER on SQL Server 2000

select
l.seq,
l.mn_no as mn_no,
l.sb_no as sb_no,
l.dp_no as dp_no,
sum(costprice) as amt

INTO #foo

from dbo.mac_pur_tempdetail d
inner join dbo.mac_pur_tempheader h
on d.header_id = h.header_id
and h.ref = 'SAH1FIHC'
inner join dbo.mac_actlocmap l
on l.loc_main = d.loc_id
and l.description = 'PUR'
group by  l.seq,l.mn_no,l.sb_no,l.dp_no

select
(select count(seq) from #foo s
where s.seq <= a.seq and a.mn_no = s.mn_no) as new_seq,
* from #foo a
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文