如何在 SELECT 部分中包含 BIT 类型列而不将其包含在 T-SQL 中的 GROUP BY 中?

发布于 2024-11-08 04:59:17 字数 768 浏览 6 评论 0原文

这是我的 T-SQL 查询,

SELECT 
    ProductID,
    VendorID,
    ProductName= MAX(ProductName),
    VendorName = MAX(VendorName),
    IsActive = MAX(IsActive) # This brings error 
FROM ProductVendorAssoc 
GROUP BY  
    ProductID,
    VendorID

我只想对 ProductID 和 VendorID 字段应用 GROUP BY,但需要填充 ProductID、VendorID、ProductName、VendorName , IsActive 字段。

在这里,我使用聚合函数 MAX(ProductName) 来避免分组依据列表中的 ProductName

但同样的技巧不适用于 BIT 列,因为操作数数据类型 bit 对于 max 运算符无效。

如何在 SELECT 部分中包含 BIT 类型列而不将其包含在 GROUP BY 中?

更新。

如果我需要在SELECT中包含INT列(例如UserID),我应该做什么同样的方式

Here is my T-SQL query

SELECT 
    ProductID,
    VendorID,
    ProductName= MAX(ProductName),
    VendorName = MAX(VendorName),
    IsActive = MAX(IsActive) # This brings error 
FROM ProductVendorAssoc 
GROUP BY  
    ProductID,
    VendorID

I want to apply GROUP BY only for the ProductID and VendorID fields, but need to populate the ProductID, VendorID, ProductName, VendorName, IsActive fields.

Here I used the agreggate function MAX(ProductName) to avoid ProductName in the group by list.

But the same trick is not working for BIT columns as operand data type bit is invalid for max operator.

How can i include BIT type column in SELECT part with out including it on the GROUP BY?

Update.

What should I need to do if i need to include an INT column like UserID in SELECT in the same way

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

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

发布评论

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

评论(3

那请放手 2024-11-15 04:59:19

在 SQL2005/2008 中,这将如下所示:

select ProductId, VendorId, ProductName, VendorName, IsActive
from
(
    select *, row_number() over (partition by ProductId, VendorId order by ProductId) RowNumber
    from Production.Product
) tt
where RowNumber = 1

In SQL2005/2008 this would be look like this:

select ProductId, VendorId, ProductName, VendorName, IsActive
from
(
    select *, row_number() over (partition by ProductId, VendorId order by ProductId) RowNumber
    from Production.Product
) tt
where RowNumber = 1
飞烟轻若梦 2024-11-15 04:59:18

将 CASE 表达式放在那里,或者将其转换为 int:

IsActive = MAX(CASE WHEN IsActive=1 THEN 1 ELSE 0 END)

或者,

IsActive = MAX(CONVERT(int,IsActive))

显然,您还应该意识到,这意味着结果集中的 ProductName、VendorName 和 IsActive 列中的值可能全部来自基中的不同行桌子。


如果您希望这三列实际上全部来自同一行(假设 SQL Server 2005 或更高版本),您可以执行以下操作:

;With Numbered as (
    SELECT *,ROW_NUMBER() OVER (
        PARTITION BY ProductID,VendorID
        ORDER BY /* Something appropriate, or if we just want random... */ newid()) as rn
    FROM ProductVendorAssoc
)
select
    ProductID,
    VendorID,
    ProductName,
    VendorName,
    IsActive
FROM Numbered where rn=1

Put a CASE expression in there, or convert it to int:

IsActive = MAX(CASE WHEN IsActive=1 THEN 1 ELSE 0 END)

or,

IsActive = MAX(CONVERT(int,IsActive))

You should also be aware, obviously, that this means that the values in the ProductName, VendorName and IsActive columns in the result set may all come from different rows in the base table.


If you want those three columns to actually all be from the same row (and assuming SQL Server 2005 or later), you'd do something like:

;With Numbered as (
    SELECT *,ROW_NUMBER() OVER (
        PARTITION BY ProductID,VendorID
        ORDER BY /* Something appropriate, or if we just want random... */ newid()) as rn
    FROM ProductVendorAssoc
)
select
    ProductID,
    VendorID,
    ProductName,
    VendorName,
    IsActive
FROM Numbered where rn=1
回忆凄美了谁 2024-11-15 04:59:18

更短的方法来做到这一点:

IsActive = MAX(0+IsActive)

Shorter way to do this:

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