根据特定条件计算布尔值

时间:2017-07-06 12:19:37

标签: sql-server boolean counting

Database Table

亲爱的All,附带的是sql server 2016数据库表。我希望根据条件AssetTypeID获取真实和flase值的列IsAssetIssued计数。我正在尝试

SELECT COUNT(IsAssetIssued) AS AvlAssets 
FROM AssetStatus 
WHERE (IsAssetIssued = True) 
  AND (AssetTypeID = 1)

但这不会为列中的错误值提供准确的结果。请提出更好的解决方案。

谢谢,

3 个答案:

答案 0 :(得分:0)

试试这个;

SELECT COUNT(IsAssetIssued) AS AvlAssets ,IsAssetIssued
FROM AssetStatus 
WHERE (AssetTypeID = 1)
group by IsAssetIssued

答案 1 :(得分:0)

试试这个

SELECT AssetTypeID,IsAssetIssued FROM 
(
SELECT AssetTypeID,IsAssetIssued,
COUNT(IsAssetIssued) OVER(Partition by AssetTypeID Order by AssetTypeID )AS AvlAssets ,
ROW_NUMBER()OVER(Partition by AssetTypeID Order by AssetTypeID ) AS Seq
FROM AssetStatus 
)Dt
WHERE Dt.Seq=1

答案 2 :(得分:0)

感谢您的帮助,人们帮助我登录。查询中只有轻微的变化,即我将IsAssetIssued值固定为false并将AssetTypeID设置为动态。

SELECT COUNT(IsAssetIssued)AS AvlAssets FROM AssetStatus WHERE(IsAssetIssued =' False')AND(AssetTypeID = 1);

它完美无缺。