I have a question in postgresql; I've tried using a cursor for the following but could not get this to work.
I have a large employee table that has multiple columns, one of them being the company name, see below:
The master table is not sorted by date, it needs to be sorted during the filtering process. This table can have more than 100,000 records; I want to create a 2nd table that will have only some of the records (by each company name) - essentially, get the rows for each company name by pre-defined number of records and then union the tables together into a new table. My pre-defined records table would look something like this
Ideally, I could have more than 1000 companies in my original, master table and I might need only 50 companies and some records for the 50 companies that will be defined in my records table. How can this be done in postgresql
Sample Output: Records are the number of records that are mentioned in the pre-defined records desired table and are sorted by date



cursorbut could not get it to workselect * from employee_table where company_name in (select company_name from pre_defined_records_table)select * from employee_table t1 left join pre_defined_records_table t2 on t1.company_name = t2.company_name where t1.company_name in (select company_name from pre_defined_records_table)number of required recordsi.e. the select query should only returnxnumber of records which are specified in the 2nd table. SoApplehas 3 records andAmazonhas 2 in the output as compared to 6 and 5 records I would get if I just did what you said in your comment.