Hi. do I need to buy this ticket in advance or just when we go. I am hoping to go directly from Copenhagen airport. Thanks
Buy the ticket there, when you know which train you can make. This is two legs by regional trains that don't have advance-purchase discounts. Expect to pay the equivalent of about 200 Swedish kroner (1st and 2nd-class prices are similar at www.sj.se).
I see you've gotten an answer on this specific route. But there's a trick to finding this out for any route. Just look at prices for today or tomorrow (taking the time difference into account - most train websites default to the current local time in their country, not your local time), and then the price for a few months out. If they are the same, no need to buy ahead. If they're not, then you'll want to book as soon as you know your dates (of course, arriving at an airport and planning on getting a specific train is always risky).
Thank you both
Do not buy in advance, and you do not need to use SJ website. At the airport you will see red kiosks with the name Skånetrafiken on them. These are operated by the company that does all of the trains and busses in the southern county of Sweden. You should be able to buy a ticket that includes to Ystad. With that ticket you can get to Malmo from cph airport and then switch trains to Ystad. You only need to get off at the first stop in Sweden, Hyllie station, from there you can get a train to Ystad.