このビューテーブルはSQL Serverにあり、別の列に各レコードの一意のキーにしたいと考えています。ビューのクエリ操作で使用するユニークなキー
個別の列に各レコードの固有のキーを作成する方法はありますか?例
のインデックスのようにこれは、コードビュー
SELECT 'order' AS type, id AS id, id_customer, amount AS debit, 0 AS credit, order_date AS date, '....' AS description
FROM dbo.Orders
UNION ALL
SELECT 'receipt' AS type, id AS id, id_customer, 0 AS debit, amount AS credit, receipt_date AS date, 'cash' AS description
FROM dbo.receipts
私はこのコード を使用してこのプレゼンテーションを求めていますが作成されますが、私はそれぞれを繰り返して、請求書や領収書と同様のID番号がある問題を抱えています私は一意のキーは、次のコードでの比較をしたいので、二度、それらの
declare @id_customer int
;with initial as(
select *
from result
where id_customer= @id_customer
),report as(
select r.id,[balance]=isnull((select sum(b.debit-b.credit)
from initial b
where b.[date]<r.[date]) + r.debit - r.credit ,r.debit-r.credit)
from initial r
)
select [Operation type] = type,
reference_no = r.id,
[description],
[Debit] = debit,
[Credit] = credit,
[Balance] = b.balance
from result r
inner join report b on b.id = r.id
where r.id_customer = @id_customer
order by r.[date]
の比較仕事をするクエリを追加質問。 –
行番号を尋ねていますか?それ以外の場合は、データを理解せずにキーを作成する方法を教えられません。 – MgSam
ユニークなキーを作りましょうか?各行に固有の値を取得しようとしていますか? NEWID()を使用できます。 –