简体   繁体   English

从SQL表中提取当前数据值和相应的子值

[英]Extracting the current data value and corresponding subvalue from SQL tables

If this has been answered, just link me to the answer and I'll sit there going 'Why didn't I think of that for the search?' 如果已回答问题,只需将我链接到答案,我就会坐在那里说“为什么我没有为搜索找到答案?”

I have two databases which I'm trying to extract data from using SQL. 我有两个数据库,我正在尝试从使用SQL中提取数据。 The data bases are a parent and child. 数据库是父母和孩子。

Per - Parent, Hed - Child 每人-父母,被带孩子-子女

Per has the following columns of information: Site, varfrom, varto, sdate, table and others which aren't important Per具有以下信息列:Site,varfrom,varto,sdate,table和其他不重要的信息

Hed has the following columns of information: Site, varfrom, varto, table, release, name, and others which aren't important Hed具有以下信息列:网站,varfrom,varto,表,发行版,名称和其他不重要的信息

Per example - the true and false are one of the extra columns 每个示例-true和false是额外的列之一

STATION VARFROM VARTO   SDATE   STIME   REFSTN  REFTAB
221209A 100.00  141 8/11/2006   1200.00 221209A 23  FALSE
221209A 100.00  141 22/04/2007  400.00  221209A 23  TRUE
221209A 100.00  141 30/04/2007  1600.00 221209A 24  FALSE
221209A 100.00  141 2/11/2007   0.00    221209A 24  TRUE
221209A 100.00  141 5/11/2007   2000.00 221209A 25  FALSE
221209A 100.00  141 24/11/2008  0.00    221209A 26  FALSE
221209A 100.00  141 16/02/2010  45.00   221209A 27  FALSE
221210A 100.00  141 22/08/1972  1545.00 221210A 1   FALSE
221210A 100.00  141 3/06/1978   1500.00 221210A 2   FALSE
221210A 100.00  141 5/07/1982   1130.00 221210A 1   FALSE
221210A 100.00  141 28/07/1984  1225.00 221210A 3   FALSE
221210A 100.00  141 24/02/1987  1200.00 221210A 2   FALSE
221210A 100.00  141 28/04/1988  1600.00 221210A 4   FALSE
221210A 100.00  141 5/01/1992   1430.00 221210A 3   FALSE

Hed example 附例

STATION     VARFROM VARTO   TABLE   RELEASE NAME
221209A     100.00  141     23      0       23.0
221209A     100.00  141     24      0       24.0
221209A     100.00  141     25      2       25.02
221209A     100.00  141     25      1       25.01
221209A     100.00  141     25      0       25.00
221209A     100.00  141     26      0       26.00
221209A     100.00  141     27      0       27.00
221210A     100.00  141     1       3        1.03
221210A     100.00  141     1       2        1 B
221210A     100.00  141     2       6        2.06
221210A     100.00  141     2       5        2.05
221210A     100.00  141     2       4        2 D
221210A     100.00  141     3       1        3.01
221210A     100.00  141     3       0        3
221210A     100.00  141     4       2        4.02
221210A     100.00  141     4       1        4 A

So what I want is for it to return the most recent sdate for each site, then give me the current name for each site 所以我想要的是返回每个站点的最新日期,然后为我提供每个站点的当前名称

So the answer for the example would be 所以这个例子的答案是

Site    Varfrom Varto sdate      name
221209A 100.00  141   16/02/2010 27.00
221210A 100.00  141   5/01/1992   3.01

It doesn't matter if site comes from Per or Hed. 网站来自Per还是Hed都无关紧要。

Currently I have any number of coding attempts which give me out everything, except that the name is incorrect. 目前,我有许多编码尝试,除了名字不正确以外,其他所有功能我都无法理解。 Below is one example. 以下是一个示例。 I just can't see how to make it give me the latest sdate with the right name. 我只是看不到如何使它具有正确名称的最新日期。

select
 hed.site,
 hed.name,
 max(per.sdate) as "Period",
 hed.varfrom,
 hed.varto
from hed inner join per 
on hed.site=per.site AND
 hed.table=per.reftab
where
 hed.varfrom=100.00 
 AND  hed.varto=141

Any help would be very much appreciated. 任何帮助将不胜感激。

Try this 尝试这个

;with cte as (
select site,Varfrom ,Varto,reftable,sdate,r=ROW_NUMBER() over(partition by site order by sdate desc) from per
)

select per.site,per.Varfrom ,per.Varto,hed.name from cte per
inner join hed on per.site = hed.site and per.reftable=hed.reftable and per.r=1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM