nilsinelabore
nilsinelabore

Reputation: 5095

How to pull SQL data by top 10 in Excel table

I want to pull the data of the top 10 ID based on Excel table Excel_table1 column Value, from an SQL view SQL_view where original_code starts with HC in SQL_view, into another Excel spreadsheet.

Excel_table1 looks something like this:

ID        Value    Code
242432    10000    HC-part
242432    9900     HC-part1
924242    8900     XS
    ...

SQL_view looks like this:

id        Info1   Info2    Country      original_code   Date      Quantity
  
242432    XX      YY       Japan        HC-part         2020-10   150
242491    XX      YY       Japan        HC-part1        2020-10   250
    ...

So Excel_table1 columns ID is the same as SQL_view column id and Excel_table1 columns Code is the same as SQL_view column original_code

Expected output would be of format:

id        Info1   Info2    Country      original_code   Date      Quantity
  
242432    XX      YY       Japan        HC-part         2020-10   150
242491    XX      YY       Japan        HC-part1        2020-10   250
    ...

these rows from SQL_view have been selected because: 1) id 242432 and 242491 are from the top 10 ID in Excel_table1 AND their original_code starts with HC.

Upvotes: 0

Views: 153

Answers (1)

Rakhi Agrawal
Rakhi Agrawal

Reputation: 917

You will need a query which will result the output based on the result of another sub query. So you will be needing:

  1. To get the top 10 IDs from Excel_table1 based on column value where the code starts with 'HC'
select ID from Excel_table1 where Code like 'HC%' ORDER BY Value DESC limit 3
  1. To get the complete data from SQL_View for the IDs returned in above query
select * from SQL_view where id in (select ID from Excel_table1 where Code like 'HC%' ORDER BY Value DESC limit 3)

The query under point 2 is the final one which will output you the expected results.

Hope I got your question right.

Upvotes: 1

Related Questions