在这种情况下,如何 select nvarchar 数据类型的不同值并给它们排名?
How can I select Distinct Values of nvarchar datatype and give them a Rank in this scenario?
我是 运行 SQL Server 2012
,我的数据库中有一个名为 DateDimension
的 table。
我的 DateDimension
Table 是一个包含 2 列的 table:第 date
列包含从“2014-01-01
”到“[=20=”的每日日期 运行 ]' 和列 WeekGroup
是 nvarchar(100)
数据类型。
摘录如下:
date WeekGroup
2014-01-01 Wk 30 Dec 2013-05 Jan 2014
2014-01-02 Wk 30 Dec 2013-05 Jan 2014
2014-01-03 Wk 30 Dec 2013-05 Jan 2014
2014-01-04 Wk 30 Dec 2013-05 Jan 2014
2014-01-05 Wk 30 Dec 2013-05 Jan 2014
2014-01-06 Wk 06 Jan 2014-12 Jan 2014
...
我现在有以下查询:
SELECT
[date],
[WeekGroup],
RANK() OVER (ORDER BY [date] ASC) AS 'Rank'
FROM DateDimension
WHERE date BETWEEN '2014-01-01' AND '2019-12-31'
ORDER BY [date] ASC
我的输出如下:
date WeekGroup Rank
2014-01-01 Wk 30 Dec 2013-05 Jan 2014 1
2014-01-02 Wk 30 Dec 2013-05 Jan 2014 2
2014-01-03 Wk 30 Dec 2013-05 Jan 2014 3
2014-01-04 Wk 30 Dec 2013-05 Jan 2014 4
2014-01-05 Wk 30 Dec 2013-05 Jan 2014 5
2014-01-06 Wk 06 Jan 2014-12 Jan 2014 6
...
我想微调上述查询,以便获得以下输出,其中 distinct
WeekGroup
和相应的 Rank
:
WeekGroup Rank
Wk 30 Dec 2013-05 Jan 2014 1
Wk 06 Jan 2014-12 Jan 2014 2
...
如何在 nvarchar
变量 (WeekGroup
) 上执行此操作?
将 date
排除在外并按周分组汇总:
SELECT WeekGroup,
RANK() OVER (ORDER BY MIN([date]) ASC) AS [Rank]
FROM DateDimension
WHERE date BETWEEN '2014-01-01' AND '2019-12-31'
GROUP BY WeekGroup
ORDER BY MIN([date]) ASC;
这使用了 CTE 和 RANK():
declare @DateDimension table ([date] date, WeekGroup nvarchar(100))
insert into @DateDimension values
('2014-01-01', 'Wk 30 Dec 2013-05 Jan 2014')
, ('2014-01-02', 'Wk 30 Dec 2013-05 Jan 2014')
, ('2014-01-03', 'Wk 30 Dec 2013-05 Jan 2014')
, ('2014-01-04', 'Wk 30 Dec 2013-05 Jan 2014')
, ('2014-01-05', 'Wk 30 Dec 2013-05 Jan 2014')
, ('2014-01-06', 'Wk 06 Jan 2014-12 Jan 2014')
;with cte as (
SELECT [WeekGroup], count(*) as count
FROM @DateDimension
WHERE date BETWEEN '2014-01-01' AND '2019-12-31'
group by [WeekGroup])
select [WeekGroup], rank() over (order by [count] desc) as [Rank] from cte
试试这个。
declare @DatA table ([date] date, WeekGroup nvarchar(100))
insert into @DatA values
('2014-01-01', 'Wk 30 Dec 2013-05 Jan 2014')
, ('2014-01-02', 'Wk 30 Dec 2013-05 Jan 2014')
, ('2014-01-03', 'Wk 30 Dec 2013-05 Jan 2014')
, ('2014-01-04', 'Wk 30 Dec 2013-05 Jan 2014')
, ('2014-01-05', 'Wk 30 Dec 2013-05 Jan 2014')
, ('2014-01-06', 'Wk 06 Jan 2014-12 Jan 2014')
SELECT WEEKGROUP, RANK() OVER (ORDER BY A.WeekGroup desc) RNK
FROM (
SELECT DISTINCT WEEKGROUP FROM @DatA
WHERE date BETWEEN '2014-01-01' AND '2019-12-31'
)A
我是 运行 SQL Server 2012
,我的数据库中有一个名为 DateDimension
的 table。
我的 DateDimension
Table 是一个包含 2 列的 table:第 date
列包含从“2014-01-01
”到“[=20=”的每日日期 运行 ]' 和列 WeekGroup
是 nvarchar(100)
数据类型。
摘录如下:
date WeekGroup
2014-01-01 Wk 30 Dec 2013-05 Jan 2014
2014-01-02 Wk 30 Dec 2013-05 Jan 2014
2014-01-03 Wk 30 Dec 2013-05 Jan 2014
2014-01-04 Wk 30 Dec 2013-05 Jan 2014
2014-01-05 Wk 30 Dec 2013-05 Jan 2014
2014-01-06 Wk 06 Jan 2014-12 Jan 2014
...
我现在有以下查询:
SELECT
[date],
[WeekGroup],
RANK() OVER (ORDER BY [date] ASC) AS 'Rank'
FROM DateDimension
WHERE date BETWEEN '2014-01-01' AND '2019-12-31'
ORDER BY [date] ASC
我的输出如下:
date WeekGroup Rank
2014-01-01 Wk 30 Dec 2013-05 Jan 2014 1
2014-01-02 Wk 30 Dec 2013-05 Jan 2014 2
2014-01-03 Wk 30 Dec 2013-05 Jan 2014 3
2014-01-04 Wk 30 Dec 2013-05 Jan 2014 4
2014-01-05 Wk 30 Dec 2013-05 Jan 2014 5
2014-01-06 Wk 06 Jan 2014-12 Jan 2014 6
...
我想微调上述查询,以便获得以下输出,其中 distinct
WeekGroup
和相应的 Rank
:
WeekGroup Rank
Wk 30 Dec 2013-05 Jan 2014 1
Wk 06 Jan 2014-12 Jan 2014 2
...
如何在 nvarchar
变量 (WeekGroup
) 上执行此操作?
将 date
排除在外并按周分组汇总:
SELECT WeekGroup,
RANK() OVER (ORDER BY MIN([date]) ASC) AS [Rank]
FROM DateDimension
WHERE date BETWEEN '2014-01-01' AND '2019-12-31'
GROUP BY WeekGroup
ORDER BY MIN([date]) ASC;
这使用了 CTE 和 RANK():
declare @DateDimension table ([date] date, WeekGroup nvarchar(100))
insert into @DateDimension values
('2014-01-01', 'Wk 30 Dec 2013-05 Jan 2014')
, ('2014-01-02', 'Wk 30 Dec 2013-05 Jan 2014')
, ('2014-01-03', 'Wk 30 Dec 2013-05 Jan 2014')
, ('2014-01-04', 'Wk 30 Dec 2013-05 Jan 2014')
, ('2014-01-05', 'Wk 30 Dec 2013-05 Jan 2014')
, ('2014-01-06', 'Wk 06 Jan 2014-12 Jan 2014')
;with cte as (
SELECT [WeekGroup], count(*) as count
FROM @DateDimension
WHERE date BETWEEN '2014-01-01' AND '2019-12-31'
group by [WeekGroup])
select [WeekGroup], rank() over (order by [count] desc) as [Rank] from cte
试试这个。
declare @DatA table ([date] date, WeekGroup nvarchar(100))
insert into @DatA values
('2014-01-01', 'Wk 30 Dec 2013-05 Jan 2014')
, ('2014-01-02', 'Wk 30 Dec 2013-05 Jan 2014')
, ('2014-01-03', 'Wk 30 Dec 2013-05 Jan 2014')
, ('2014-01-04', 'Wk 30 Dec 2013-05 Jan 2014')
, ('2014-01-05', 'Wk 30 Dec 2013-05 Jan 2014')
, ('2014-01-06', 'Wk 06 Jan 2014-12 Jan 2014')
SELECT WEEKGROUP, RANK() OVER (ORDER BY A.WeekGroup desc) RNK
FROM (
SELECT DISTINCT WEEKGROUP FROM @DatA
WHERE date BETWEEN '2014-01-01' AND '2019-12-31'
)A