Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL两条记录,应该是一条_Sql_Join - Fatal编程技术网

SQL两条记录,应该是一条

SQL两条记录,应该是一条,sql,join,Sql,Join,我一直在寻找这个问题的答案,但仍在努力 Location Cashier TXN Date Product Date Time Ref Toronto Z 15-01-04 15-01-04 090501 Transaction was a very Toronto Z 15-01-04 15-01-04 090501 intresting one NewYork X

我一直在寻找这个问题的答案,但仍在努力

Location    Cashier TXN Date    Product Date    Time    Ref
Toronto     Z       15-01-04    15-01-04        090501  Transaction was a very
Toronto     Z       15-01-04    15-01-04        090501  intresting one
NewYork     X       15-01-04    15-01-04        123035  Transaction completed
London      Z       15-02-04    15-01-04        100612  This transaction had complications
London      Z       15-02-04    15-01-04        100612  in it. We need to follow up
Rochest     Y       15-01-04    15-01-04        153045  This transaction was a fun one to
Rochest     Y       15-01-04    15-01-04        153045  process
Vanc        L       15-01-04    15-01-04        174535  Something intresting
我希望具有相同位置、出纳、日期和时间的记录只出现一次,两条适用的参考线在一个框中。。。e、 g

Toronto     Z       15-01-04    15-01-04        090501  Transaction was a very intresting one
我知道这是相当基本的,但在我的SQL学习过程中,任何帮助都将不胜感激


谢谢

如果您使用的是oracle,您可以使用Listag

SELECT location, cashier, txn_date, product_date, time       LISTAGG(ref, '; ') WITHIN GROUP (ORDER BY ref) as "ref_list"
  FROM mytable;
如果您的数据如示例中所列,请使用此查询:

此查询针对MySQL完成

  select distinct Location,
                  Cashier,
                  time,
                  concat((select ref
                           from test
                          where Location = 'Toronto' limit 1),
                         (select ref
                            from test
                           where Location = 'Toronto' limit 1 offset 1))
    from test;
输出应如下所示:

为了三个价值观

您可以使用STUFF和Group BY进行组合。假设您使用的是SQL Server

Select location,Cashier,[Time],
  stuff((SELECT distinct ', ' + cast(ref as varchar(500))
           FROM t t2
           where t2.location = t1.location
           FOR XML PATH('')),1,1,'') 
Grom t t1
Group By location, Cashier,[Time]

您使用的是哪种数据库管理系统?博士后?神谕您如何知道具有相同位置/出纳/日期的行应按何种顺序连接?关系表中的行没有顺序。根据您向我们展示的样本数据,无法检测哪一行是第一行,哪一行是第二行数据存储在哪个数据库中?到目前为止,您尝试了什么?听起来您需要一个组concat,因为您希望所有的Ref都合并在一起。结果是有三条记录需要合并?您只需添加一个新的concat,偏移量为2。这对这个特定实例有效,但由于查询中有这么多硬编码,因此它不是很动态的。哦,是的,一点也不!但对他来说,这是一个开始——我不认为stack应该是获得完整解决方案的地方。这个链接展示了如何使用mysql、sqlserver和oracle来实现这一点,这真是太棒了!喜欢示例SQL FIDLE选项!如果我有最多5条单独的评论记录,那么还有一条后续跟进,会发生什么?我需要额外的t值吗?
select distinct t.Location,
                  t.Cashier,
                  t.time,
                  concat((select ref
                           from test
                          where Location = 'Toronto' limit 1),
                         (select ref
                            from test
                           where Location = 'Toronto' limit 1 offset 1),
                           (select ref
                            from test
                           where Location = 'Toronto' limit 1 offset 2))
   from test t where t.Location = 'Toronto'; 
Select location,Cashier,[Time],
  stuff((SELECT distinct ', ' + cast(ref as varchar(500))
           FROM t t2
           where t2.location = t1.location
           FOR XML PATH('')),1,1,'') 
Grom t t1
Group By location, Cashier,[Time]