如何合并这2个SQL?

如何合并这2个SQL?,sql,Sql,我需要获得每张卡的首次使用日期 我有这个SQLs SELECT distinct card from liq where dateF between '2016-06-09 10:00:00' and '2016-07-09 12:00:00' select top 1 * from liq where card = 1129535097095808 order by dateF asc 在第一张卡中,我获得每张卡的卡号。在第二行中,我必须找到每一行的第一个日期,但我不知道如何合并这两个S

我需要获得每张卡的首次使用日期

我有这个SQLs

SELECT distinct card from liq where dateF between '2016-06-09 10:00:00' and '2016-07-09 12:00:00' 

select top 1 * from liq where card = 1129535097095808 order by dateF asc
在第一张卡中,我获得每张卡的卡号。在第二行中,我必须找到每一行的第一个日期,但我不知道如何合并这两个SQL,因为如果在(query1)中使用“card”,它将只返回所有行中的第一行

有人能帮我吗


谢谢

最简单但不是最快的:

select top 1 * from liq where card in (SELECT distinct card from liq where dateF between '2016-06-09 10:00:00' and '2016-07-09 12:00:00' ) order by dateF asc

或者尝试在一个查询中执行此操作,如下所示

Select [card], MIN(dateF) over (partition by [card] order by DateF) from liq
    where dateF between '2016-06-09 10:00:00' and '2016-07-09 12:00:00'
group by [card]
所以你得到一个卡号,以及它的第一次使用日期

编辑:未测试查询,但请尝试以下操作:

WITH CTE AS(
        SELECT *,
               ROW_NUMBER() OVER( PARTITION BY CARD ORDER BY dateF) RN
        from liq 
        where dateF between '2016-06-09 10:00:00' and '2016-07-09 12:00:00'
)
SELECT CARD
FROM CTE 
WHERE RN=1 

尝试添加一些示例表数据和预期结果,以及格式良好的文本。标记您正在使用的dbms。
SELECT top 1 * FROM(SELECT distinct t1.card FROM liq t1 WHERE t1.dateF BETWEEN '2016-06-09 10:00:00' AND '2016-07-09 12:00:00') t2, liq t3 WHERE t2.card=t3.card ORDER BY t2.dateF ASC