Quantcast
Channel: Hot Weekly Questions - Web Applications Stack Exchange
Viewing all articles
Browse latest Browse all 9782

Query that combines the contents of 3 columns, but also filters by a date in a 4th column

$
0
0

This is an example of my data that is located on a sheet called Data:

DateFruit 1Fruit 2Fruit 3
1/1/2020AppleMangoApple
2/1/2021CherryBananaPear
2/1/2021Orange
1/1/2022AppleCherryOrange
2/1/2022
2/1/2022PearBanana
21/1/2022CherryBanana

Desired output:

Filtered to only show data between 1/1/2022 and 31/12/2022, sorted by the Count. The start and end dates are located in cells C2 and E2

SuggestionsCount
Banana2
Cherry2
Apple1
Orange1
Pear1

I have achieved what I want in 2 steps:

First is a query that filters the data by dates specified and removes any rows that have no data in the 'Fruit 1' column. This formula is located in Cell D5.

=QUERY (Data!A:D,"SELECT A, B, C, D WHERE A > date '"&TEXT(C2,"yyyy-mm-dd")&"' and J <= date '"&TEXT(E2,"yyyy-mm-dd")&"' AND B<>''",1)
D5E5F5G5
1/1/2022AppleCherryOrange
2/1/2022PearBanana
21/1/2022CherryBanana

then running a second query on those results that combines the 'Fruit 1', 'Fruit 2' and 'Fruit 3' columns and returns the data sorted by count as shown in my desired output above.

=QUERY({E5:E;F5:F;G5:G}, "select Col1, count(Col1) where Col1<>'' group by Col1 order by count(Col1) desc label (Col1) 'Sorted List', count(Col1) 'Count'",1)

What I would really like to do is to do this in one query, but I can't find information on how to run a second query on the data that is generated by the first query... or how to approach it.

I hope this makes sense!


Viewing all articles
Browse latest Browse all 9782

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>