我是SQL的新手,我想在Microsoft Visual Studio中使用SQL查询填充列,其中我有一个从1到24的序号。但是这些数字填充了2个条件:group by Year,Month。
以蓝色突出显示我想要的输出:
注意:月份以整数表示。例如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 :(得分:1)
这是你想要的吗?
select dense_rank() over (order by year, month)
答案 1 :(得分:0)
假设您希望每行都有唯一编号,您可能需要使用ROW_NUMBER()
函数,如果在PARTITION BY
子句中没有给出OVER
,则会分配唯一编号 - < / p>
SELECT [year] AS [Year]
, [month] AS [Month]
, ROW_NUMBER() OVER (ORDER BY [year], [month]) AS [No.]
FROM [dbo].[upload_STS_data];
注意:根据您的需要,您可以在ORDER BY
中使用 upload_id 或 Users_acct_id 。