SQL Server GROUP BY 和 FIRST

发布于 2024-12-11 13:59:52 字数 718 浏览 0 评论 0原文

我有很多画廊里的物品。我有很多照片。图片在画廊里。

我想创建一个视图,在其中列出画廊以及画廊中第一张图片的一些属性。

类似于:

ALTER VIEW [foto].[gallery_with_picture]    
AS 
select
        main.*
        ,FIRST(pics.[picture_id])
        ,FIRST(pics.[picture_width])
        ,FIRST(pics.[picture_height])
        ,FIRST(pics.[URLPart] as picture_url_part)
        ,FIRST(pics.[Extension] as picture_extension)
    from
        [v_gallery] main
        left join [v_picture_recursive] pics on main.[foto_reference_picture_group_modeling_object/obj_id] = pics.woc_root
    group by
        main.*
    order by
        pics.[picture_id]

当然这不起作用,因为 SQL Server 没有可用的 FIRST 聚合函数。或者确实如此?

或者我应该使用 top1 进行内部选择,因为没有更简单的解决方案?

I have a lot of gallery objects. I have a lot of pictures. The pictures are in the galleries.

I want to create a view where I list the galleries and some properties of the first picture in the gallery.

Something like:

ALTER VIEW [foto].[gallery_with_picture]    
AS 
select
        main.*
        ,FIRST(pics.[picture_id])
        ,FIRST(pics.[picture_width])
        ,FIRST(pics.[picture_height])
        ,FIRST(pics.[URLPart] as picture_url_part)
        ,FIRST(pics.[Extension] as picture_extension)
    from
        [v_gallery] main
        left join [v_picture_recursive] pics on main.[foto_reference_picture_group_modeling_object/obj_id] = pics.woc_root
    group by
        main.*
    order by
        pics.[picture_id]

Of course this does not work as SQL Server has no working FIRST aggregate function for this. Or does it?

Or should I do an inner select with a top1 as there is no simpler solution?

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

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

发布评论

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

评论(1

一指流沙 2024-12-18 13:59:52

这是一种方法。

ALTER VIEW [foto].[gallery_with_picture] 
AS
SELECT main.*, /*But best to list columns explicitly*/
       pics.[picture_id],
       pics.[picture_width],
       pics.[picture_height],
       pics.[URLPart]   AS picture_url_part,
       pics.[Extension] AS picture_extension
FROM   [v_gallery] main
       OUTER APPLY
       (SELECT TOP 1 *
        FROM   [v_picture_recursive] pics
        WHERE  main.[foto_reference_picture_group_modeling_object/obj_id] =
               pics.woc_root
        ORDER  BY pics.[picture_id]) pics  

或者另一个

ALTER VIEW [foto].[gallery_with_picture] 
AS
 WITH pics AS ( 
 SELECT *, 
        ROW_NUMBER() OVER (PARTITION BY woc_root 
                               ORDER BY [picture_id]) RN 
 FROM [v_picture_recursive])
SELECT main.*,
       pics.[picture_id],
       pics.[picture_width],
       pics.[picture_height],
       pics.[URLPart]   AS picture_url_part,
       pics.[Extension] AS picture_extension
FROM   [v_gallery] main
       LEFT JOIN pics
         ON main.[foto_reference_picture_group_modeling_object/obj_id] =
            pics.woc_root
            AND RN = 1  

Here's one way.

ALTER VIEW [foto].[gallery_with_picture] 
AS
SELECT main.*, /*But best to list columns explicitly*/
       pics.[picture_id],
       pics.[picture_width],
       pics.[picture_height],
       pics.[URLPart]   AS picture_url_part,
       pics.[Extension] AS picture_extension
FROM   [v_gallery] main
       OUTER APPLY
       (SELECT TOP 1 *
        FROM   [v_picture_recursive] pics
        WHERE  main.[foto_reference_picture_group_modeling_object/obj_id] =
               pics.woc_root
        ORDER  BY pics.[picture_id]) pics  

Or another

ALTER VIEW [foto].[gallery_with_picture] 
AS
 WITH pics AS ( 
 SELECT *, 
        ROW_NUMBER() OVER (PARTITION BY woc_root 
                               ORDER BY [picture_id]) RN 
 FROM [v_picture_recursive])
SELECT main.*,
       pics.[picture_id],
       pics.[picture_width],
       pics.[picture_height],
       pics.[URLPart]   AS picture_url_part,
       pics.[Extension] AS picture_extension
FROM   [v_gallery] main
       LEFT JOIN pics
         ON main.[foto_reference_picture_group_modeling_object/obj_id] =
            pics.woc_root
            AND RN = 1  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文