English 中文(简体)
供应商无关SQL,用于跨记录连接字段值
原标题:Vendor agnostic SQL to concatenate field values across records
  • 时间:2010-10-15 05:54:08
  •  标签:
  • sql

我有以下数据库架构:-

Data is ...

Location Table
1. New York
2. London
3. Tokyo
4. Melbourne

OtherNames Table (aka Aliases)

1. NYC
1. New York City
4. Home
3. Foo
3. PewPew

作为SQL,我试图做的是得到以下结果:-

ID、名称、名称+别名

例如。

1 | New York | new york nyc new york city
2 | London | NULL
3 | Tokyo | tokyo foo pewpew
4 | Melbourne | melbourne home

我不知道如何获得最后一栏。

这就像我想要一个SubQuery,COALESCE是OtherName.Name字段,每个Location行?

它与上一个问题我有..但我上一个问的问题没有给我正确的结果(我没有问正确的问题,之前:P)

注意:我想要一个TSQL/非服务器特定的答案。因此,请不要建议<code>GROUP_CONCT()

最佳回答

SQL不适合这种操作(1NF冲突等等),因此SQL中的各种解决方案将是特定于供应商的。如果你想要独立于供应商的东西,那么就使用会消耗普通SQL的东西(而不是生成它),例如报告编写器或3GL应用程序;)

问题回答

如果您使用的是SQL Server 2005以后的版本,我个人喜欢XPATH方法





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签