Sql 使用“选择顶部”生成一个参数列表,其中
我有一张这样的桌子:Sql 使用“选择顶部”生成一个参数列表,其中,sql,azure-sql-database,Sql,Azure Sql Database,我有一张这样的桌子: | date | example_variable | | 2013-2-22 | cat | | 2013-3-22 | dog | | 2013-1-22 | ewe | | 2013-8-22 | pig | | date | rank | example_variable | | 2013-2-22 | 1
| date | example_variable |
| 2013-2-22 | cat |
| 2013-3-22 | dog |
| 2013-1-22 | ewe |
| 2013-8-22 | pig |
| date | rank | example_variable |
| 2013-2-22 | 1 | cat1 |
| 2013-2-22 | 2 | cat2 |
| 2013-3-22 | 1 | dog1 |
| 2013-3-22 | 2 | dog2 |
| 2013-1-22 | 1 | ewe1 |
| 2013-1-22 | 2 | ewe2 |
| 2013-8-22 | 1 | pig1 |
| 2013-8-22 | 2 | pig2 |
SELECT
a.rank, b.rank, c.rank,
a.example_variable as V1,
b.example_variable as V2,
c.example_variable as V3,
a.AsOfDate as 1stDate,
b.AsOfDate as 2ndDate,
c.AsOfDate as 3rdDate
FROM <table> a, <table> b, <table> c
WHERE a.rank = b.rank,
And b.rank = c.rank,
AND a.AsOfDate = '2013-1-22'
AND b.AsOfDate = '2013-2-22'
AND c.AsOfDate = '2013-3-22'
我已经知道怎么做了:
SELECT
a.example_variable as V1,
b.example_variable as V2,
c.example_variable as V3,
a.AsOfDate as 1stDate,
b.AsOfDate as 2ndDate,
c.AsOfDate as 3rdDate
FROM <table> a, <table> b, <table> c
WHERE a.AsOfDate = '2013-1-22'
AND b.AsOfDate = '2013-2-22'
AND c.AsOfDate = '2013-3-22'
在上面的查询中,我手动输入了最近的三个日期。我想自动找到那些。看起来我可以用陀螺,但我不知道怎么用
更复杂的版本:
我简化了上面的例子。在我的现实世界中,有多行具有相同的日期,其组织方式如下:
| date | example_variable |
| 2013-2-22 | cat |
| 2013-3-22 | dog |
| 2013-1-22 | ewe |
| 2013-8-22 | pig |
| date | rank | example_variable |
| 2013-2-22 | 1 | cat1 |
| 2013-2-22 | 2 | cat2 |
| 2013-3-22 | 1 | dog1 |
| 2013-3-22 | 2 | dog2 |
| 2013-1-22 | 1 | ewe1 |
| 2013-1-22 | 2 | ewe2 |
| 2013-8-22 | 1 | pig1 |
| 2013-8-22 | 2 | pig2 |
SELECT
a.rank, b.rank, c.rank,
a.example_variable as V1,
b.example_variable as V2,
c.example_variable as V3,
a.AsOfDate as 1stDate,
b.AsOfDate as 2ndDate,
c.AsOfDate as 3rdDate
FROM <table> a, <table> b, <table> c
WHERE a.rank = b.rank,
And b.rank = c.rank,
AND a.AsOfDate = '2013-1-22'
AND b.AsOfDate = '2013-2-22'
AND c.AsOfDate = '2013-3-22'
查询如下:
| date | example_variable |
| 2013-2-22 | cat |
| 2013-3-22 | dog |
| 2013-1-22 | ewe |
| 2013-8-22 | pig |
| date | rank | example_variable |
| 2013-2-22 | 1 | cat1 |
| 2013-2-22 | 2 | cat2 |
| 2013-3-22 | 1 | dog1 |
| 2013-3-22 | 2 | dog2 |
| 2013-1-22 | 1 | ewe1 |
| 2013-1-22 | 2 | ewe2 |
| 2013-8-22 | 1 | pig1 |
| 2013-8-22 | 2 | pig2 |
SELECT
a.rank, b.rank, c.rank,
a.example_variable as V1,
b.example_variable as V2,
c.example_variable as V3,
a.AsOfDate as 1stDate,
b.AsOfDate as 2ndDate,
c.AsOfDate as 3rdDate
FROM <table> a, <table> b, <table> c
WHERE a.rank = b.rank,
And b.rank = c.rank,
AND a.AsOfDate = '2013-1-22'
AND b.AsOfDate = '2013-2-22'
AND c.AsOfDate = '2013-3-22'
编辑1:即使使用更复杂的数据版本,您仍应能够使用行编号: 看 如果您没有row_编号,可以使用以下选项:
;with d1 as
(
select t.date, t.[rank], t.example_variable
from yt t
inner join
(
select min(date) date, [rank]
from yt
group by [rank]
) d
on t.date = d.date
and t.[rank] = d.[rank]
)
select d1.rank,
d1.example_variable v1,
d2.example_variable v2,
d3.example_variable v3,
d1.date [1stDate],
d2.date [2ndDate],
d3.date [3rdDate]
from d1
cross apply
(
select top 1 date, [rank], example_variable
from yt t
where d1.date < t.date
and d1.[rank] = t.[rank]
) d2
cross apply
(
select top 1 date, [rank], example_variable
from yt t
where d2.date < t.date
and d2.[rank] = t.[rank]
) d3;
select d1.example_variable v1,
d2.example_variable v2,
d3.example_variable v3,
d1.date [1stdate],
d2.date [2nddate],
d3.date [3rddate]
from
(
select top 1 date, example_variable
from yt
order by date
) d1
cross apply
(
select top 1 d2.date, d2.example_variable
from yt d2
where d1.date < d2.date
order by d2.date
) d2
cross apply
(
select top 1 d3.date, d3.example_variable
from yt d3
where d2.date < d3.date
order by d3.date
) d3;
看
如果您没有行\号,则可以使用以下选项:
;with d1 as
(
select t.date, t.[rank], t.example_variable
from yt t
inner join
(
select min(date) date, [rank]
from yt
group by [rank]
) d
on t.date = d.date
and t.[rank] = d.[rank]
)
select d1.rank,
d1.example_variable v1,
d2.example_variable v2,
d3.example_variable v3,
d1.date [1stDate],
d2.date [2ndDate],
d3.date [3rdDate]
from d1
cross apply
(
select top 1 date, [rank], example_variable
from yt t
where d1.date < t.date
and d1.[rank] = t.[rank]
) d2
cross apply
(
select top 1 date, [rank], example_variable
from yt t
where d2.date < t.date
and d2.[rank] = t.[rank]
) d3;
select d1.example_variable v1,
d2.example_variable v2,
d3.example_variable v3,
d1.date [1stdate],
d2.date [2nddate],
d3.date [3rddate]
from
(
select top 1 date, example_variable
from yt
order by date
) d1
cross apply
(
select top 1 d2.date, d2.example_variable
from yt d2
where d1.date < d2.date
order by d2.date
) d2
cross apply
(
select top 1 d3.date, d3.example_variable
from yt d3
where d2.date < d3.date
order by d3.date
) d3;
请参见我正在使用Azure SQL。然而,与性能相比,我更喜欢清晰和简单。在我看来,SQL Azure不支持窗口功能。@user1744318此链接显示它受支持-@user1744318查看我的编辑,我添加了一个没有行号的版本。您介意在自己的答案中添加第二个吗?就我的情况而言,第二个是非常可取的。第一个例子为我创造了更多的工作,因为我的真实世界的例子有我的例子中没有的复杂性。提前谢谢,如果我有点奇怪,请原谅。@user1744318实际上我更喜欢将它们保留在同一个答案中,因为它们都是您问题的解决方案。即使一个比另一个更受欢迎,您仍然可以接受: