如何过滤 SQL EAV 模型中的行?

发布于 2024-12-26 11:41:05 字数 349 浏览 0 评论 0原文

我得到了一个 EAV SQL 表,其中包含用于显示 GUI 的首选项列表。每个首选项都由表中的一行表示。

我需要过滤这些行,使每个不同的 ColumnName/key 列组合只有一行。如果 ID_UserPersonal 列不为 NULL,则意味着该行代表特定于用户的首选项,该首选项必须优先于默认首选项(当 ID_UserPersonal 为 NULL 时)。在这种情况下,必须过滤掉默认首选项,并为该 ColumnName/key 列组合保留用户定义的首选项。

数据行

I got a EAV SQL table which contains a list of preferences used for the display of a GUI. Each preference is represented by a row in the table.

I need to filter these rows to have only one row per different ColumnName/key column combination. If the ID_UserPersonal column is not NULL, it means that the row represents a user-specific preference which must have precedence over the default ones (when ID_UserPersonal is NULL). In that case, the default preference must be filtered out and the user-defined one must be kept for that ColumnName/key column combination.

Data Rows

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

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

发布评论

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

评论(2

苏佲洛 2025-01-02 11:41:05

您可以通过 ID_Personal 在非 NULL 值后面放置 NULL 来对行进行排名,然后选择排名值为 1 的行,如下所示:

;
WITH ranked AS (
  SELECT
    *,
    rnk = ROW_NUMBER() OVER (
      PARTITION BY ID_Role, Section, GridName, ColumnName, [key]
      ORDER BY CASE WHEN ID_Personal IS NULL THEN 1 ELSE 0 END, DateCreated
    )
  FROM AnEAVTable
)
SELECT *  /* or specify your columns explicitly, which is even better */
FROM ranked
WHERE rnk = 1

请注意 在我的示例中,ROW_NUMBER() 函数的 PARTITION BY 子句,以防我在其中添加太多(或者可能太少)列。

参考文献:

You could rank rows by ID_Personal putting NULLs after non-NULL values, then select the rows where ranking values are 1, something like this:

;
WITH ranked AS (
  SELECT
    *,
    rnk = ROW_NUMBER() OVER (
      PARTITION BY ID_Role, Section, GridName, ColumnName, [key]
      ORDER BY CASE WHEN ID_Personal IS NULL THEN 1 ELSE 0 END, DateCreated
    )
  FROM AnEAVTable
)
SELECT *  /* or specify your columns explicitly, which is even better */
FROM ranked
WHERE rnk = 1

Please pay attention to the PARTITION BY clause of the ROW_NUMBER() function in my example, in case I added too much (or, perhaps, too few) columns there.

References:

左耳近心 2025-01-02 11:41:05

以下是您需要的完整示例:

create table #t(
f1 int null,
f2 int null,
f3 int null)

insert into #t values (1,2,3), (1,2,null), (1,3,null)



--select the not null
select t.* from #t t
where t.f3 is NOT NULL
UNION ALL
--union with the NULL not present in the previously select
select t.* from #t t
inner join (select convert(varchar,f1) + convert(varchar,f2) r
            from #t where f3 is not null) a
            on a.r <> convert(varchar,t.f1) + convert(varchar,t.f2)


drop table #t

您需要连接唯一标识表中行的字段。

Here is a full example of what you need:

create table #t(
f1 int null,
f2 int null,
f3 int null)

insert into #t values (1,2,3), (1,2,null), (1,3,null)



--select the not null
select t.* from #t t
where t.f3 is NOT NULL
UNION ALL
--union with the NULL not present in the previously select
select t.* from #t t
inner join (select convert(varchar,f1) + convert(varchar,f2) r
            from #t where f3 is not null) a
            on a.r <> convert(varchar,t.f1) + convert(varchar,t.f2)


drop table #t

You'll need to make the concatenation of the fields that uniquelly identify a row on your table.

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