【问题标题】:SQL to create data as table when there is no relation in SQLSQL在SQL中没有关系时将数据创建为表
【发布时间】:2021-09-14 14:00:08
【问题描述】:

我有以下表格

表 1 我有费率

表 2 我有日期

表 3 我有数据显示适用于 startend

之间的费率价格

我想用 SQL 查询创建 表 4,其中我有每个

汇率(来自表 1)+ 每个日期(来自表 2)+ 如果该日期存在于开始价格和结束价格之间,否则 0 显示如下表 4

我不知道如何在 SQL 查询中实现它,因为我是 SQL 新手,以及如果表之间没有关系如何链接

表 1

Rate ID
ConfD1 46
ConfD2 47

表 2

Dates
15-09-2018
16-09-2018
17-09-2021
18-09-2021
19-02-2022

表 3

Rate ID startdate enddate price
ConfD1 46 01-01-2021 31-10-2021 111
ConfD1 46 01-11-2021 01-03-2022 222
ConfD2 47 01-01-2021 31-10-2021 333
ConfD2 47 01-11-2021 01-03-2022 444
ConfD3 48 01-01-2021 31-10-2021 555
ConfD3 48 01-11-2021 01-03-2022 666

表 4

Rate date price
ConfD1 15-09-2018 0
ConfD1 16-09-2018 0
ConfD1 17-09-2021 111
ConfD1 18-09-2021 111
ConfD1 19-02-2022 222
ConfD2 15-09-2018 0
ConfD2 16-09-2018 0
ConfD2 17-09-2021 333
ConfD2 18-09-2021 333
ConfD2 19-02-2022 444

【问题讨论】:

  • confD2的id是47,为什么在表2中显示为46??
  • @RobertoHernandez 复制粘贴 :) ,现已更新

标签: sql sql-server


【解决方案1】:

不错的工作共享数据和所需的输出。如果下次您将数据变为可消耗的,那会更容易。像这样。

create table Table1
(
    Rate varchar(10)
    , ID int
)

insert Table1
select 'ConfD1', 46 union all
select 'ConfD2', 47

set dateformat dmy

create table Table2
(
    Dates date
)

insert Table2
select '15-09-2018' union all
select '16-09-2018' union all
select '17-09-2021' union all
select '18-09-2021' union all
select '19-02-2022'

create table Table3
(
    Rate varchar(10)
    , ID int
    , startdate date
    , enddate date
    , price int
)

insert Table3
select 'ConfD1', 46, '01-01-2021', '31-10-2021', 111 union all
select 'ConfD1', 46, '01-11-2021', '01-03-2022', 222 union all
select 'ConfD2', 46, '01-01-2021', '31-10-2021', 333 union all
select 'ConfD2', 46, '01-11-2021', '01-03-2022', 444 union all
select 'ConfD3', 46, '01-01-2021', '31-10-2021', 555 union all
select 'ConfD3', 46, '01-11-2021', '01-03-2022', 666

此查询将返回您要查找的数据。

select t1.Rate
    , t2.Dates
    , price = coalesce(t3.price, 0)
from Table1 t1
cross join Table2 t2
left join Table3 t3 on t3.startdate <= t2.Dates 
                    and t3.enddate >= t2.Dates 
                    and t3.rate = t1.rate

【讨论】:

    【解决方案2】:
    with dates ( coddate ) as 
    (  select convert(datetime , '15/09/2018' , 103) union all
       select convert(datetime , '16/09/2018' , 103) union all
       select convert(datetime , '17/09/2021' , 103) union all
       select convert(datetime , '18/09/2021' , 103) union all
       select convert(datetime , '19/02/2021' , 103) 
    ) ,
    rates  ( rate, id ) as
    ( 
    select 'ConfD1' , 46 union all 
    select 'ConfD2' , 47  
    ), 
    rateprices ( Rate , Id, Startdate , EndDate, price ) 
    as
    (  select 'ConfD1' , 46 , convert(datetime , '01/01/2021' , 103) , convert(datetime , '31/10/2021' , 103 ), 111 union all
       select 'ConfD1' , 46 , convert(datetime , '01/01/2021' , 103) , convert(datetime , '01/03/2022' , 103 ), 222 union all
       select 'ConfD2' , 47 , convert(datetime , '01/01/2021' , 103) , convert(datetime , '31/10/2021' , 103 ), 333 union all
       select 'ConfD2' , 47 , convert(datetime , '01/01/2021' , 103) , convert(datetime , '01/03/2022' , 103 ), 444 union all
       select 'ConfD3' , 48 , convert(datetime , '01/01/2021' , 103) , convert(datetime , '31/10/2021' , 103 ), 555 union all 
       select 'ConfD3' , 48 , convert(datetime , '01/01/2021' , 103) , convert(datetime , '01/03/2022' , 103 ), 666 
    ), 
    rates_and_dates ( rate , id , coddate ) 
    as 
    ( select a.rate , a.id , b.coddate from rates a cross join dates b
    ), 
    result ( rate, id, coddate, price )
    as
    (
    select a.rate , a.id , a.coddate , price = coalesce(b.price, 0)
    from rates_and_dates a inner join rates c 
      on a.rate=c.rate  
    left join rateprices b 
      on a.rate=b.rate and b.startdate <= a.coddate and b.enddate >= a.coddate 
    ) 
    select rate, coddate, price  from result 
    order by 2, 1
    

    db<>fiddle

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-27
      • 2013-01-19
      • 1970-01-01
      • 1970-01-01
      • 2013-11-19
      • 1970-01-01
      相关资源
      最近更新 更多