Solved

SQL query to find duplicate values using the HAVING clause

Posted on 2014-09-25
3
281 Views
Last Modified: 2014-09-25
I'm trying to find duplicate values using the HAVING clause, but the SQL query below is returning the following error: "SQL command not properly ended". What am I doing wrong?

SELECT enrol.rep_start_year, 
         uni.university_code,
         enrol.student_oen,
         COUNT(*) AS duplicate_oen_count
         
    FROM (SELECT rep_start_year, period,
                     student_oen,
                 fee_cat_type_id,
                 uni_institution_type_id,
                 reg_status_type_id
            FROM edcs_unistat.ustat_enrolment_data) enrol
         INNER JOIN edcs_unistat.uni_institution_type uniinst
             ON enrol.uni_institution_type_id = uniinst.uni_institution_type_id
         INNER JOIN
         (SELECT u.university_id,
                 (CASE
                      WHEN u.university_code = 'DOMC' THEN 'CARL'
                      WHEN u.university_code = 'NOSM-LAKE' THEN 'NOSM'
                      WHEN u.university_code = 'NOSM-LAUR' THEN 'NOSM'
                      WHEN u.university_code = 'LAUR-ALGM' THEN 'ALGM'
                      WHEN u.university_code = 'LAUR-HRST' THEN 'HRST'
                      WHEN u.university_code = 'DOMC' THEN 'CARL'
                      ELSE u.university_code
                  END)
                     AS university_code
            FROM edcs_core.university u) uni
             ON uniinst.university_id = uni.university_id
         INNER JOIN edcs_unistat.reg_status_type regstatus
             ON enrol.reg_status_type_id = regstatus.reg_status_type_id
         INNER JOIN edcs_unistat.fee_cat_type feetyp ON enrol.fee_cat_type_id = feetyp.fee_cat_type_id

where       enrol.rep_start_year in ('2012', '2013')
AND regstatus.reg_status_type_code IN ('12','13','14','15','16','18')
AND feetyp.fee_cat_type_code IN ('3', '4')
and         enrol.period in ('FAL')

GROUP BY enrol.rep_start_year, uni.university_code, enrol.student_oen
ORDER BY enrol.rep_start_year, uni.university_code, enrol.student_oen      

HAVING COUNT(*) > 1

Open in new window

0
Comment
Question by:mariita
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 35

Expert Comment

by:johnsone
ID: 40344557
First thing I would do is get rid of the blank lines.  Some tools don't like them.
0
 
LVL 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
ID: 40344564
Move the order by.  HAVING is part of the GROUP BY:
GROUP BY enroll.rep_start_year, uni.university_code, enroll.student_oen
HAVING COUNT(*) > 1
ORDER BY enroll.rep_start_year, uni.university_code, enroll.student_oen
0
 

Author Closing Comment

by:mariita
ID: 40344622
Thanks!
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to trim a value in SQL 2 50
Trying to understand why my Index is so large 12 52
Need more granular date groupings 4 44
Oracle Partitions. 1 20
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question