Jay*_*y C 1 sql sql-server sql-server-2012 data-partitioning
我正在使用SQL Server 2012我有以下示例数据
Date Type Symbol Price
6/30/1995 gaus 313586U72 109.25
6/30/1995 gbus 313586U72 108.94
6/30/1995 csus NES 34.5
6/30/1995 lcus NES 34.5
6/30/1995 lcus NYN 40.25
6/30/1995 uaus NYN 40.25
6/30/1995 agus SRR 10.25
6/30/1995 lcus SRR 0.45
7/1/1995 gaus 313586U72 109.25
7/1/1995 gbus 313586U72 108.94
Run Code Online (Sandbox Code Playgroud)
我想在符号和价格匹配时过滤掉.如果类型不匹配就没问题.因此,根据上述数据,我希望只能看到
Date Type Symbol Price
6/30/1995 gaus 313586U72 109.25
6/30/1995 gbus 313586U72 108.94
6/30/1995 agus SRR 10.25
6/30/1995 lcus SRR 0.45
7/1/1995 gaus 313586U72 109.25
7/1/1995 gbus 313586U72 108.94
Run Code Online (Sandbox Code Playgroud)
NES和NYN已被过滤掉,因为它们的符号和价格匹配.
我在考虑使用分区和行号,但我不确定如何使用该函数或其他函数对行和过滤行.
***更新我将测试回复.我应该提到我只想看到同一日期出现的符号和价格的重复.该表也称为duppri
一种方法是使用exists
带有相关子查询的谓词,该子查询检查特定符号是否具有多个价格:
select * from table1 t
where exists (
select 1
from table1
where symbol = t.symbol
and price <> t.price);
Run Code Online (Sandbox Code Playgroud)
这将返回:
| Date | Type | Symbol | Price |
|------------------------|------|-----------|--------|
| June, 30 1995 02:00:00 | gaus | 313586U72 | 109.25 |
| June, 30 1995 02:00:00 | gbus | 313586U72 | 108.94 |
| June, 30 1995 02:00:00 | agus | SRR | 10.25 |
| June, 30 1995 02:00:00 | lcus | SRR | 0.45 |
| July, 01 1995 02:00:00 | gaus | 313586U72 | 109.25 |
| July, 01 1995 02:00:00 | gbus | 313586U72 | 108.94 |
Run Code Online (Sandbox Code Playgroud)
编辑:由Gordon Linoffs激发灵感回答另一个选项可能是avg()
用作窗口函数:
select Date, Type, Symbol, Price
from (
select Date, Type, Symbol, Price, avg = avg(price) over (partition by symbol)
from table1) a
where avg <> price;
Run Code Online (Sandbox Code Playgroud)
编辑:检查以确保仅返回同一日期的重复项:http://www.sqlfiddle.com/#!6/29d67/1
归档时间: |
|
查看次数: |
167 次 |
最近记录: |