Tuesday, October 14, 2014

SQL to display duplicate rows within a table

Because you don't want duplicate rows.

This works for me in Oracle. YMMV. Just change "table_name" to the table you wish to query.

Happy deduping!

select * from 

table_name -- change table name

where id_number in (SELECT id_number
FROM (SELECT ROW_NUMBER() 
OVER(PARTITION BY id_number ORDER BY id_number asc) AS r,
f.* FROM (select * from 

table_name -- change table name

) f) x WHERE x.r = 2)

order by id_number

Wednesday, April 2, 2014

Flagging Executives with SQL

Here's a bit of Oracle SQL code that will flag executives in your data, for use as a potentially very important independent variable in an analysis.

SELECT id_number, 
       report_name,
       business_title,
       'Y' as executive
  FROM your_table
 WHERE business_title like ('Part%')
    or business_title like ('Prin%')
    or business_title like ('Pres%')
    or business_title like ('Owne%')
    or business_title like ('Foun%')
    or business_title like ('Vice%')
    or business_title like ('Chie%')
    or business_title like ('Chai%')
    or business_title like ('VP%')
    or business_title like ('V.P.%')
    or business_title like ('C.E.%')
    or business_title like ('COO%')
    or business_title like ('CFO%')
    or business_title like ('CIO%')
    or business_title like ('CEO%')
 ORDER BY id_number