SQL查询从列中获取不同的数据并在结果中获取休息数据

时间:2017-02-20 08:32:59

标签: mysql sql

我可能会问这个问题是一个重复的问题,但请仔细检查一下,因为我已经尝试了所有内容而我错过了一些东西。

select 

    Q.f_query_id,
    Q.cust_id,  
    Q.current_agent,
    Q.current_agent_time,
    Q.current_status,
    Q.query_type,
    Q.pax_name,
    Q.pax_no,
    Q.pax_email,
    Q.start_date,   
    Q.return_date,
    Q.origin_code,
    Q.destination_code,
    Q.adult,
    Q.child,
    Q.infant,
    Q.class,
    Q.linked_pnrs,
    Q.followup_time

from a_query Q 
inner join
(
    select MIN(cust_id) cust_id, pax_email
    from a_query where current_status = 'F'
    group by pax_email
) Q1
    on Q.cust_id = Q1.cust_id
    and Q.pax_email= Q1.pax_email

请帮忙

我有这个查询我想要做的是获得唯一的客户ID(cust_id)以及所有其他列。因为在数据中有很多cust_id,它们在同一时间重复多次,我也想在一列(current_status)上使用where子句来获得所需的结果。

所以psuedo代码就像这样

 select * from a_query where cust_id is unique and status = 'f'

样本表

f_query_id  cust_id  current_status ......    pax_email
1          1              F               abc@gmail.com
2          2              B               xyz@gmail.com
3          1              F               abc@gmail.com

期望的结果

f_query_id  cust_id  current_status ......    pax_email
1          1              F               abc@gmail.com

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

试试这个(对于SQL Server)

;WITH commonTableExpression AS(
SELECT  *, 
        ROW_NUMBER() OVER(partition by cust_id ORDER BY cust_id)  rowNumber
FROM    a_query
WHERE   current_status = 'F'
)
SELECT * FROM commonTableExpression WHERE rowNumber = 1

对于MySQL

SELECT t.*,
       @prev_value := cust_id
FROM   a_query
WHERE  (IF(@prev_value = cust_id, @row_num + 1, 1) = 1)
       AND current_status = 'F'

答案 1 :(得分:0)

SELECT
    T.userID, T.summedScore, Tmod.modif AS modifier, (T.summedScore *  NZ(Tmod.modif,1)) AS finalscore
FROM
(
    SELECT [userID], sum([score]) AS summedScore
    FROM tbl_KPIScores
    WHERE Modifier=False
    GROUP BY UserID
) AS T
LEFT JOIN
(
    SELECT userID, FIRST([score]) AS modif 
    FROM tbl_KPIScores
    WHERE Modifier=True
    GROUP BY UserID
) AS Tmod ON T.userID=Tmod.userID

感谢大家解决了这个问题。