2015-12-10 48 views
0

所以我想只显示2个键值对,如果他们的日期是相同的。因此,举例来说,如果一个键值对不具有完全相同的日期,时间,另一键值对,那就不要让他们在所有...如何选择日期相同的2个键值对的值?

所以我的逻辑如下,但我不知道如何实现它:

SELECT ForeignKeyID, Value, Date 
    FROM tblMyTable 
//declare myDate= null ???? 
    where (ForeignKeyID = 1 and Value > 14) or (ForeignKeyID = 2 and Value > 10) 
//somehow check if myDate from the first key-value pair has the exact same myDate as the second one or vice versa. 

这里是我想要的,不想显示:

Time     ForeignKeyID Value 
2015-05-27 00:30:00.000  1  15.000 //Yes because same date for 2 exists 
2015-05-27 00:30:00.000  2  13.800 //Yes because same date for 1 exists 
2015-05-27 00:45:00.000  1  15.000 //Yes 
2015-05-27 00:45:00.000  2  13.800 //Yes 
2015-05-27 01:00:00.000  1  15.000 //Yes 
2015-05-27 01:00:00.000  2  13.300 //Yes 
2015-05-27 01:15:00.000  1  15.000 //Yes 
2015-05-27 01:15:00.000  2  13.300 //Yes 
2015-05-27 01:30:00.000  2  13.300 //No because the same date for 1 doesnt exist! 
2015-05-27 01:45:00.000  2  13.300 //No because the same date for 1 doesnt exist! 
2015-05-27 02:00:00.000  1  15.000 //No because the same date for 2 doesnt exist! 
2015-05-27 02:15:00.000  1  15.000 //No because the same date for 2 doesnt exist! 

在这里我的无知,实在不好意思,但我想在这里学习...

+1

谷歌和了解SQL中的WHERE EXISTS()函数。 –

回答

1

在这种情况下,如精灵连接可能是最简单的:

select t1.ForeignKeyID, t1.Value, t1.Date, 
     t2.ForeignKeyID, t2.Value, t2.Date 
from tblMyTable t1 join 
    tblMyTable t2 
    on t1.ForeignKeyID = 1 and t1.value > 14 and 
     t2.ForeignKeyID = 2 and t2.value > 10 and 
     t1.date = t2.date; 

这确实将匹配值放在一行而不是两行。

编辑:

如果你想两行,你可以使用复杂的exists逻辑:

select t.ForeignKeyID, t.Value, t.Date 
from tblMyTable t 
where (t.ForeignKeyID = 1 and t.value > 14 and 
     exists (select 1 
       from tblMyTable t2 
       where t2.ForeignKeyID = 2 and t2.value > 10 and t2.date = t.date 
      ) 
    ) or 
     (t.ForeignKeyID = 2 and t.value > 10 and 
     exists (select 1 
       from tblMyTable t2 
       where t2.ForeignKeyID = 1 and t2.value > 14 and t2.date = t.date 
      ) 
    ); 

我认为第一个版本可能更加有用。

或者,更简单的方法是使用窗口功能:

select t.ForeignKeyID, t.Value, t.Date 
from (select t.*, 
      sum(case when ForeignKeyID = 1 and value > 14 then 1 else 0 end) over (partition by date) as cnt1, 
      sum(case when ForeignKeyID = 2 and value > 10 then 1 else 0 end) over (partition by date) as cnt2 
     from mytable t 
     where (ForeignKeyID = 1 and Value > 14) or (ForeignKeyID = 2 and Value > 10) 
    ) t 
where cnt1 > 0 and cnt2 > 0 
+0

我按3分钟前执行的查询和处理依旧,奇怪的事情是,它几乎立即当我这样做是没有日期限制... – Vrankela

+0

所以第一个是仍然在过去的15分钟执行。至于第二个(在存在逻辑)给我一个空的结果,我知道是不可能的,因为我从该表中的例子... 至于第三个,林不知道什么是应该做的? – Vrankela

+0

@弗兰克拉。 。 。你必须有大量的数据。目前还不清楚为什么第二个会返回一个空的结果集。 –

0

您可以使用分析功能计数结束,看你每次约会多少个键获取:

select foreignkeyid, value, date 
from 
(
    select 
    foreignkeyid, 
    value, 
    date, 
    count(distinct foreignkeyid) over (partition by date) as cnt 
    from tblmytable 
    where (foreignkeyid = 1 and value > 14) or (foreignkeyid = 2 and value > 10) 
) counted 
where cnt = 2 
order by date, foreignkeyid; 
+0

忘记我评论了任何内容,它只会混淆其他人... – Vrankela