Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8355969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:01:58+00:00 2026-06-09T10:01:58+00:00

I am new in this forum and need some help! My code is not

  • 0

I am new in this forum and need some help!

My code is not giving the correct result. The problem is that I am not getting the max value of t140.BEL_GRLAG_AP (that is what I try to do in line num. 16). The sql still gives two or more lines per person and I only want one line per person, the one with max(t140.BEL_GRLAG_AP).

Can any of you tell me what I am missing ?
Any help is apprecieted!

select distinc tort128.NUM_AVTALE_ID as AvtaleID
, tort009.IDE_ARBGIV_NR as Orgnr
, tort134.NVN_ARBGIV as Arbeidsgiver
, mid(convert(varchar(8),tort127.DAT_KUNDE_FOEDT_NUM),7,2) + 
mid(convert(varchar(8),tort127.DAT_KUNDE_FOEDT_NUM),5,2) +
mid(convert(varchar(8),tort127.DAT_KUNDE_FOEDT_NUM),1,4) + 
RIGHT('00000' + TRIM(convert(CHAR(5),tort127.IDE_KUNDE_PRSNR)),5) as Fødselsnummer
, tort001.NVN_KUNDE_FOR + ' ' + tort001.NVN_KUNDE_ETTER as Navn
, tort140.NUM_ALDERSGRENSE as Aldersgrense

from tort128

join tort127 on tort128.IDE_SEKV_TORT127 = tort127.IDE_SEKV_TORT127
join tort001 on tort127.DAT_KUNDE_FOEDT_NUM = tort001.DAT_KUNDE_FOEDT_NUM and  tort127.IDE_KUNDE_PRSNR=tort001.IDE_KUNDE_PRSNR
join tort009 on tort127.DAT_KUNDE_FOEDT_NUM = tort009.DAT_KUNDE_FOEDT_NUM and   tort127.IDE_KUNDE_PRSNR=tort009.IDE_KUNDE_PRSNR
join tort134 on tort009.IDE_ARBGIV_NR = tort134.IDE_ARBGIV_NR
join tort138 on tort128.IDE_SEKV_TORT128 = tort138.IDE_SEKV_TORT128
left join tort140 on tort138.IDE_SEKV_TORT138 = tort140.IDE_SEKV_TORT138 
and tort140.BEL_GRLAG_AP=(select max(t140.BEL_GRLAG_AP) from tort140 t140
where 1 = 1
  and t140.IDE_SEKV_TORT138 = tort138.IDE_SEKV_TORT138)
and tort140.BEL_LOENN_AAR = (select max(t140_2.BEL_LOENN_AAR) from tort140 t140_2
where 1 = 1
  and t140_2.IDE_SEKV_TORT138 = tort138.IDE_SEKV_TORT138)

where

tort128.NUM_AVTALE_ID = 200854
and tort128.DAT_GYLDIG_FOM <= 20120101
and (tort128.DAT_GYLDIG_TOM >= 20120101
or tort128.DAT_GYLDIG_TOM is null)
and tort128.DAT_HISTORISK is null
and tort128.TYP_STATUS! = 'kns'
and tort127.DAT_KUNDE_FOEDT_NUM >= 19460101
and tort127.DAT_KUNDE_FOEDT_NUM <= 19551234
and tort127.DAT_TERMINERT is null
and tort127.DAT_REGISTRERT <= 20120101
and tort009.DAT_SLUTT is null
and tort134.DAT_HISTORISK is null
and tort138.DAT_AKSJON=(select max(p.DAT_AKSJON) from tort138 p
where 1 = 1
and p.IDE_SEKV_TORT128=tort128.IDE_SEKV_TORT128)

order by

tort127.DAT_KUNDE_FOEDT_NUM
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T10:02:01+00:00Added an answer on June 9, 2026 at 10:02 am

    If you are joining on different tables then you can do something like the following:

    SELECT t1.ID, t2.id, t2.val
    FROM table1 t1
    INNER JOIN 
    (
        SELECT Max(value) val, id
        FROM table2
        GROUP BY id
    ) t2
        ON t1.id = t2.id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to this forum, but i came here because i need some help
ASP.NET 4.0 Need some help with this vexing HTTP POST problem - I have
How can I ensure that this does NOT open in a new window?? onclick=ob=this.form.table;window.open(ob.options[ob.selectedIndex].value)/>
I currently have this useful code that I found elsewhere on StackOverflow: form.DrawToBitmap(bmp, new
I have inherited some code that is not working as I think it should:
I need some help to get the ContentType of a file that is uploaded
I am relatively new to C and I need some help with methods dealing
I am fairly new to web design and need some help coding a form.
I'm new to this forum, but I've seen it a few times while trying
So I have this JS form that creates a new record ( MySQL ,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.