Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting table in SQL or Access

Code  | FName | Freq
---------------------
L1    | Ted   | 4
L1    | Frank | 
L1    | Ted   | 5
L2    | Ted   | 7
L1    | Jim   | 4
L1    | Ted   | 7
L4    | Ed    | 1
L2    | Ted   | 2
L1    | Frank | 2

Lets say I have a table like that in my database. Would it possible to grab the records in SQL (or grab in SQL then reorganize in another Access query) so that I can get record set that looks like this:

Code | FName | Freq
-------------------------
L1    | Ted   | 4, 5, 7
L1    | Frank | 2
L2    | Ted   | 2, 7
L4    | Ed    | 1
L1    | Jim   | 4

where the different 'Freq' numbers are appended together in a list based off 'Code' AND 'Fname'? Order doesn't matter as long as all the different freqency numbers are in the list. I can assume that for each combination of Code and Fname that there will be distinct, non-repeated frequency numbers (so a combination of 'L1' and 'Ted' will never have a freq value of 4 more than once)

like image 655
user1384831 Avatar asked Jan 25 '26 12:01

user1384831


1 Answers

Then you are in luck!

select code, fname, listagg(cast(freq as varchar(32), ',') within group (order by freq)
from t
group by code, fname

I'm not sure what this does with the blank, however.

like image 159
Gordon Linoff Avatar answered Jan 28 '26 02:01

Gordon Linoff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!