One way would be Karl X Gustav succeeding in storming Copenhagen and somehow convince the Emperor, the Poles, the Dutch and probably everyone else in Europe, that it'd be a much better idea if he ruled both sides of the Øresund or alternatively fight them off - which he failed to do miserably OTL.
Alternatively have the unionist revival of the 1800s catch greater support alongside increased agricultural development (happened in Denmark after 1864, but granted - as a direct effect of the defeat in the 2nd Dano-German War) and have the three kingdoms unite peacefully.
Alternatively have the unionist revival of the 1800s catch greater support alongside increased agricultural development (happened in Denmark after 1864, but granted - as a direct effect of the defeat in the 2nd Dano-German War) and have the three kingdoms unite peacefully.