<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Interesting Query - SQL Challenge ! in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/interesting-query-sql-challenge/m-p/13806060#M2030388</link>
    <description>&lt;P&gt;Understanding this question was posted and answered during the beginning of COVID-19, we're in another wave so this query hit my search results. I have a similar algorithm, though not yet avoiding this hurdle:&lt;/P&gt;&lt;P&gt;&lt;EM&gt;avoid the referencing on YSAM_SICKNESS being done twice&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;Instead of a JOIN, I have nested select clauses. Entering column names instead of "*" in a count function makes your intent more clear.&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;having 
  count(patientid) = (
    select
      count(symptomid)&lt;/LI-CODE&gt;&lt;P&gt;&lt;BR /&gt;&lt;EM&gt;instead of:&lt;/EM&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;having &lt;BR /&gt;  count(*) = (&lt;BR /&gt;    select&lt;BR /&gt;      count(*)&lt;/PRE&gt;&lt;LI-CODE lang="sql"&gt;select
  patientid,
  count(patientid),
  min(symptomid),
  max(symptomid)
from
  ysam_pat_tests
where
  symptomid in (
    select
        symptomid
      from
        ysam_sickness
      where
        sicknessname = 'Corona Virus'
      )
group by
  patientid
having
  count(patientid) = (
    select
      count(symptomid)
    from
      ysam_sickness
    where
      sicknessname = 'Corona Virus'
)
;&lt;/LI-CODE&gt;&lt;P&gt;results locally (not HANA):&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;select patientid, min(patientname) as nm, count(patientid), min(symptomid), max(symptomid) from ysam_pat_tests where symptomid in (select symptomid from ysam_sickness where sicknessname = 'Corona Virus') group by patientid having count(patientid) = (select count(symptomid) from  ysam_sickness where sicknessname = 'Corona Virus');

 patientid | nm | count | min | max
-----------+----+-------+-----+-----
      1002 | B  |     2 |   1 |   2
      1003 | C  |     2 |   1 |   2
(2 rows)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 24 Aug 2024 23:03:53 GMT</pubDate>
    <dc:creator>JimSpath</dc:creator>
    <dc:date>2024-08-24T23:03:53Z</dc:date>
    <item>
      <title>Interesting Query - SQL Challenge !</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/interesting-query-sql-challenge/m-p/12197376#M1981394</link>
      <description>&lt;P&gt;Hi Experts,&lt;/P&gt;
  &lt;P&gt;In one of my project requirements , I came upon a challenging SQL scenario.. ( &lt;EM&gt;which I am still working on to get the best possible option &lt;/EM&gt;).&lt;/P&gt;
  &lt;P&gt; So I wanted to share this interesting query for others to try and see the approaches we can have . (Apologies but I am not able to share the DDL scripts ). We have the following tables for Symptoms Master(YSAM_SYMPTOMS), Sickness Master(YSAM_SICKNESS) which tells which all the symptoms for a sickness and Patient Test Results transaction table(YSAM_PAT_TESTS).&lt;/P&gt;
  &lt;P&gt;The task is to write a SQL query for "Getting the list of all Patient IDs and Patient Names having ALL THE symptoms of Corono Virus".&lt;/P&gt;
  &lt;P&gt;&lt;IMG class="migrated-image" src="https://community.sap.com/legacyfs/online/storage/attachments/storage/7/attachments/1788562-interesting-sql-challenge.png" /&gt;&lt;/P&gt;
  &lt;P&gt;The solution should be in single open SQL only &lt;STRONG&gt;( no ABAP or AMDP logic. Also the table YSAM_SYMPTOMS master table is for reference and its not needed in the Query ) . &lt;/STRONG&gt;I have one solution for this but I don't think its that elegant. So request the community to share their thoughts on the SQL Query.&lt;/P&gt;
  &lt;P&gt;Note : My current system is SAP_ABAP - 7.50 ( so no CTEs also) with HANA as DB. &lt;/P&gt;
  &lt;P&gt;Regards,&lt;/P&gt;
  &lt;P&gt;Samson&lt;/P&gt;</description>
      <pubDate>Sun, 15 Mar 2020 02:15:01 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/interesting-query-sql-challenge/m-p/12197376#M1981394</guid>
      <dc:creator>samson_moses</dc:creator>
      <dc:date>2020-03-15T02:15:01Z</dc:date>
    </item>
    <item>
      <title>Re: Interesting Query - SQL Challenge !</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/interesting-query-sql-challenge/m-p/12197377#M1981395</link>
      <description>&lt;P&gt;While I can understand not being allowed to provide the full DDL and real data from the system, I'm not at all clear why you don't think you can provide simplified ddl and sample data for this query.&lt;/P&gt;&lt;P&gt;I've simplified it down a bit more.  My system is SAP Adaptive Server Enterprise, so the TSQL may need a little tweaking for HANA.&lt;/P&gt;&lt;P&gt;create table YSAM_PAT_TESTS (PATIENTID int, SYMPTOMID int) &lt;/P&gt;&lt;P&gt;create table YSAM_SICKNESS (SICKNESSNAME varchar(20), SYMPTOMID int) &lt;/P&gt;&lt;P&gt; insert YSAM_PAT_TESTS values (1001, 01)
insert YSAM_PAT_TESTS values (1001, 03)
&lt;BR /&gt;&lt;BR /&gt;insert YSAM_PAT_TESTS values (1002, 01)
insert YSAM_PAT_TESTS values (1002, 02)
&lt;BR /&gt;insert YSAM_PAT_TESTS values (1003, 01)
insert YSAM_PAT_TESTS values (1003, 02)
&lt;BR /&gt;insert YSAM_PAT_TESTS values (1003, 06)
insert YSAM_PAT_TESTS values (1004, 02)
&lt;BR /&gt;insert YSAM_PAT_TESTS values (1004, 04)
insert YSAM_PAT_TESTS values (1004, 07)
&lt;BR /&gt;insert YSAM_PAT_TESTS values (1005, 06)
insert YSAM_PAT_TESTS values (1005, 07) &lt;BR /&gt;insert YSAM_SICKNESS values ("Common Flu", 01)
&lt;BR /&gt;insert YSAM_SICKNESS values ("Corona Virus", 01)
&lt;BR /&gt;insert YSAM_SICKNESS values ("Corona Virus", 02)
&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;SELECT
&lt;BR /&gt;p.PATIENTID
&lt;BR /&gt;FROM
YSAM_PAT_TESTS p,
YSAM_SICKNESS s
&lt;BR /&gt;WHERE
p.SYMPTOMID = s.SYMPTOMID
&lt;BR /&gt;AND s.SICKNESSNAME = "Corona Virus"
&lt;BR /&gt;GROUP BY
 p.PATIENTID
&lt;BR /&gt;HAVING 
count(*) = (
&lt;BR /&gt;SELECT count(*) &lt;BR /&gt;FROM YSAM_SICKNESS 
&lt;BR /&gt;WHERE SICKNESSNAME = "Corona Virus"
) &lt;/P&gt;</description>
      <pubDate>Sun, 15 Mar 2020 05:01:29 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/interesting-query-sql-challenge/m-p/12197377#M1981395</guid>
      <dc:creator>former_member188958</dc:creator>
      <dc:date>2020-03-15T05:01:29Z</dc:date>
    </item>
    <item>
      <title>Re: Interesting Query - SQL Challenge !</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/interesting-query-sql-challenge/m-p/12197378#M1981396</link>
      <description>&lt;PRE&gt;&lt;CODE&gt;SELECT PATIENTID, PATIENTNAME,
  CASE WHEN SYMPTOMID = '01' THEN 'FEVER' ELSE '' END AS "FEVER",
  CASE WHEN SYMPTOMID = '02' THEN 'COLD'  ELSE '' END AS "COLD",
FROM
(SELECT PATIENTID, PATIENTNAME, SYMPTOMID FROM YSAM_PAT_TESTS  
      WHERE SYMPTOMID IN 
      (SELECT SYMPTOMID FROM YSAM_SICKNESS WHERE SICKNESSID = '02' )) AS Z
HAVING COUNT(*) = (SELECT COUNT(*) FROM YSAM_SICKNESS WHERE SICKNESSID = '02')
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 15 Mar 2020 07:16:11 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/interesting-query-sql-challenge/m-p/12197378#M1981396</guid>
      <dc:creator>venkateswaran_k</dc:creator>
      <dc:date>2020-03-15T07:16:11Z</dc:date>
    </item>
    <item>
      <title>Re: Interesting Query - SQL Challenge !</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/interesting-query-sql-challenge/m-p/12197379#M1981397</link>
      <description>&lt;P&gt;Hi Venkat,&lt;/P&gt;&lt;P&gt;Thanks for the response.&lt;/P&gt;&lt;P&gt;I too had the same kind of solution ( as below ). &lt;/P&gt;&lt;P&gt;&lt;IMG class="migrated-image" src="https://community.sap.com/legacyfs/online/storage/attachments/storage/7/attachments/1788577-query.png" /&gt;&lt;/P&gt;&lt;P&gt;But somehow I wanted to avoid the sub-query on YSAM_SICKNESS being done twice. So I am going to keep the thread open for now to see other options.  &lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Samson&lt;/P&gt;</description>
      <pubDate>Sun, 15 Mar 2020 18:13:57 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/interesting-query-sql-challenge/m-p/12197379#M1981397</guid>
      <dc:creator>samson_moses</dc:creator>
      <dc:date>2020-03-15T18:13:57Z</dc:date>
    </item>
    <item>
      <title>Re: Interesting Query - SQL Challenge !</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/interesting-query-sql-challenge/m-p/12197380#M1981398</link>
      <description>&lt;P&gt;Hi Brad,&lt;/P&gt;&lt;P&gt;Sincere apologies for not thinking of providing the simplified DDL. Also thanks for sharing the DDL and taking out your time for responding. &lt;/P&gt;&lt;P&gt;Your query would definitely work, but I wanted to avoid the referencing on YSAM_SICKNESS being done twice (either via JOIN or Sub-Query, like Venkat's response ) . &lt;/P&gt;&lt;P&gt;So I am going to keep the thread open for now to see other options.&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Samson.&lt;/P&gt;</description>
      <pubDate>Sun, 15 Mar 2020 18:18:15 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/interesting-query-sql-challenge/m-p/12197380#M1981398</guid>
      <dc:creator>samson_moses</dc:creator>
      <dc:date>2020-03-15T18:18:15Z</dc:date>
    </item>
    <item>
      <title>Re: Interesting Query - SQL Challenge !</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/interesting-query-sql-challenge/m-p/13799987#M2030194</link>
      <description>&lt;DIV&gt;&lt;DIV&gt;&lt;P&gt;&lt;SPAN&gt;select &lt;/SPAN&gt;&lt;SPAN&gt;p&lt;/SPAN&gt;&lt;SPAN&gt;~&lt;/SPAN&gt;&lt;SPAN&gt;patientid&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;p&lt;/SPAN&gt;&lt;SPAN&gt;~&lt;/SPAN&gt;&lt;SPAN&gt;patientname&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;from &lt;/SPAN&gt;&lt;SPAN&gt;YSAM_PAT_TESTS&lt;/SPAN&gt;&lt;SPAN&gt; AS &lt;/SPAN&gt;&lt;SPAN&gt;p&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;join &lt;/SPAN&gt;&lt;SPAN&gt;ysam_sickness&lt;/SPAN&gt;&lt;SPAN&gt; as &lt;/SPAN&gt;&lt;SPAN&gt;s&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;on &lt;/SPAN&gt;&lt;SPAN&gt;p&lt;/SPAN&gt;&lt;SPAN&gt;~&lt;/SPAN&gt;&lt;SPAN&gt;symptomid&lt;/SPAN&gt;&lt;SPAN&gt; = &lt;/SPAN&gt;&lt;SPAN&gt;s&lt;/SPAN&gt;&lt;SPAN&gt;~&lt;/SPAN&gt;&lt;SPAN&gt;symptomid&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;and &lt;/SPAN&gt;&lt;SPAN&gt;sicknessid&lt;/SPAN&gt;&lt;SPAN&gt; = &lt;/SPAN&gt;&lt;SPAN&gt;'02'&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;GROUP BY &lt;/SPAN&gt;&lt;SPAN&gt;patientid&lt;/SPAN&gt;&lt;SPAN&gt; , &lt;/SPAN&gt;&lt;SPAN&gt;patientname&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;having count(*) = ( SELECT count(*)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;FROM &lt;/SPAN&gt;&lt;SPAN&gt;YSAM_SICKNESS&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;WHERE &lt;/SPAN&gt;&lt;SPAN&gt;sicknessid&lt;/SPAN&gt;&lt;SPAN&gt; = &lt;/SPAN&gt;&lt;SPAN&gt;'02'&lt;/SPAN&gt;&lt;SPAN&gt; )&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;into table &lt;a href="https://community.sap.com/t5/user/viewprofilepage/user-id/1407137"&gt;@DATA&lt;/a&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;wlt_pat&lt;/SPAN&gt;&lt;SPAN&gt;).&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Mon, 19 Aug 2024 22:12:20 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/interesting-query-sql-challenge/m-p/13799987#M2030194</guid>
      <dc:creator>Tulasi_chinni</dc:creator>
      <dc:date>2024-08-19T22:12:20Z</dc:date>
    </item>
    <item>
      <title>Re: Interesting Query - SQL Challenge !</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/interesting-query-sql-challenge/m-p/13806060#M2030388</link>
      <description>&lt;P&gt;Understanding this question was posted and answered during the beginning of COVID-19, we're in another wave so this query hit my search results. I have a similar algorithm, though not yet avoiding this hurdle:&lt;/P&gt;&lt;P&gt;&lt;EM&gt;avoid the referencing on YSAM_SICKNESS being done twice&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;Instead of a JOIN, I have nested select clauses. Entering column names instead of "*" in a count function makes your intent more clear.&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;having 
  count(patientid) = (
    select
      count(symptomid)&lt;/LI-CODE&gt;&lt;P&gt;&lt;BR /&gt;&lt;EM&gt;instead of:&lt;/EM&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;having &lt;BR /&gt;  count(*) = (&lt;BR /&gt;    select&lt;BR /&gt;      count(*)&lt;/PRE&gt;&lt;LI-CODE lang="sql"&gt;select
  patientid,
  count(patientid),
  min(symptomid),
  max(symptomid)
from
  ysam_pat_tests
where
  symptomid in (
    select
        symptomid
      from
        ysam_sickness
      where
        sicknessname = 'Corona Virus'
      )
group by
  patientid
having
  count(patientid) = (
    select
      count(symptomid)
    from
      ysam_sickness
    where
      sicknessname = 'Corona Virus'
)
;&lt;/LI-CODE&gt;&lt;P&gt;results locally (not HANA):&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;select patientid, min(patientname) as nm, count(patientid), min(symptomid), max(symptomid) from ysam_pat_tests where symptomid in (select symptomid from ysam_sickness where sicknessname = 'Corona Virus') group by patientid having count(patientid) = (select count(symptomid) from  ysam_sickness where sicknessname = 'Corona Virus');

 patientid | nm | count | min | max
-----------+----+-------+-----+-----
      1002 | B  |     2 |   1 |   2
      1003 | C  |     2 |   1 |   2
(2 rows)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 24 Aug 2024 23:03:53 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/interesting-query-sql-challenge/m-p/13806060#M2030388</guid>
      <dc:creator>JimSpath</dc:creator>
      <dc:date>2024-08-24T23:03:53Z</dc:date>
    </item>
  </channel>
</rss>

