Tuesday, October 23, 2012

Google Spreadsheet sorting with multiple columns


( by Brett Walters )


I'm using google spreadsheet to scrape an HTML table and manipulate the data for re-publishing on the web.  Because our initial web form (which produces the HTML table) can't capture the info we need, I have staff manually adding data in a column,  All good up to here.  The new data is maiden name, and with the final output, I'd like it to sort maiden if it exists and then last name.  However, 'ORDER BY' treats the blank cell as a higher sorting cell.

This is what I've tried:

=QUERY(AddBirthName!$A$2:$D$2467;"select A,B,C where D = 1973 ORDER BY (B if B != ''),A";0)

this doesn't work...

Here is a example of the spreadsheet. Looking at the worksheet '1973' I'd like to see the order go 'Harding, Honday, Kimpinger, Korniger' ....



Here is the screenshot of the Sheet1:

Now after inserting the following formula:
=QUERY(Sheet1!$A$2:$D$2467;"select A,B,C where D = 1973 ORDER BY B,A";0)

Here is the screenshot of Sheet Sorting but not in the desired order. 

Now here is the formula to get the required output:

=QUERY(ArrayFormula(IF({{1},{1},{1},{1},{0}};Sheet1!$A$2:$D$2467;if(Sheet1!B2:B="";Sheet1!A2:A;Sheet1!B2:B)));"select Col1,Col2,Col3 where Col4 = 1973 ORDER BY Col5";0)

And here is the screenshot of Sheet sorted in the desired order:

I hope the above solution will help you, and if you need more help then please do comment below on this blog itself, I will try to help you out.

I also take up private or confidential projects:

If this blog post was helpful to you, and if you think you want to help me too and make my this blog survive then please donate here: http://igoogledrive.blogspot.com/2012/09/donate.html 


1 comment:

  1. Hi there,

    First of all thanks for taking your time to write this nice article! Could you please help me how can I search through three spreadsheets?

    I have the following sheets:

    - ListA
    - ListB
    - ListC

    Note: at each sheet I have a column which contains YES or NO.

    How do I sort all my lists (spreadsheets) where myBoleanColumn equals to YES/NO ?

    Alex :)