Sql server 使用Microsoft SQL Server,基于表数据递增列

Sql server 使用Microsoft SQL Server,基于表数据递增列,sql-server,vb.net,Sql Server,Vb.net,如何为进入ReportID的每个项目增加“项目计数” 如您所见,reportId可以有多个ShipNumber,而ShipNumber可以有多个shipitems ShipNumber和ShipItem是ShipLineTable的外键。项目计数和ReportId是此表的主键ReportLineTable 我需要知道如何在此表中插入“项目计数”列,并使其为报表ID下的每个项目递增。例如,报表ID 1有两个装运编号(1111222)中的3个项目,我需要我的“项目计数”列为报表ID中的每个项目递增。

如何为进入
ReportID
的每个项目增加“项目计数”

如您所见,
reportId
可以有多个
ShipNumber
,而
ShipNumber
可以有多个
shipitems

ShipNumber
ShipItem
ShipLineTable
的外键。项目计数和
ReportId
是此表的主键
ReportLineTable

我需要知道如何在此表中插入“项目计数”列,并使其为报表ID下的每个项目递增。例如,报表ID 1有两个装运编号(1111222)中的3个项目,我需要我的“项目计数”列为报表ID中的每个项目递增。我使用的是Microsoft SQL Server

仅供参考ShipItem保留ShipNumber中的项目计数,并且此表已创建。ReportLineTable从该表中获取信息,我需要增加项目计数

| Item count | Report ID | ShipNumber | ShipItem 
    1            1           1111         1
    2            1           1111         2
    3            1           2222         1
    1            2           3333         1 
    2            2           3333         2
    3            2           3333         3 
    4            2           4444         1
    5            2           4444         2          
    1            3           5555         1
    2            3           6666         2 
    1            4           7777         1
    2            4           8888         1
    3            4           8888         2 
    4            4           9999         1 
    5            4           9999         2
编辑:尝试了此操作,但主键冲突

Dim NextItemCount as integer
dim lastitemcount as integer

Dim MaxItemCount As String = "SELECT MAX(ItemCount) FROM ReportLineTable WHERE ReportID= @ReportID"
        Dim MaxItemCountCommand As New SqlCommand(MaxItemCount, con)
        MaxItemCountCommand .Parameters.Add("@ReportID", SqlDbType.Int).Value = NextItemCount
        If con.State = ConnectionState.Closed Then con.Open()
        Try
            LastItemCount = CInt(MaxItemCountCommand.ExecuteScalar)
        Catch ex As System.Exception
            LastItemCount = 0
        End Try
        con.Close()
        NextItemCount = LastItemCount + 1
我在插入中使用
NextItemCount

   Insert in ReportLineTable (enter every column including ItemCount, values (@Itemcount)",con)
   parameters.add(@ItemCount.SqlDbInt).value = NextItemCount
这是你想要的吗

select t.*,
       row_number() over (partition by reportId order by itemcount) as reportlinetable
from t;
这是你想要的吗

select t.*,
       row_number() over (partition by reportId order by itemcount) as reportlinetable
from t;

我可以毫无问题地执行此SQL代码。我不知道这是否正是你想要的,但我希望这有帮助。代码是SQL的,但是您可以创建一个存储过程o触发器来自动化这个过程

我将此表与您发布的列一起使用

create table ReportLineTable 
(
    itemCount int,
    reportCount int,
    shipNumber int,
    shipItem int
    primary key (itemCount, reportCount)
)

begin 
    Declare @itemNumber int;
    Declare @reportCount int = 100;

    select 
        @itemNumber = MAX(itemCount) + 1 
    from 
        ReportLineTable
    where 
        reportCount = @reportCount;

    if @itemNumber is NULL 
        set @itemNumber = 1;

    insert into ReportLineTable 
    values(@itemNumber, @reportCount, 10000, 1);
end;
-- 10000 and 1 we'll be values from the other table ! Here as example.
祝你好运


Miquel

我可以毫无问题地执行此SQL代码。我不知道这是否正是你想要的,但我希望这有帮助。代码是SQL的,但是您可以创建一个存储过程o触发器来自动化这个过程

我将此表与您发布的列一起使用

create table ReportLineTable 
(
    itemCount int,
    reportCount int,
    shipNumber int,
    shipItem int
    primary key (itemCount, reportCount)
)

begin 
    Declare @itemNumber int;
    Declare @reportCount int = 100;

    select 
        @itemNumber = MAX(itemCount) + 1 
    from 
        ReportLineTable
    where 
        reportCount = @reportCount;

    if @itemNumber is NULL 
        set @itemNumber = 1;

    insert into ReportLineTable 
    values(@itemNumber, @reportCount, 10000, 1);
end;
-- 10000 and 1 we'll be values from the other table ! Here as example.
祝你好运


Miquel

这给了我需要的结果:

"INSERT INTO ReportLineTable 
(ItemCount) 
SELECT 
ROW_NUMBER() OVER(PARTITION BY @ReportID ORDER BY ShipNumber) AS ItemCount 
FROM ShipLineTable"

这给了我所需要的结果:

"INSERT INTO ReportLineTable 
(ItemCount) 
SELECT 
ROW_NUMBER() OVER(PARTITION BY @ReportID ORDER BY ShipNumber) AS ItemCount 
FROM ShipLineTable"

我是否必须循环检查报告id中的每个shipitem以及每个shipitem的增量项计数?这与您的报告有什么不同。请注意,删除某人花时间回答的问题是非常糟糕的。我尝试了某人回答的问题,但无法使其发挥作用,因此我想再次尝试提问,但由于我的上一个问题不再引起注意,因此有了更多细节,我是否必须在报告id中循环检查每个shipitem并增加每个shipitem的项目计数?这是如何做到的和你的不同。请注意,删除某人花时间回答的问题是非常糟糕的。我尝试了某人回答的问题,但无法使其发挥作用,因此我想再次尝试提问,但由于我的上一个问题不再引起注意,因此更详细的问题与我需要的问题非常接近,因此我能够使用它并解决其余问题,谢谢。它离我需要的东西足够近,所以我可以用它来解决剩下的问题,谢谢。