我可能会重复问这个问题,但请再问一遍,因为我已经尝试了所有方法,但我遗漏了一些东西。
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 子句来获得所需的结果。
所以伪代码是这样的
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
感谢您的帮助。
最佳答案
试试这个(对于 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'
关于mysql - SQL 查询从列中获取不同的数据并在结果中获取其余数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42339727/