扁平化 SQL Server 中的记录集?

发布于 2024-07-08 07:23:50 字数 1242 浏览 4 评论 0原文

假设您获得如下所示的记录集:

| ID  | Foo  | Bar  | Red  |
|-----|------|------|------|
| 1   | 100  | NULL | NULL |
| 1   | NULL | 200  | NULL |
| 1   | NULL | NULL | 300  |
| 2   | 400  | NULL | NULL |
| ... | ...  | ...  | ...  | -- etc.

您想要:

| ID  | Foo | Bar | Red |
|-----|-----|-----|-----|
| 1   | 100 | 200 | 300 |
| 2   | 400 | ... | ... |
| ... | ... | ... | ... | -- etc.

您可以使用类似的内容:

SELECT
  ID,
  MAX(Foo) AS Foo,
  MAX(Bar) AS Bar,
  MAX(Red) AS Red
FROM foobarred
GROUP BY ID

现在,当 Foo、Bar 和 Red 都是 VARCHAR 时,您如何完成类似的任务?

| ID  | Foo      | Bar     | Red     |
|-----|----------|---------|---------|
| 1   | 'Text1'  | NULL    | NULL    |
| 1   | NULL     | 'Text2' | NULL    |
| 1   | NULL     | NULL    | 'Text3' |
| 2   | 'Test4'  | NULL    | NULL    |
| ... | ...      | ...     | ...     | -- etc.

致:

| ID  | Foo      | Bar     | Red     |
|-----|----------|---------|---------|
| 1   | 'Text1'  | 'Text2' | 'Text3' |
| 2   | 'Text4'  | ...     | ...     |
| ... | ...      | ...     | ...     | -- etc.

目前主要使用 SQL Server 2000; 但可以访问 2005 年的服务器。

Say you get a recordset like the following:

| ID  | Foo  | Bar  | Red  |
|-----|------|------|------|
| 1   | 100  | NULL | NULL |
| 1   | NULL | 200  | NULL |
| 1   | NULL | NULL | 300  |
| 2   | 400  | NULL | NULL |
| ... | ...  | ...  | ...  | -- etc.

And you want:

| ID  | Foo | Bar | Red |
|-----|-----|-----|-----|
| 1   | 100 | 200 | 300 |
| 2   | 400 | ... | ... |
| ... | ... | ... | ... | -- etc.

You could use something like:

SELECT
  ID,
  MAX(Foo) AS Foo,
  MAX(Bar) AS Bar,
  MAX(Red) AS Red
FROM foobarred
GROUP BY ID

Now, how might you accomplish similar when Foo, Bar, and Red are VARCHAR?

| ID  | Foo      | Bar     | Red     |
|-----|----------|---------|---------|
| 1   | 'Text1'  | NULL    | NULL    |
| 1   | NULL     | 'Text2' | NULL    |
| 1   | NULL     | NULL    | 'Text3' |
| 2   | 'Test4'  | NULL    | NULL    |
| ... | ...      | ...     | ...     | -- etc.

To:

| ID  | Foo      | Bar     | Red     |
|-----|----------|---------|---------|
| 1   | 'Text1'  | 'Text2' | 'Text3' |
| 2   | 'Text4'  | ...     | ...     |
| ... | ...      | ...     | ...     | -- etc.

Currently working primarily with SQL Server 2000; but have access to 2005 servers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

虐人心 2024-07-15 07:23:50

上面的查询适用于 VARCHAR 字段,就像适用于 INT 字段一样。 不过,查询的问题是,如果有两行具有相同的 ID,并且这两行的“Foo”列中都有某些内容,则只会显示具有最高值的行(对于 INT 和 VARCHAR) 。

The query you had above works just fine for VARCHAR fields as it did for INT fields. The problem with your query though is that if you have two rows with the same ID, and both of those rows had something in the "Foo" column, then only the one with the highest value (both for INT and VARCHAR) will be displayed.

空城仅有旧梦在 2024-07-15 07:23:50

我目前无法访问 SQL2K 框,但 select max(column) 将在 2005 年对 nvarchars 起作用。唯一的问题是,如果原始表中每个 id 的每列下都有多个文本值...

CREATE TABLE Flatten (
    id int not null,
    foo Nvarchar(10) null,
    bar Nvarchar(10) null,
    red Nvarchar(10) null)

INSERT INTO Flatten (ID, foo, bar, red) VALUES (1, 'Text1', null, null)
INSERT INTO Flatten (ID, foo, bar, red) VALUES (1, null, 'Text2', null)
INSERT INTO Flatten (ID, foo, bar, red) VALUES (1, null, null, 'Text3')
INSERT INTO Flatten (ID, foo, bar, red) VALUES (2, 'Text4', null, null)



SELECT 
    ID, 
    max(foo),
    max(bar),
    max(red)
FROM
Flatten
GROUP BY ID

回报

ID          Foo        Bar        Red
----------- ---------- ---------- ----------
1           Text1      Text2      Text3
2           Text4      NULL       NULL

I don't have access to a SQL2K box at the minute but select max(column) will work on nvarchars in 2005. The only problem will be if you have multiple text values under each column for each id in your original table...

CREATE TABLE Flatten (
    id int not null,
    foo Nvarchar(10) null,
    bar Nvarchar(10) null,
    red Nvarchar(10) null)

INSERT INTO Flatten (ID, foo, bar, red) VALUES (1, 'Text1', null, null)
INSERT INTO Flatten (ID, foo, bar, red) VALUES (1, null, 'Text2', null)
INSERT INTO Flatten (ID, foo, bar, red) VALUES (1, null, null, 'Text3')
INSERT INTO Flatten (ID, foo, bar, red) VALUES (2, 'Text4', null, null)



SELECT 
    ID, 
    max(foo),
    max(bar),
    max(red)
FROM
Flatten
GROUP BY ID

returns

ID          Foo        Bar        Red
----------- ---------- ---------- ----------
1           Text1      Text2      Text3
2           Text4      NULL       NULL
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文