对更多表进行 UNION 时创建唯一 ID 的建议

发布于 2024-12-12 18:56:03 字数 847 浏览 0 评论 0原文

我有一个这样的视图

CREATE VIEW PEOPLE

AS

SELECT CustomerId, CustomerName FROM Customers
UNION ALL
SELECT EmployeeId, EmployeeName FROM Employees
UNION ALL
SELECT FriendId, FriendName From Friends

,现在我需要为该视图添加唯一的 ID,因为我当然可以有一个 CustomerId = 15 和一个 EmployeeID = 15

所以技巧我正在做的是以下

SELECT
  CAST('1' + CAST(CustomerId AS VARCHAR(30)) AS INT) as UniqueCustomerId,
 CustomerId, CustomerName FROM Customers
UNION ALL
SELECT
  CAST('2' + CAST(EmployeeId AS VARCHAR(30)) AS INT) as UniqueEmployeeId,
 EmployeeId, EmployeeName FROM Employees
UNION ALL
SELECT
  CAST('3' + CAST(FriendId AS VARCHAR(30)) AS INT) as UniqueFriendId, 
FriendId, FriendName From Friends

无论如何,由于我有很多记录,所以转换为 varchar(30) 并返回 int 是一项开销。

你能建议一个更好的方法吗?

I have a view made like this

CREATE VIEW PEOPLE

AS

SELECT CustomerId, CustomerName FROM Customers
UNION ALL
SELECT EmployeeId, EmployeeName FROM Employees
UNION ALL
SELECT FriendId, FriendName From Friends

now I need to add unique ID for the view, because of course i can have a CustomerId = 15 and an EmployeeID = 15

So the trick I am doing is the following

SELECT
  CAST('1' + CAST(CustomerId AS VARCHAR(30)) AS INT) as UniqueCustomerId,
 CustomerId, CustomerName FROM Customers
UNION ALL
SELECT
  CAST('2' + CAST(EmployeeId AS VARCHAR(30)) AS INT) as UniqueEmployeeId,
 EmployeeId, EmployeeName FROM Employees
UNION ALL
SELECT
  CAST('3' + CAST(FriendId AS VARCHAR(30)) AS INT) as UniqueFriendId, 
FriendId, FriendName From Friends

Anyway this casting to varchar(30) and back to int is an overhead since I have many records.

Could you suggest a better approach?

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

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

发布评论

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

评论(4

格子衫的從容 2024-12-19 18:56:03

如果你必须有一个 id,只需做数学计算:

SELECT  1000000 + CustomerID AS UniqueCustomerId
      , CustomerId
      , CustomerName
FROM    Customers
UNION ALL
SELECT  2000000 + EmployeeId AS UniqueEmployeeId
      , EmployeeId
      , EmployeeName
FROM    Employees
UNION ALL
SELECT  3000000 + FriendId AS UniqueFriendId
      , FriendId
      , FriendName
FROM    Friends 

If you've got to have a single id, just do math:

SELECT  1000000 + CustomerID AS UniqueCustomerId
      , CustomerId
      , CustomerName
FROM    Customers
UNION ALL
SELECT  2000000 + EmployeeId AS UniqueEmployeeId
      , EmployeeId
      , EmployeeName
FROM    Employees
UNION ALL
SELECT  3000000 + FriendId AS UniqueFriendId
      , FriendId
      , FriendName
FROM    Friends 
时光倒影 2024-12-19 18:56:03

我更喜欢这种方法,因为它仍然可以使用 CustomerID、EmployeeID 和 FriendID 上的索引

SELECT 1 [PersonType],CustomerId [PersonId], CustomerName [PersonName] FROM Customers
UNION ALL
SELECT 2, EmployeeId, EmployeeName FROM Employees
UNION ALL
SELECT 3, FriendId, FriendName From Friends

I prefer this approach as it can still use indexes on the CustomerID, EmployeeID, and FriendID

SELECT 1 [PersonType],CustomerId [PersonId], CustomerName [PersonName] FROM Customers
UNION ALL
SELECT 2, EmployeeId, EmployeeName FROM Employees
UNION ALL
SELECT 3, FriendId, FriendName From Friends
讽刺将军 2024-12-19 18:56:03

干净且可读性好,我在 Postgres 中使用它。

select CAST(row_number() OVER () AS bigint) AS id,
       *
from (
         select mtm.id     as mkt_tech_map_id,
                'vesivagu' as type,
                mv.geom,
                ''         as label
         from mkt_tech_map mtm
                  left join mkt_vesivagu mv on mtm.id = mv.mkt_tech_map_id

         union

         select mtm.id       as mkt_tech_map_id,
                'kraavisete' as type,
                mk.geom,
                ''           as label
         from mkt_tech_map mtm
                  left join mkt_kraavisete mk on mtm.id = mk.mkt_tech_map_id

         
     ) as q

Clean and well readable, I use this in Postgres.

select CAST(row_number() OVER () AS bigint) AS id,
       *
from (
         select mtm.id     as mkt_tech_map_id,
                'vesivagu' as type,
                mv.geom,
                ''         as label
         from mkt_tech_map mtm
                  left join mkt_vesivagu mv on mtm.id = mv.mkt_tech_map_id

         union

         select mtm.id       as mkt_tech_map_id,
                'kraavisete' as type,
                mk.geom,
                ''           as label
         from mkt_tech_map mtm
                  left join mkt_kraavisete mk on mtm.id = mk.mkt_tech_map_id

         
     ) as q
草莓味的萝莉 2024-12-19 18:56:03

这应该可以正常工作:

CREATE VIEW PEOPLE AS  
SELECT CustomerId, null as EmployeeId, null as FriendId, CustomerName FROM Customers 
UNION ALL 
SELECT null, EmployeeId, null, EmployeeName FROM Employees 
UNION ALL 
SELECT null, null, FriendId, FriendName From Friends 

This should work fine:

CREATE VIEW PEOPLE AS  
SELECT CustomerId, null as EmployeeId, null as FriendId, CustomerName FROM Customers 
UNION ALL 
SELECT null, EmployeeId, null, EmployeeName FROM Employees 
UNION ALL 
SELECT null, null, FriendId, FriendName From Friends 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文