如何像这样在 SQL 中加入两个 table?

How to join two table in SQL like this?

有两个 table,分别是 master 和 detail table。我在下面的图片中展示了它们。第一个是 master,另一个是 detail table。 Master table 存储费率的限额,detail table 存储费率和日期。我使用 tables 来计算比率。例如,1 天的 0.00 和 1000.00 之间的金额比率为 1.

我想和他们一起创建一个结果 table 以下列格式显示比率列表:

列名是硬编码的,请忽略它们。

如何编写SQL加入脚本?

我使用 MSSQL Server 2014。

你的问题有点不清楚,但看起来你想做一个枢轴转换。试试这个:

select 
    number_of_day, [0-1000], [1000-5000], [5000-15000] 
from 
(
    select number_of_day, concat(min_amount,'-', max_amount) range, rate 
    from master join detail on detail.repo_rate_master_id = master.id
) x 
pivot 
(
    max(rate) for range in ([0-1000], [1000-5000], [5000-15000])
) p 

Sample SQL Fiddle