将来自多个表的数据组合到一个列中

时间:2018-05-23 19:44:33

标签: sql amazon-redshift

表A的当前输出如下:

public MainPage()
{
    var labelTappedGestureRecognizer = new TapGestureRecognizer
    {
        Command = new Command<string>(async labelText => await DisplayAlert("Label Tapped", labelText, "OK"))
    };

    var myLabel = new Label
    {
        Text = "This is my label",
        HorizontalOptions = LayoutOptions.Center,
        VerticalOptions = LayoutOptions.Center
    };

    myLabel.GestureRecognizers.Add(labelTappedGestureRecognizer);
    labelTappedGestureRecognizer.SetBinding(TapGestureRecognizer.CommandParameterProperty, new Binding(nameof(Label.Text), source: myLabel));

    Content = myLabel;
}

id event timestamp registered acct_add funded applied 1 Register 5/18/2018 4:00:00 AM 1 0 0 0 1 AddAccount 5/18/2018 5:00:00 AM 0 1 0 0 1 Funded 5/18/2018 6:00:00 AM 0 0 1 0 2 Register 5/18/2018 7:00:00 AM 1 0 0 0 2 AddAccount 5/18/2018 8:00:00 AM 0 1 0 0 2 Funded 5/18/2018 9:00:00 AM 0 0 1 0 的值应该是:

event

事实证明,直到某个日期,最后一个事件才被解雇。但是,表B有两列:'Register', 'AddAccount', 'Funded', 'Applied'(*) id,可以转换为:

application_date

实际上我的查询是5个以上表格和100多行代码的组合,所以请您建议我如何将两个表格合并为一个视图最好没有联合等的是:

id  application_date       applied 
1   5/18/2018 6:30:00 AM   1         
2   5/18/2018 9:30:00 AM   1     

谢谢!

2 个答案:

答案 0 :(得分:2)

这是对Impaler对表B的答案的调整,其顺序与表A相同,单引号硬文,并将最后的0修改为1,以表明该ID被肯定地应用

select * from (
  select * from table_a
  union all select id, 'Applied', application_date,  0, 0, 0, 1 from table_b
  ) all_rows
order by id, timestamp;

我会这样做是对你的答案的评论,但我还不能发表评论。

答案 1 :(得分:1)

使用union all解决方案看起来非常简单:

select * from (
  select * from table_a
  union all select id, 'Applied', application_date,  0, 0, 0, 1 from table_b
  ) all_rows
order by id, timestamp;