【问题标题】:Combine data from multiple tables with specific data statements将来自多个表的数据与特定的数据语句结合起来
【发布时间】:2013-12-14 19:41:03
【问题描述】:

在使用 Teradata 时需要一些帮助我需要提取一个连接多个表的报表...这是基本概念:

Select Table1.ID, Table2.Arrival_date, Table2.Name
From Table1
     Inner Join Table1.ID on Table2.ID
Where table2.arrival_date between '10/01/2013' and '10/02/2013'

然后是另一个表,其中包含以下信息

ID     Item     Cost     Date
1      Flour    1.99     10/02/2013
2      cheese   3.99     10/01/2013
3      Flour    1.99     8/16/2013

我想把这个添加到上面的表格中

Select Itemtable.ID, Itemtable.item, itemtable.cost, itemtable.date
from itemtable
where itemtable.date between '10/01/2013' and 10/02/2013
and itemtable.item like '%flour%'

Itemtable.ID = Table1.ID - 但是当我加入时,我只会得到那些购买面粉的客户 - 我希望在该日期范围内看到所有客户,如果他们购买面粉想要它 - 我的决赛桌会看起来像这样:

ID    Arrival Date    Name     ID     Item     Cost     Date
1     10/01/2013       Dan
2     10/01/2013       Mike
3     10/01/2013       Nancy
1     10/02/2013       Dan      1      Flour   1.99     10/02/2013
5     10/02/2013       Mary   

任何帮助将不胜感激!

【问题讨论】:

  • 请告诉我们您的表格名称及其关联。提前致谢!

标签: sql join teradata database-table


【解决方案1】:

粗略地输入(我没有实际的表定义,所以我不得不即时制作),但基本上你想先过滤销售,然后执行来自客户的左连接;类似...

select Table2.ID, Table2.Arrival_dime, Table2.Name
from Table2
  left join (
    select Itemtable.ID, Itemtable.item, Itemtable.cost, ItemTable.date
    where where itemtable.date between '10/01/2013' and 10/02/2013
    and itemtable.item like '%flour%') itemTable on Table2.ID = itemTable.ID
where table2.arrival_date between '10/01/2013' and '10/02/2013'

【讨论】:

    猜你喜欢
    • 2015-06-18
    • 2021-08-21
    • 1970-01-01
    • 2014-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-21
    • 1970-01-01
    相关资源
    最近更新 更多