Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#16 2020-10-10 18:57:17

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 12,446
Website GitHub

Re: RFC: extracting distinct values

It’ll be MAX(posted) for me. It’s more SQLish and we’re dealing with SQL inside these fields in the same way we can currently use FIELD(column, value1, value2, value3) inside sort. It feels more consistent.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Hire Txp Builders – finely-crafted code, design and Txp

Offline

#17 2020-10-10 18:57:38

gaekwad
Server grease monkey
From: People's Republic of Cornwall
Registered: 2005-11-19
Posts: 4,727
GitHub

Re: RFC: extracting distinct values

etc wrote #326324:

posted=max, max(posted) or something else?...

There’s far more prior art for foo="bar" than foo(bar), so given the A/B choice I’d choose the former.

Edit: actually, Stef’s right – ignore me.

Last edited by gaekwad (2020-10-10 18:58:46)

Offline

#18 2020-10-10 18:58:26

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 12,446
Website GitHub

Re: RFC: extracting distinct values

gaekwad wrote #326326:

There’s far more prior art for foo="bar" than foo(bar), so given the A/B choice I’d choose the former.

Haha! I just chose the opposite. But I get what you mean. Hmmmm. Torn. It’s more attributey but then if we’re not using quotes, it might be confusing for people that are used to using them in regular attributes, to have to omit them here.

Last edited by Bloke (2020-10-10 19:00:36)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Hire Txp Builders – finely-crafted code, design and Txp

Offline

#19 2020-10-10 19:04:08

gaekwad
Server grease monkey
From: People's Republic of Cornwall
Registered: 2005-11-19
Posts: 4,727
GitHub

Re: RFC: extracting distinct values

Bloke wrote #326327:

I just chose the opposite. But I get what you mean.

Tag stuff is fairly well-defined with plenty of foo="bar" formatting, but I totally see your point. I guess it depends on whether you want to introduce something that’s a bit different into tag markup that makes it a more complex.

I would wager most people don’t do this kind of stuff, but the kind who do would be comfortable with either option, given sufficient explanation in the relevant doc(s). As you say, it’s database extraction speak, not HTML-like markup…is there a precedent for tag stuff with foo(bar) constructs already?

Offline

#20 2020-10-10 19:12:26

etc
Developer
Registered: 2010-11-11
Posts: 5,677
Website GitHub

Re: RFC: extracting distinct values

gaekwad wrote #326328:

Tag stuff is fairly well-defined with plenty of foo="bar" formatting…is there a precedent for tag stuff with foo(bar) constructs already?

Be it posted=max or max(posted), it’s not an attribute but a value:

fields="authorid, max(posted), sum(comments_count)"

And while we are there, which other SQL aggregate functions could be useful? Currently we have max, min, sum, avg.

Offline

#21 2020-10-10 19:17:41

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 12,446
Website GitHub

Re: RFC: extracting distinct values

etc wrote #326329:

which other SQL aggregate functions could be useful? Currently we have max, min, sum, avg.

Just count to add from that list, I think. Unless you reckon that’s adequately covered elsewhere. The rest are a bit esoteric and statistics-driven to be of much use, imo.

The only other outliers are:

  • group_concat() but since you have to use groups for that, we’re starting to get into seriously convoluted tag territory.
  • json_* because someone might want to pull out json-formatted content in future. But since we’re only targeting article tags here and don’t store JSON data yet, this might be overkill now.

The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Hire Txp Builders – finely-crafted code, design and Txp

Offline

#22 2020-10-10 19:31:52

etc
Developer
Registered: 2010-11-11
Posts: 5,677
Website GitHub

Re: RFC: extracting distinct values

Bloke wrote #326330:

Just count to add from that list, I think. Unless you reckon that’s adequately covered elsewhere.

count is already available, but not as fields value: max(posted) is a date and can be output via <txp:posted />, but not count(posted).

group_concat() but since you have to use groups for that, we’re starting to get into seriously convoluted tag territory.

We actually already do GROUP BY, so adding group_concat() is not a problem, but I doubt of its utility.

json_* because someone might want to pull out json-formatted content in future. But since we’re only targeting article tags here and don’t store JSON data yet, this might be overkill now.

They look promising, but are available in MySQL 5.7.22+ only, do we require this?

Offline

#23 2021-01-04 17:22:24

etc
Developer
Registered: 2010-11-11
Posts: 5,677
Website GitHub

Re: RFC: extracting distinct values

FWIW, GROUP_CONCAT(field SEPARATOR ',') is available now (4.8.5) as list(field). It’s yet time to change list for concat or join or ..?

Offline

#24 2025-09-23 21:13:05

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 12,446
Website GitHub

Re: RFC: extracting distinct values

Circling back to this feature… I want to build a dropdown list of all values of custom_5, to allow filtering by that field (it’s product “brand”). My understanding:

<article::custom section='<txp:section />' fields="thisid, list(brand)" break="" limit="0">
    <custom::field name="brand" />
</article::custom>

should build me a unique comma-separated list of custom_5 values from articles in that section. But instead, I’m getting all of them, repeated. It doesn’t seem to matter if I include thisid or not.

What am I missing?

EDIT: duh, it’s simpler than I thought. Swear this didn’t work the first time I tried it, but now it does:

<article::custom section='<txp:section />' fields="brand" break="," limit="0">
    <custom::field name="brand" />
</article::custom>

Last edited by Bloke (2025-09-23 21:23:49)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Hire Txp Builders – finely-crafted code, design and Txp

Offline

#25 2025-09-24 08:30:09

etc
Developer
Registered: 2010-11-11
Posts: 5,677
Website GitHub

Re: RFC: extracting distinct values

Bloke wrote #340633:

<article::custom section='<txp:section />' fields="thisid, list(brand)" break="" limit="0">...

should build me a unique comma-separated list of custom_5 values from articles in that section.

The intended use is fields="section, list(brand)", though this will not remove duplicates.

Since GROUP_CONCAT() supports DISTINCT clause, this is easy to add. It makes sense then to sort by brand too. I wouldn’t like to alter list() behaviour, so a new syntax is needed. Should it be unique() or distinct() or something else?

Offline

#26 2025-09-24 08:55:53

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 12,446
Website GitHub

Re: RFC: extracting distinct values

etc wrote #340645:

The intended use is fields="section, list(brand)", though this will not remove duplicates.

Oh. So I need section as well, because I’m filtering by section= in the tag? Maybe that’s where I was going wrong

I wouldn’t like to alter list() behaviour, so a new syntax is needed. Should it be unique() or distinct() or something else?

Either works for me. distinct() is more SQLish.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Hire Txp Builders – finely-crafted code, design and Txp

Offline

#27 2025-09-24 09:17:31

etc
Developer
Registered: 2010-11-11
Posts: 5,677
Website GitHub

Re: RFC: extracting distinct values

Bloke wrote #340646:

Oh. So I need section as well, because I’m filtering by section= in the tag? Maybe that’s where I was going wrong

No, you are right, not in this case.

Offline

#28 2025-09-24 09:25:39

etc
Developer
Registered: 2010-11-11
Posts: 5,677
Website GitHub

Re: RFC: extracting distinct values

Bloke wrote #340646:

Either works for me. distinct() is more SQLish.

If list() sounds like distinct and sorted, we can yet alter it this way, I don’t think it is widely used. Then concat() would replace it for raw concatenations.

Offline

Board footer

Powered by FluxBB