最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

oracle database - How can I return values from multiple columns as well as 'Yes' or 'No' to show

programmeradmin1浏览0评论

I'm trying to pull data relating to a certain type of customer ID. For one of the columns, I need to know only whether there is a value present or not, while still returning all values for the rest of the columns. E.g.

Customer Type ID Country ID Type ID Number
Personal Australia Drivers Licence XX12345
Commercial New Zealand Passport XX12345
Commercial New Zealand Drivers Licence null
Personal New Zealand Electronic XX12345678

I'm trying to pull data relating to a certain type of customer ID. For one of the columns, I need to know only whether there is a value present or not, while still returning all values for the rest of the columns. E.g.

Customer Type ID Country ID Type ID Number
Personal Australia Drivers Licence XX12345
Commercial New Zealand Passport XX12345
Commercial New Zealand Drivers Licence null
Personal New Zealand Electronic XX12345678

In the last column I only want 'Yes' or 'No' values to appear. If there is a value, then 'Yes' else 'No'.

I have been searching for hours, and I found that others have used "select case when exists" but I don't understand how I could make that work for me in this scenario.

To get the values, I need to reference 2 tables. This is the query I used to pull all of the data.

SELECT a.Customer_Type, b.ID_Country, b.ID_Type, b.ID_Number, count(\*)  
FROM table a, table b  
WHERE a.Table_A_Customer_Number = b.Table_B_Customer_Number  
GROUP BY a.Customer_Type, b.ID_Country, b.ID_Type, b.ID_Number

This has given me all values for all columns, where I'd like to only see 'Yes' or 'No' for b.ID_Number.

Share Improve this question edited 3 hours ago MT0 168k11 gold badges66 silver badges127 bronze badges asked 7 hours ago SQL NoobSQL Noob 11 silver badge1 bronze badge New contributor SQL Noob is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Add a comment  | 

1 Answer 1

Reset to default 2

That looks like a case expression indeed, but without exists.

  SELECT a.customer_type,
         b.id_country,
         b.id_type,
         b.id_number,
         COUNT (*),
         CASE WHEN b.id_number IS NOT NULL THEN 'Yes' ELSE 'No' END flag
    FROM tablea a, tableb b
   WHERE a.table_a_customer_number = b.table_b_customer_number
GROUP BY a.customer_type,
         b.id_country,
         b.id_type,
         b.id_number;

Or, as Tim suggested in a comment, join tables explicitly and leave where clause for filtering purposes (if any):

  SELECT a.customer_type,
         b.id_country,
         b.id_type,
         b.id_number,
         COUNT (*),
         CASE WHEN b.id_number IS NOT NULL THEN 'Yes' ELSE 'No' END flag
    FROM tablea a join tableb b 
         ON a.table_a_customer_number = b.table_b_customer_number
GROUP BY a.customer_type,
         b.id_country,
         b.id_type,
         b.id_number;
发布评论

评论列表(0)

  1. 暂无评论