{"id":20003,"date":"2022-11-08T10:42:55","date_gmt":"2022-11-08T05:12:55","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-how-can-i-improve-this-query-to-avoid-using-nested-views-closed\/"},"modified":"2022-11-08T10:42:55","modified_gmt":"2022-11-08T05:12:55","slug":"solved-how-can-i-improve-this-query-to-avoid-using-nested-views-closed","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-how-can-i-improve-this-query-to-avoid-using-nested-views-closed\/","title":{"rendered":"[Solved] How can I improve this query to avoid using nested views? [closed]"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-10463280\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"10463280\" data-parentid=\"10462850\" data-score=\"11\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<h3>Familiarize with the data that you have:<\/h3>\n<p>The first key thing is to understand what data you have. Here in this case, you have four tables<\/p>\n<ul>\n<li>InsuranceCompanies<\/li>\n<li>Patient<\/li>\n<li>Doctors<\/li>\n<li>Visits<\/li>\n<\/ul>\n<h3>Your goal:<\/h3>\n<p>Find the list of all the patients who visited all orthopedists (specialty) associated with their Insurance Companies.<\/p>\n<h3>Let&#8217;s take a step back and analyze it in smaller pieces:<\/h3>\n<p>Generally, the requirements might be a bit overwhelming when you look at them on the whole. Let&#8217;s split the requirements into smaller components to understand what you need to do.<\/p>\n<ol>\n<li><em>Part a:<\/em> You need to find the list of doctors, whose speciality is &#8216;Orthopedist&#8217;<\/li>\n<li><em>Part b:<\/em> Find the list of patients who visited doctors identified in #1.<\/li>\n<li><em>Part c:<\/em> Filter the result #2 to find the list of patients and doctors who share the same insurance company.<\/li>\n<li><em>Part d:<\/em> Find out that the patients who visited each one of those <em>Orthopedists<\/em> who belong to the same insurance company as the patient do.<\/li>\n<\/ol>\n<h3>How to approach:<\/h3>\n<ol>\n<li>\n<p>You need to identify your main goal, here in this case to identify the list of patients. So, query the Patient table first.<\/p>\n<\/li>\n<li>\n<p>You have the patients, actually all of them but we need to find which of these patients visited the doctors. Let&#8217;s not worry about whether the doctor is an Orthopedist or not. We just need the list of patients and the doctors they have visited. There is no mapping between Patient and Doctors table. To find out this information,<\/p>\n<p><em>Join the Patient table with Visits table on the correct key field.<\/em><\/p>\n<p><em>Then join the output with the Doctors table on the correct key field.<\/em><\/p>\n<\/li>\n<li>\n<p>If you have done the join correctly, you should now have the list of all the patients and the doctors that they have visited. If you used <code>LEFT OUTER JOIN<\/code>, you will find even the patients who had never visited a doctor. If you used <code>RIGHT OUTER JOIN<\/code>, you will find only the patients who visited a doctor.<\/p>\n<\/li>\n<li>\n<p>Now, you have all the patients and the doctors whom they have visited. However, the requirement is to find only the doctors who are <em>Orthopedists<\/em>. So, apply the condition to filter the result to give only the desired result.<\/p>\n<\/li>\n<li>\n<p>You have now achieved the requirements as split into smaller components in <em>part a<\/em> and <em>part b<\/em>. You still need to filter it by the insurance companies. Here is the tricky part, the requirement doesn&#8217;t say that you need to display the insurance company, so we don&#8217;t have to use the table InsuranceCompanies. Your next question will <code>'How am I going to filter the results?'<\/code>. Valid point. Find out if any of the three tables <code>Patient<\/code>, <code>Doctor<\/code> and <code>Visits<\/code> contain the insurance company information. <code>Patient<\/code> and <code>Doctors<\/code> have a common field. Join that common field to filter the result.<\/p>\n<\/li>\n<li>\n<p>Find the count of unique <em>Orthopedists<\/em> that each patient has visited.<\/p>\n<\/li>\n<li>\n<p>Here is the part that can be done in many ways, one of the way of doing this would be to add a sub query that would be your fourth column in the output. This sub query would query the table Doctors and filter by speciality = &#8216;Orthopedist&#8217;. In addition to that filter, you also have to filter by matching the insurance company on the inner table with the insurance company id on the Patients table that is on the main query. This subquery will return the count of all the Orthopedists for insurance company id that matches the patient&#8217;s data.<\/p>\n<\/li>\n<li>\n<p>You should now have the fields <code>patient id<\/code>, <code>patient name<\/code>, <code>patients visits count<\/code> and the <code>total number of Orthopedists in same insurance company<\/code> from the sub query. You can then add an outer join that will filter the results from this derived table on the fields where <code>patients visits count<\/code> matches with  <code>total number of Orthopedists in same insurance company<\/code>. I am not saying this is the best approach. This is one approach that I can think of.<\/p>\n<\/li>\n<li>\n<p>If you follow the above logic, you should have this.<\/p>\n<\/li>\n<\/ol>\n<p>List of patients who have visited all the doctors<\/p>\n<p>Filtered by only doctors, whose are Orthopedists<\/p>\n<p>Filtered by patients and doctors sharing the same insurance company information.<\/p>\n<p>Again, the whole output is then filtered by the two count fields found inside the derived table output.<\/p>\n<h3>The ball is in your court:<\/h3>\n<ul>\n<li>Try it step by step and once you find the answer. Post it here as a separate answer. I will upvote it to compensate for all the downvotes that you got on this question.<\/li>\n<\/ul>\n<p>I am confident that you can do this easily.<\/p>\n<h3>If you stumble&#8230;<\/h3>\n<p>Don&#8217;t hesitate to post your questions as <code>comments to this answer<\/code>, Others and I will be glad to assist you.<\/p>\n<h3>Disclaimer<\/h3>\n<p>I have provided one of the many ways how this logic can be implemented. I am sure that there are many ways to implement this in a far better manner.<\/p>\n<h1>Outcome:<\/h1>\n<p>Please refer @Ofek Ron&#8217;s answer for the correct query that produces the desired output. I didn&#8217;t write any part of the query. It was all OP&#8217;s effort.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">4<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved How can I improve this query to avoid using nested views? [closed] <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Familiarize with the data that you have: The first key thing is to understand what data you have. Here in this case, you have four tables InsuranceCompanies Patient Doctors Visits Your goal: Find the list of all the patients who visited all orthopedists (specialty) associated with their Insurance Companies. Let&#8217;s take a step back &#8230; <a title=\"[Solved] How can I improve this query to avoid using nested views? [closed]\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-how-can-i-improve-this-query-to-avoid-using-nested-views-closed\/\" aria-label=\"More on [Solved] How can I improve this query to avoid using nested views? [closed]\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[340,341],"class_list":["post-20003","post","type-post","status-publish","format-standard","hentry","category-solved","tag-mysql","tag-sql"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] How can I improve this query to avoid using nested views? [closed] - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-how-can-i-improve-this-query-to-avoid-using-nested-views-closed\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] How can I improve this query to avoid using nested views? [closed] - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Familiarize with the data that you have: The first key thing is to understand what data you have. Here in this case, you have four tables InsuranceCompanies Patient Doctors Visits Your goal: Find the list of all the patients who visited all orthopedists (specialty) associated with their Insurance Companies. Let&#8217;s take a step back ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-how-can-i-improve-this-query-to-avoid-using-nested-views-closed\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-08T05:12:55+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-can-i-improve-this-query-to-avoid-using-nested-views-closed\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-can-i-improve-this-query-to-avoid-using-nested-views-closed\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] How can I improve this query to avoid using nested views? [closed]\",\"datePublished\":\"2022-11-08T05:12:55+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-can-i-improve-this-query-to-avoid-using-nested-views-closed\/\"},\"wordCount\":830,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"mysql\",\"sql\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-can-i-improve-this-query-to-avoid-using-nested-views-closed\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-how-can-i-improve-this-query-to-avoid-using-nested-views-closed\/\",\"name\":\"[Solved] How can I improve this query to avoid using nested views? [closed] - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-11-08T05:12:55+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-can-i-improve-this-query-to-avoid-using-nested-views-closed\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-how-can-i-improve-this-query-to-avoid-using-nested-views-closed\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-can-i-improve-this-query-to-avoid-using-nested-views-closed\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] How can I improve this query to avoid using nested views? [closed]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] How can I improve this query to avoid using nested views? [closed] - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-how-can-i-improve-this-query-to-avoid-using-nested-views-closed\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] How can I improve this query to avoid using nested views? [closed] - JassWeb","og_description":"[ad_1] Familiarize with the data that you have: The first key thing is to understand what data you have. Here in this case, you have four tables InsuranceCompanies Patient Doctors Visits Your goal: Find the list of all the patients who visited all orthopedists (specialty) associated with their Insurance Companies. Let&#8217;s take a step back ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-how-can-i-improve-this-query-to-avoid-using-nested-views-closed\/","og_site_name":"JassWeb","article_published_time":"2022-11-08T05:12:55+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-how-can-i-improve-this-query-to-avoid-using-nested-views-closed\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-can-i-improve-this-query-to-avoid-using-nested-views-closed\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] How can I improve this query to avoid using nested views? [closed]","datePublished":"2022-11-08T05:12:55+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-can-i-improve-this-query-to-avoid-using-nested-views-closed\/"},"wordCount":830,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["mysql","sql"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-how-can-i-improve-this-query-to-avoid-using-nested-views-closed\/","url":"https:\/\/jassweb.com\/solved\/solved-how-can-i-improve-this-query-to-avoid-using-nested-views-closed\/","name":"[Solved] How can I improve this query to avoid using nested views? [closed] - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-11-08T05:12:55+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-can-i-improve-this-query-to-avoid-using-nested-views-closed\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-how-can-i-improve-this-query-to-avoid-using-nested-views-closed\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-how-can-i-improve-this-query-to-avoid-using-nested-views-closed\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] How can I improve this query to avoid using nested views? [closed]"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/20003","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=20003"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/20003\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=20003"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=20003"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=20003"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}