Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Sub Query question

$
0
0

I'm working on a query that can probably be done by a simple subquery but I am struggling with getting the results.  I am working a dataset that contains information about student's Major and Minor.  Basically, everyone should have a major but doesn't have to have a minor.

Example of the data:

ID              Plan                            descr                                type

1234          ECON                         economics                        MAJ
1234          FREN                          french                               MIN
1235          BUS                            business                           MAJ

Desired output:

ID               MAJOR                            MINOR

1234          ECON                             FREN

1234          BUS                                <blank>

Query that I have so far:

select ap.*, descr, acad_plan_type from biodemo_s d
            INNER JOIN ADDRESSES  A
                  ON A.EMPlID = D.EMPLID
          AND A.e_addr_type = 'CAMP'
            INNER JOIN
                (select emplid, acad_plan, max(effdt)eff_dt
                        from ACAD_PLAN
                        group by Emplid, acad_plan
                )ap ON d.emplid = ap.emplid
       Inner JOIN
                (SELECT acad_plan, descr, acad_plan_type,Row_Number() OVER (PARTITION BY acad_plan ORDER BY effdt DESC) rn
                        FROM ACAD_PLAN_TBL
                )apl ON ap.acad_plan = apl.acad_plan
        AND rn = '1'




Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>