How to select the longest value of a string?

0

HI

I'm writing a query that selects different barcodes. Every barcode has a specific sequence. But in the database the barcode exicsts multiple times. Everytime a sequence is added the barcode is visable in a different row. For example:

Enter image description here

I don't want to use a date filter because sometimes two sequences happen at the same date. Now I've used three select filters but I'm wondering if it's possible to use an other way.

SELECT wc.dn_barcode , ( SELECT da_waarnemingsequence FROM collo_dwh.collo wc2 WHERE wc2.dn_barcode = wc.dn_barcode AND length(sequence) = ( SELECT max(length(sequence) FROM collo_dwh.collo wc3 WHERE wc3.dn_barcode = wc2.dn_barcode ) ) sequence

FROM (database wc)

WHERE wc.da_datum_sortering1 BETWEEN "date_add"('day', -7, current_date) AND current_date AND wc.da_landcode_gea = 'NL' AND wc.sequence LIKE '%A1%B1%' AND wc.sequenceNOT LIKE '%I%'

질문됨 일 년 전191회 조회
1개 답변
0
수락된 답변

Here's an alternative using a window function:

SELECT barcode, sequence, d FROM (
     SELECT barcode, sequence, d, rank() OVER (PARTITION BY barcode
                             ORDER BY length(sequence) desc) AS r
     FROM "db1"."barcodes" 
) as windw
where windw.r = 1

AWS
Don_D
답변함 일 년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠