Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags

Query language question

A topic by CodeMonki created May 04, 2024 Views: 96 Replies: 1
Viewing posts 1 to 2

I have the following table:

people.name:"Alice","Sam","Thomas","Sara","Walter"
people.age:25,28,40,34,43
people.job:"Developer","Sales","Developer","Developer","Accounting"
people:table people

and I have the following query:

p:select job orderby job asc by job from people

each x in p
  print[x]
end

This produces:

DeveloperDeveloperDeveloperSalesAccounting

Can I impose something like SQL’s SELECT DISTINCT keyword on the query?

Developer (4 edits)

To "collapse" result rows within each group, perform an aggregation (any expression that produces a single value instead of a list) when you select result columns. For example,

 select job:first job by job from people
+--------------+
| job          |
+--------------+
| "Developer"  |
| "Sales"      |
| "Accounting" |
+--------------+

Note that if you're producing multiple result columns, you need to aggregate every column in some way to collapse the group. Otherwise, the aggregations will be "spread" to match the length of the other columns:

 select job:first job experience:sum age by job from people
+--------------+------------+
| job          | experience |
+--------------+------------+
| "Developer"  | 99         |
| "Sales"      | 28         |
| "Accounting" | 43         |
+--------------+------------+
 select job:first job experience:age by job from people
+--------------+------------+
| job          | experience |
+--------------+------------+
| "Developer"  | 25         |
| "Developer"  | 40         |
| "Developer"  | 34         |
| "Sales"      | 28         |
| "Accounting" | 43         |
+--------------+------------+

And if all you want is a single list of results, you can use "extract":

 extract first job by job from people
("Developer","Sales","Accounting"

To order results by the grouping column, you probably want to order values _before_ grouping; otherwise you're ordering _within_ groups, which appear in the result in order of their original appearance. Clauses execute right-to-left, so:

 select job:first job by job orderby job asc from people
+--------------+
| job          |
+--------------+
| "Accounting" |
| "Developer"  |
| "Sales"      |
+--------------+