0

I have a table which contains a column called region, the region contains 2 values - Mexico and USA. I want to download a sample subset of the table, but what I need is the values should be based on both USA and MEXICO.


I have tried these queries - SELECT * FROM TBL WHERE REGION LIKE 'us%' or REGION LIKE 'mexic%'

SELECT * FROM TBL WHERE REGION IN ("us", "mexico") LIMIT 1000

The AND condition doesn't works in the above query. Also the above query only returns one of the regions. What I need is 50% of USA and 50% MEXICO in the sample table. Can this be done using any window functions or any simpler method would also be helpful.

Sample table

P.S - The sample record is to be showcased in a DECK (PPT) presentation.


Mohseen Mulla
  • 542
  • 7
  • 15
  • Do you want to limit based on the minimum amount of regions? (like if there is 4 Mexico and 3 USA you want to return 3 Mexico and 3 USA) And do you have a snippet of the table please ? – Paul Oct 02 '21 at 12:48
  • Hi @Paul its a huge table with millions of records, what i want exactly is a smaller subset of the data lets say 1k records but for both the regions. – Mohseen Mulla Oct 02 '21 at 12:49

1 Answers1

1

If you want an arbitrary amount of rows, you can use unions :

(SELECT * FROM TBL WHERE REGION LIKE 'mexic%' LIMIT 5)
UNION
(SELECT * FROM TBL WHERE REGION LIKE 'us%' LIMIT 5);
Paul
  • 314
  • 2
  • 13
  • Throws out a parse exception EOF. If i remove the first LIMIT 5, it still gives me only US records – Mohseen Mulla Oct 02 '21 at 12:58
  • Can you provide an example with a fake input and the expected output please ? To be sure that I understand that data you're dealing with – Paul Oct 02 '21 at 13:01
  • Sure editing the question to add the dummy table – Mohseen Mulla Oct 02 '21 at 13:02
  • There was a small mistake (I hadn't touched SQL in a while) can you look at my edited answer @MohseenMulla ? – Paul Oct 02 '21 at 13:08
  • 1
    Not sure if the parse exception will be fixed though, I don't really know where it is coming from :( – Paul Oct 02 '21 at 13:11