-1

次のクエリがあります。「列 'Sites.IsMobileSite' は、集計関数または GROUP BY 句のいずれにも含まれていないため、選択リストでは無効です。」というエラーが表示されます。次のSQLコードの場合-

 select max(rc.[name]) [Reseller], max(c.[name]) [Customer],    
        case 
            when max(c.[Url]) is not null then max(c.[URL])
            else 'NA'
        end[URL],   
        case 
            when max(s.[Domain]) is not null then max(s.[Domain])
            else 'NA'
        end[Site Name],
        case 
        when (s.[IsMobileSite]) = 0 then 'No'
        else 'Yes'
        end [Is Mobile Site],
        case 
            when max(s.[CreatedDate]) is not null then max(s.  [CreatedDate])               
        end[Created Date]
    from customers c with(nolock)       
    left outer join Sites s with(nolock) on c.CustomerId = s.CustomerId         
    left outer join customers rc on rc.CustomerId = c.ResellerId
    where c.[name] is not null
    and ( c.customerId is null or rc.CustomerId = c.CustomerId)
    and c.IsActive !='' 
4

1 に答える 1

0

ダミアンに同意します。コードは、何をしようとしているのかを示すのに十分明確ではなく、返されたすべての列を最大ラッパーに入れるのは本当に意味がありません。ただし、エラーを削除するには、IsMobileSite を max ラッパーでラップする必要があります。

    when ( max(s.[IsMobileSite])) = 0 then 'No'
    else 'Yes'
    end [Is Mobile Site],

またはそれによってグループ化します:

 select max(rc.[name]) [Reseller], max(c.[name]) [Customer],    
    case 
        when max(c.[Url]) is not null then max(c.[URL])
        else 'NA'
    end[URL],   
    case 
        when max(s.[Domain]) is not null then max(s.[Domain])
        else 'NA'
    end[Site Name],
    case 
    when (s.[IsMobileSite]) = 0 then 'No'
    else 'Yes'
    end [Is Mobile Site],
    case 
        when max(s.[CreatedDate]) is not null then max(s.  [CreatedDate])               
    end[Created Date]
from customers c with(nolock)       
left outer join Sites s with(nolock) on c.CustomerId = s.CustomerId         
left outer join customers rc on rc.CustomerId = c.ResellerId
where c.[name] is not null
and ( c.customerId is null or rc.CustomerId = c.CustomerId)
and c.IsActive !='' 
GROUP BY s.[IsMobileSite]

また、これらすべての case ステートメントの代わりに、coalesce が代わりにジョブを実行し、提供されたリストから最初の非 null アイテムを返します。

coalesce(max(c.[Url]), 'NA') [URL], 

null でない場合は max(c.[Url]) を返し、それ以外の場合は「NA」を返します。

選択基準に一致する最初の行を返したい場合は、次のようにします。

SELECT  TOP 1
    rc.name Reseller,
    c.name Customer,
    COALESCE(c.Url, 'NA') URL,
    COALESCE(s.domain, 'NA') [Site Name],
    CASE
        WHEN s.IsMobileSite = 0 THEN  'No'
        ELSE 'YES'
    END [Is Mobile Site],
    s.CreatedDate
FROM customers c WITH(nolock)
JOIN Sites s WITH(nolock)
ON c.CustomerId = s.CustomerId    
LEFT JOIN customers rc 
ON rc.CustomerId = c.ResellerId
WHERE c.name IS NOT NULL
AND ( c.customerId is null OR rc.CustomerId = c.CustomerId
AND c.IsActive !=''  
于 2013-11-08T15:05:36.287 に答える