删除SQL中没有的内容

时间:2019-02-22 23:54:58

标签: sql sql-server

我有两个结果集,一个是:

select * from tgCity

大约有850条记录。它具有cityIDcity name

另一个结果集是:

select max(cityID), city 
from tgCity 
group by City

这将返回大约700条记录。原因是在850个记录中有一些城市的150个重复记录。

如何在tgCity表中编写要删除的脚本,该脚本仅删除那150条记录?

3 个答案:

答案 0 :(得分:1)

假设CityId是唯一的,则常见方法是:

delete from tgCity tg
    where tg.cityId < (select tg2.cityId
                       from tgCity tg2
                       where tg2.city = tg.city
                      );

答案 1 :(得分:1)

这是另一个使用WHERE EXISTS条件的经典解决方案:

DELETE t
FROM tgCity t
WHERE EXISTS (
    SELECT 1 FROM tgCity t1 WHERE t1.city = t.city AND t1.cityId < t.cityId
)

这将删除具有相同city的记录,同时保留组中cityId最小的记录。

Demo on DB Fiddle

答案 2 :(得分:0)

此脚本删除重复的城市:

    [Table("BiddingData")]
    public class Bid : ObservableObject
    {

        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [Key, Column(Order = 0)]
        public int Id { get; set; }

        [Column("Section", Order = 1)]
        public int SectionId { get; set; }

        [Column("Table", Order = 2)]
        public int TableNumber { get; set; }

        [Column("RoundNumber", Order = 3)]
        public int RoundNumber { get; set; }

        [Column("Board", Order = 4)]
        public int Board { get; set; }

        [Column("Counter", Order = 5)]
        [Index("IX_Counter", IsUnique = false)]
        public int Counter { get; set; }

        [Column(Order = 6)]
        public string Direction { get; set; }

        [Column("Bid", Order = 7)]
        public string BidText { get; set; }

        [Column(Order = 8)]
        public DateTime DateLog { get; set; }

        [Column(Order = 9)]
        public DateTime TimeLog { get; set; }

        [Column(Order = 10)]
        public bool Erased { get; set; }

        [NotMapped]
        [JsonIgnore]
        public String ElapsedTime { get; set; }


        [Column(Order = 11)]
        [JsonIgnore]
        public String IpAddress { get; set; }

        public virtual Round Round { get; set; }

        [NotMapped]
        [JsonIgnore]
        public String BidValue
        {
            get
            {
                switch (BidText)
                {
                    case "X":
                    case "XX":
                        return BidText;
                    case "P":
                        return "PASS";
                    default:
                        switch (BidText.Substring(1))
                        {
                            case "N":
                                return BidText.Substring(0, 1) + "N";
                            case "C":
                                return BidText.Substring(0, 1) + "♣";
                            case "D":
                                return BidText.Substring(0, 1) + "♦";
                            case "H":
                                return BidText.Substring(0, 1) + "♥";
                            case "S":
                                return BidText.Substring(0, 1) + "♠";
                            default: return "";
                        }

                }
            }
        }

        [NotMapped]
        [JsonIgnore]
        public Brush ForegroundColor
        {
            get
            {
                Color color;

                switch (BidText)
                {
                    case "X":
                    case "XX":
                    case "P":
                        color = Colors.White;
                        break;
                    default:
                        switch (BidText.Substring(1))
                        {
                            case "C":
                                color = Colors.DarkGreen;
                                break;
                            case "D":
                                color = Colors.Yellow;
                                break;
                            case "H":
                                color = Colors.Red;
                                break;
                            case "S":
                                color = Colors.Blue;
                                break;
                            case "N":
                            default:
                                color = Colors.Black;
                                break;
                        }

                        break;

                }

                return new SolidColorBrush(color);
            }
        }

        [NotMapped]
        [JsonIgnore]
        public Brush BackgroundColor
        {
            get
            {
                switch (BidText)
                {
                    case "X":
                        return new SolidColorBrush(Colors.Blue);
                    case "XX":
                        return new SolidColorBrush(Colors.Red);
                    case "P":
                        return new SolidColorBrush(Colors.Green);
                    default:
                        return new SolidColorBrush(Colors.White);

                }
            }
        }

        [NotMapped]
        [JsonIgnore]
        public String TableString
        {
            get
            {
                if (Round?.Section != null)
                {
                    return Round.Section.Letter.Trim() + "-" + TableNumber;
                }

                return TableNumber.ToString();                
            }

        }

        //TODO i18N
        [NotMapped]
        [JsonIgnore]
        public String BoardString
        {
            get
            {
                if (this.Round != null)
                {
                    return "Round #" + this.RoundNumber + " - Board #" + this.Board + " - " + this.Round.NSPair + " VS " + this.Round.EWPair;
                }                
                else
                {
                    return "Board #" + this.Board;
                }
            }
        } 
    }
}