(SQL)选择涉及连接的查询,以添加详细程度

时间:2017-01-26 16:22:32

标签: sql sql-server

我熟悉基本联接,但我只使用它们来增加列,而不是添加一定程度的细节。

我们说我有表 TableA TableB (见下图)。有以下内容:

' 关键'代表一个人

' 年龄'这个人的年龄

' 所获'他们赚了多少钱

' 铲斗'出售的桶类型

所以在 TableA 中,它显示了一个人在特定年龄段赚了多少钱。 TableB 显示了他们通过销售不同类型的水桶所赚取的资金的百分比 - 对于每个人来说,' 分裂'所有桶都增加了100个。

鉴于这两个表格,我如何查询每个人在不同年龄和桶组合中的制作量?

感谢。

修改

没关系,我明白了。对于那些好奇的人:

SELECT 
    a.Masterkey, 
    a.Age, 
    b.Bucket, 
    a.Earned * b.Split/100 AS Earned
FROM 
    TableA a INNER JOIN TableB b
ON
    a.Masterkey = b.Masterkey
ORDER BY 1,2,3

2 个答案:

答案 0 :(得分:0)

您可以在var response = new Employee() // Instantiates Employee to ensure correct properties used. { Id = 100, // Has PascalCase. Department = new DepartmentModel() { Id = 200, DepartmentName = "Abc", Locations = Employee.Department.Locations .Select(lo => new Location { Area = lo.Area, StreetNo = lo.StreetNo, Nearby = lo.Nearby } ).ToList() } }; 条款中进行数学运算。只需像往常一样加入表格,SELECT或类似的东西。

答案 1 :(得分:0)

这应该得到你所需要的:

declare @e table (pk nvarchar(2),Age int,Earned decimal(10,2));
declare @s table (pk nvarchar(2),Bucket nvarchar(1),Split decimal(10,2));

insert into @e values ('a1',6,10),('a1',7,20),('b1',14,50),('b1',15,100);
insert into @s values ('a1','A',25),('a1','B',75),('b1','A',50),('b1','B',50);

select e.pk
        ,e.Age
        ,s.Bucket
        ,e.Earned * (s.Split/100) as Earned
from @e e
    left join @s s
        on e.pk = s.pk;

输出:

+----+-----+--------+-------------+
| pk | Age | Bucket |   Earned    |
+----+-----+--------+-------------+
| a1 |   6 | A      |  2.50000000 |
| a1 |   6 | B      |  7.50000000 |
| a1 |   7 | A      |  5.00000000 |
| a1 |   7 | B      | 15.00000000 |
| b1 |  14 | A      | 25.00000000 |
| b1 |  14 | B      | 25.00000000 |
| b1 |  15 | A      | 50.00000000 |
| b1 |  15 | B      | 50.00000000 |
+----+-----+--------+-------------+