简体   繁体   中英

get the count for different data in single column as set

I am having table "Activation" with the columns account ID and email and product name

product name column is having data like this below

 Platform
 Access
 Onboard
 OnGuard
 Platform
 Access
 Onboard
 OnGuard

when user activated we will insert the data as a set like this ( Platform ,Access ,Onboard ,OnGuard) and i need to give a warning to the user if he is inserting more than one set (Platform,Access,Onboard,OnGuard)

I can get the count for single product name insertion like (platform or access), but i need to get the count for single set insertion here ..

Could any one please help on this query how to get count for single set insertion and i am using sql server as a DB for these insertions.

Many thanks in advance..

UPDATE

sorry for confusion i need to give a warning to user if he inserted more than one time any of the product name defined in the set not as a single set ..

Assuming that unique products count is 4 , you can check it using aggregation along with having .

select accountid
from activation
group by accountid
having count(distinct productname) = 4

Or you can reverse the logic according to your need(if you want to get the users against whom not all products are defined by changing = to <>

DEMO

You Can Use this In Trigger.

DECLARE @Product_Name NVARCHAR(50)
IF EXISTS (SELECT 1
            FROM Activation WHERE productname = @Product_Name) BEGIN

    RAISERROR ('Product Name  Already Exists', 16, 1)

END

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM