2017-01-08 114 views
1

我是SQL新手,我想在Microsoft Visual Studio中使用SQL查詢填充列,其中我有一個從1到24的連續編號。但是,這些編號填充2個條件:按年份,月份分組。用連續編號填充SQL列

突出顯示爲藍色是我想要的輸出:

注:本月整數表示。例如1 = 1月。

目前查詢:

CREATE TABLE [dbo].[upload_STS_data] (
[result_count] INT   NOT NULL, 
[username]  VARCHAR (50) NOT NULL, 
[year]   INT   NOT NULL, 
[month]   INT   NOT NULL, 
[Users_acct_id] INT   NOT NULL, 
[upload_id]  INT   IDENTITY (1, 1) NOT NULL, 
PRIMARY KEY CLUSTERED ([upload_id] ASC), 
CONSTRAINT [fk_upload_STS_data_Users1] FOREIGN KEY ([Users_acct_id]) REFERENCES [dbo].[Users] ([acct_id]) 
);  


GO 
CREATE NONCLUSTERED INDEX [fk_upload_STS_data_Users1_idx] 
    ON [dbo].[upload_STS_data]([Users_acct_id] ASC); 

我怎樣才能做到這一點?

+0

完成:) @GordonLinoff – Vannessa

回答

1

這是你想要的嗎?

select dense_rank() over (order by year, month) 
0

假設你想爲每個行的唯一編號,您可能需要使用ROW_NUMBER()功能,會在沒有PARTITION BYOVER子句中給定的分配唯一的號碼 -

SELECT [year] AS [Year] 
     , [month] AS [Month] 
     , ROW_NUMBER() OVER (ORDER BY [year], [month]) AS [No.] 
FROM [dbo].[upload_STS_data]; 

注意:根據您的需要,您可以使用upload_idUsers_acct_idORDER BY中。