2017-05-22 17 views
0

ReportIDに入るすべてのアイテムに対して「アイテムカウント」の増分を作成するにはどうすればよいですか?Microsoft SQL Serverを使用し、テーブルデータに基づいて列を増やす

ご覧のとおり、reportIdには、複数のShipNumbersと、ShipNumberがあります。shipitemsです。

ShipNumberおよびShipItemは、ShipLineTableの外部キーです。アイテム数とReportIdがこのテーブルの主キーですReportLineTable

このテーブルには、ReportLineTableのアイテム数の列を挿入する方法と、レポートIDの下にあるすべてのアイテムに対して増分を設定する方法が必要です。たとえば、レポートID 1には2つの船舶番号(1111、2222)からの3つのアイテムがあります。レポートIDの各アイテムに対してアイテムカウント列を増やす必要があります。私はMicrosoft SQL Serverを使用しています。

FYI 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 

編集:これを試してみましたが、私は、私は私の挿入

Insert in ReportLineTable (enter every column including ItemCount, values (@Itemcount)",con) 
    parameters.add(@ItemCount.SqlDbInt).value = NextItemCount 
+0

を私はレポートIDと増分項目内のすべてのshipitemをループしていない各カウントshipitem? – NC25

+0

これはあなたの[最後の質問](https://stackoverflow.com/q/44119203/1070452)とどのように違うのですか?誰かが答えに時間を割いた質問を削除することは非常に悪いことに注意してください。 – Plutonix

+0

誰かが答えたものを試してみましたので、私はもう一度尋ねようと思っていました。しかし、私の最後の質問が注意を奪われて以来、より詳細に質問しました。 – NC25

答えて

0

これは私が必要との結果が得られた:

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

NextItemCountを使用

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 

主キー違反を取得することはあなたが望むものを、このですか?

+0

それは私がそれを使うことができた残りの部分を見つけて、ありがとう。 – NC25

0

このSQLコードは問題なく実行できます。これがまさにあなたが望むものなのかどうかわかりませんが、これが助けてくれることを願っていますコードはSQLにありますが、ストアドプロシージャを作成してプロセスを自動化することができます。

私はあなたが公開した列でこの表を使用しました。

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. 

最高の幸運

ミケル

関連する問題