0

Sort / Order by based on total count

That is even hard to explain. So I do use a dynamic choice box do suggest possible values based on already existing values from that column. I can sort them alphabetically but I would love it if, I could sort them based on the total count. So that my box suggests frequently used values first. I tried a lot like order by or others.
 

let lieferantName := record('ProjektStückliste',number('Lieferant Auswahl')).Lieferant;
let nennweite := record('ProjektStückliste',number('Nennweite Auswahl')).Nennweite;
let kundenName := Projekt.Kundenname;
let stuecke := (select 'ProjektStückliste'
        where Projekt.Kundenname = kundenName and if lieferantName = null then
                    true
                else
                    Lieferant = lieferantName
                end and if nennweite = null then
                true
            else
                Nennweite = nennweite
            end);
let kurzspezifikationen := sort(stuecke.Kurzspezifikation);
for kurzspezifikation in kurzspezifikationen do
    first(stuecke[Kurzspezifikation = kurzspezifikation])
end

 

Copy

 

So yeah here instead of the alphabetical sort, I would like to sort based on total count.

5 replies

null
    • Fred
    • 1 yr ago
    • Reported - view

    You can try:

    ....
    let x := for kurzspezifikation in kurzspezifikationen do
                first(stuecke[Kurzspezifikation = kurzspezifikation])
             end
    
    x order by Kurzspezifikation
    

    I’m just modifying the last part since that is the part of the code that is doing the final creation of the record array.

    Or you can try:

    let lieferantName := record('ProjektStückliste',number('Lieferant Auswahl')).Lieferant;
    let nennweite := record('ProjektStückliste',number('Nennweite Auswahl')).Nennweite;
    let kundenName := Projekt.Kundenname;
    let stuecke := (select 'ProjektStückliste'
            where Projekt.Kundenname = kundenName and if lieferantName = null then
                        true
                    else
                        Lieferant = lieferantName
                    end and if nennweite = null then
                    true
                else
                    Nennweite = nennweite
                end);
    stuecke order by Kurzspezifikation
    
      • Cloud DevOps Fullstack Engineer
      • Martin_Mueller
      • 1 yr ago
      • Reported - view

      Fred Thanks a lot but I would need the total amount of Kurzspezifikation somehow ^^. I think the easiest is to just have a formula in the table to always calculate the count of that specific Kurzspezifikation. 

    • Cloud DevOps Fullstack Engineer
    • Martin_Mueller
    • 1 yr ago
    • Reported - view

    I managed to solve it with quite complex code. If you see any potential simplification or have a better idea please let me know :).

    let lieferantName := record('ProjektStückliste',number('Lieferant Auswahl')).Lieferant;
    let nennweite := record('ProjektStückliste',number('Nennweite Auswahl')).Nennweite;
    let kundenName := Projekt.Kundenname;
    let thisRecord := this;
    let stuecke := (select 'ProjektStückliste'
            where Projekt.Kundenname = kundenName and if lieferantName = null then
                            true
                        else
                            Lieferant = lieferantName
                        end and if nennweite = null then
                        true
                    else
                        Nennweite = nennweite
                    end and thisRecord != Id);
    let items := unique(stuecke.Kurzspezifikation);
    let orderedArr := [];
    for i from 0 to length(items) do
        let maxArr := [];
        let max := "";
        let maxCount := 0;
        for item in items do
            let tmpCount := count(stuecke[Kurzspezifikation = item]);
            if tmpCount > maxCount then
                maxArr := [item];
                max := item;
                maxCount := tmpCount
            end
        end;
        orderedArr := array(orderedArr, maxArr);
        items := items[!= max]
    end;
    for item in orderedArr do
        first(stuecke[Kurzspezifikation = item])
    end
    

    with thisRecord != Id, I want to exclude the current record for the suggestion algorithms.

    • Alain_Fontaine
    • 1 yr ago
    • Reported - view

    You could perhaps try something like:

    let lieferantName := record('ProjektStückliste',number('Lieferant Auswahl')).Lieferant;
    let nennweite := record('ProjektStückliste',number('Nennweite Auswahl')).Nennweite;
    let kundenName := Projekt.Kundenname;
    let thisRecord := this;
    let stuecke := (select 'ProjektStückliste'
            where Projekt.Kundenname = kundenName and (lieferantName = null or Lieferant = lieferantName)
                        and (nennweite = null or Nennweite = nennweite) and thisRecord != Id);
    for item in unique(stuecke.Kurzspezifikation) do
        first(stuecke[Kurzspezifikation = item])
    end order by (
        let item := Kurzspezifikation;
        -count(stuecke[Kurzspezifikation = item])
    )
    

    You did not publish a model of your database, so I could not test this proposal in context.

      • Cloud DevOps Fullstack Engineer
      • Martin_Mueller
      • 1 yr ago
      • Reported - view

      Alain Fontaine Wow works like a charm. Would never have guess that you can do an order by at the end like that. As well the other improvements are super nice as well. Thank you so much.

Content aside

  • Status Answered
  • 1 yr agoLast active
  • 5Replies
  • 130Views
  • 4 Following