如何将2张桌子连在一起?

时间:2019-01-08 00:37:46

标签: sql database

我正在尝试创建具有2个现有表的联接表。如下所示:

这是第一个表查询,看起来像这样

https://ibb.co/sg2MXKf

alphabets = {"A":1,"B":2,"C":3, "D":4,"E":5,"F":6,"G":7,"H":8,"I":9,"J":1,"K":2,"L":3,"M":4,"N":5,"O":6,"P":7,"Q":8,"R":9,"S":1,
         "T":2,"U":3,"V":4,"W":5, "X":6,"Y":7,"Z":8}

def digit_sum(num):
    return sum( [ int(char) for char in str(num) ] )

def numerology(word):
    total = 0
    for letter in word:
            total += alphabets[letter]
            total = digit_sum(total)

    return total

fan = 'PRINCELY'

def vowels(fan):
    vowels=[]
    if 'I' in fan:
        vowels.append(9)
        fan1=fan[:fan.index('I')]+fan[fan.index('I')+1:]
        consonant = fan1
        if 'E' in fan:
            vowels.append(5)
            fan2=fan1[:fan1.index('E')]+fan1[fan1.index('E')+1:]
            consonant = fan2
            if 'A' in fan:
                vowels.append(1)
                fan3=fan2[:fan2.index('A')]+fan2[fan2.index('A')+1:]
                consonant = fan3
                if 'O' in fan:
                    vowels.append(6)
                    fan4=fan3[:fan3.index('O')]+fan3[fan3.index('O')+1:]
                    consonant = fan4
                    if 'U' in fan:
                        vowels.append(3)
                        fan5=fan4[:fan4.index('U')]+fan4[fan4.index('U')+1:]
                        consonant = fan5
    print(vowels)
    print(consonant)
    print(digit_sum(sum(vowels)))
    cons = numerology(consonant)
    print(cons)

vowels(fan)

#outputs
#[9, 5]
#PRNCLY
#5
#7

这是第二个表查询,看起来像这样

https://ibb.co/z8sRwpT

SELECT
    DATEPART( week, dbo.Income.IncomeDate ) AS [Week Income],
    DATEPART( YEAR, dbo.Income.IncomeDate ) AS [Year],
    SUM ( dbo.Income.CardAmount ) AS [Total Card],
    SUM ( dbo.Income.CashAmount ) AS [Total Cash],
    SUM ( dbo.Income.TipsAmount ) AS [Total Tip],
    SUM ( dbo.Income.SalaryAmount ) AS [Total Salary],
    SUM ( dbo.Income.Adjustment ) AS [Total Adjustment] 
FROM
    dbo.Income 
GROUP BY
    DATEPART( week, dbo.Income.IncomeDate ),
    DATEPART( YEAR, dbo.Income.IncomeDate ) 
ORDER BY
    DATEPART(YEAR, dbo.Income.IncomeDate )

我所期望的就是这样。表1和表2相加,总费用相加。

https://ibb.co/0DbZLYV

1 个答案:

答案 0 :(得分:0)

正如SO人士所说,我们无法访问描述您预期结果的图像。但是,我们了解到您正在寻找JOIN您所显示的两个查询的结果。

操作方法如下:将每个查询转换为子查询,然后将其JOIN一起放在其公共键字段上;在您的用例中,必须为YearWeekORDER BY子句需要移到外部查询。在SELECTWHEREORDER BY子句中,您可以使用定义的别名(此处为A和{{1},从子查询中自由访问所有字段。 }。

示例代码(您将不得不修改B,我无法告诉您您希望它的外观如何):

SELECT