将“总计列”添加到一行中,然后计算而不显示重复项

时间:2019-01-14 19:17:20

标签: sql sql-server tsql sql-server-2012

我现在像下面显示的那样立即显示输出,但是如果有相同的医师姓名和相同的def代码,我需要添加“总账单费用”列。

以下是我得到的结果

    AccountNumber  IDnumber    DischargeDate    OutStandingDays  DefCode     Name            BillTotalCharges   DeficiencyPhysician
    K00098765432    ID00123456  11/29/2018        46               SCO    Sign Consultation    193.41             Ag MD
    K00098765432    ID00123456  11/29/2018        46               SCO    Sign Consultation    35598.4            Ag MD
    K00098765432    ID00123456  11/29/2018        46               SCO    Sign Consultation    193.41             L A MD
    K00098765432    ID00123456  11/29/2018        46               SCO    Sign Consultation    35598.4            L A MD
    K00098765432    ID00123456  11/29/2018        46               SCO    Sign Consultation    193.41             D L H-C PA-C
    K00098765432    ID00123456  11/29/2018        46               SCO    Sign Consultation    35598.4            D L H-C PA-C

但是我需要它如下。

    AccountNumber  IDnumber    DischargeDate    OutStandingDays  DefCode     Name            BillTotalCharges   DeficiencyPhysician
    K00098765432    ID00123456  11/29/2018        46               SCO    Sign Consultation    35791.81           Ag MD
    K00098765432    ID00123456  11/29/2018        46               SCO    Sign Consultation    35791.81           L A MD
    K00098765432    ID00123456  11/29/2018        46               SCO    Sign Consultation    35791.81           D L H-C PA-C

列Billtotalcharges来自表BillAcct_Bills,该表具有名为BillType列的内容,该列显示最终付款和最终付款

2 个答案:

答案 0 :(得分:0)

根据您的数据,使用SUM函数的简单聚合就足够了,例如:

select
    AccountNumber,
    IDnumber,
    DischargeDate,
    OutStandingDays,
    DefCode,
    Name,
    SUM(BillTotalCharges) as Charges,
    DeficiencyPhysician
from
    table1
group by
    AccountNumber,
    IDnumber,
    DischargeDate,
    OutStandingDays,
    DefCode,
    Name,
    DeficiencyPhysician

答案 1 :(得分:0)

也许sum中只有BillTotalCharges中的一个group by,您需要在sum中有很多列才能与 Select AccountNumber, IDnumber, DischargeDate, OutStandingDays, DefCode, Name, sum(BillTotalCharges), DeficiencyPhysician from table group by AccountNumber, IDnumber, DischargeDate, OutStandingDays, DefCode, Name, DeficiencyPhysician 保持同步

# create a list 
db_list = []
# fetch the DB and store them in the list.
# db_list will contains: ['D01', 'D02', 'D03', 'D04', 'D05', 'D06']
for each in User.objects.all():
    db_list.append(each.username)

# new list that needs to be added to DB, also contains some overlaping data from db_list.
second_list = ['XY12','D01','VA02','HM01','D04','HM03','D126']

# just a counter to loop through
cc = 0
for each in second_list:
    # for each item on the second_list we need to check if that value 
    # already exists in the DB, if so update its value with the current time.
    # If not, pass to else and create a new record.
    if second_list.count(db_list[cc]):
        model = User.objects.get(username=each)
        model.last_seen = timezone.now()
        model.save()
    else:
        # this method of initialising might not be the correct way but this works :/
        model = User()
        gg = gg()
        model.username = each
        model.last_seen = timezone.now()

        # this is a foreignkey in the
        # users table which corresponds to GG table. many to one relationship
        model.gg = GG.objects.get(gg='IG07') 

        model.save()
    cc += 1 #increase counter and loop until list exhausts.