选择只包含两个值的记录

时间:2018-05-16 14:47:09

标签: sql

我有一个包含两列用户和详细信息的表。我只需要记录用户只有过期和推荐的详细信息,而不是过期,推荐,丢失和处理的用户。

**Users Details**
1560  Overdue
1560  Overdue
1560  Referral
1470  Overdue
1470  Referral
1470  Lost
1470  Processing
1480  Referral
1480  Lost
1480  Processing
1356  Referral
1356  Overdue
1289  Referral 

Output has to be:
1560
1356
1289

4 个答案:

答案 0 :(得分:4)

一种方法是按` form.setScript('customscript_toloader') form.addButton('custpagetestbutton', 'TEST button', 'createTO();');` 对数据进行分组,然后只采用那些需要function createTO() //(request, response) { alert("This function was called"); //variable set up nlapiLogExecution('DEBUG', 'script', 'runs 1'); var POID ; var POType ; var PORecord; var lines; nlapiLogExecution('DEBUG', 'script', 'runs 2'); var arrayName = new Array(); var arrayQty = new Array(); PORecord = nlapiGetNewRecord(); lines = PORecord.getLineItemCount('item'); POID = nlapiGetRecordId(); POTYPE = nlapiGetRecordType(); // get name and quantity for ( var i = 1; i < lines + 1 ; i++ ) { arrayName[i] = PORecord.getLineItemValue('item', 'item', i ); arrayQty[i] = PORecord.getLineItemValue('item', 'quantity' , i); } nlapiLogExecution('DEBUG', 'script', 'runs 3'); //creates to and changes focus var TOrecord = nlapiCreateRecord ('transferorder'); var TOrecordID = TOrecord.getId(); TOrecord.setFieldValue('customform',128); //subsidiaries CC bedford id is 2 TOrecord.setFieldValue('subsidiary',2); //testing for location and transfer location, 144 & 145 TOrecord.setFieldValue('location',144); TOrecord.setFieldValue('transferlocation',145); TOrecord.setFieldValue('memo', 'PO: ' + POID ); TOrecord.setFieldValue('employee',nlapiGetContext().getUser()); //TOrecord.setFieldValue('department',"C-C"); //set name and quantity for ( var j = 1; j < lines +1 ; j++ ) { arrayName[j] = parseInt(arrayName[j]); TOrecord.setLineItemValue("item", "item", j , arrayName[j] ); TOrecord.setLineItemValue("item", "quantity", j , parseInt(arrayQty[j])); //added parse int, should work } // set the item and location values on the currently selected line nlapiSetCurrentLineItemValue('item', 'location', 6); // commit the line to the database //nlapiCommitLineItem('item'); var TOResult = nlapiSubmitRecord(TOrecord, true, true); var TOTranID= nlapiLookupField('transferorder', TOResult, 'tranid'); var poURL = nlapiResolveURL('RECORD', 'transferorder', TOResult); nlapiSetRedirectURL('RECORD','transferorder', TOResult); return; } 但不包含其他人的数据

users

如果您 希望用户 detailsselect users from your_table group by users having sum(case when details = 'Referral' then 1 else 0 end) > 0 and sum(case when details = 'Overdue' then 1 else 0 end) > 0 and sum(case when details not in ('Referral', 'Overdue') then 1 else 0 end) = 0 ,那么

Referral

答案 1 :(得分:0)

SELECT DISTINCT
    UD1.user_id
FROM
    User_Details UD1
INNER JOIN User_Details UD2 ON
    UD2.user_id = UD1.user_id AND
    UD2.description = 'Referral'
WHERE
    UD1.description = 'Overdue' AND
    NOT EXISTS (
        SELECT *
        FROM User_Details UD3
        WHERE
            UD3.user_id = UD1.user_id AND
            UD3.description NOT IN ('Overdue', 'Referral')
    )

答案 2 :(得分:0)

你可以这样做。查询具有单行的中间映射表,指示每个用户正在使用哪些详细信息。 只需按表格名称更改[YOUR_TABLE]:

SELECT MAPPING.USERS 
  FROM (SELECT USERS   
              ,CASE WHEN SUM(CASE WHEN DETAILS = 'Overdue'    THEN 1 ELSE 0 END) >= 1 THEN 1 ELSE 0 END AS HAS_OVERDUE
              ,CASE WHEN SUM(CASE WHEN DETAILS = 'Lost'       THEN 1 ELSE 0 END) >= 1 THEN 1 ELSE 0 END AS HAS_LOST
              ,CASE WHEN SUM(CASE WHEN DETAILS = 'processing' THEN 1 ELSE 0 END) >= 1 THEN 1 ELSE 0 END AS HAS_PROCESSING
              ,CASE WHEN SUM(CASE WHEN DETAILS = 'referral'   THEN 1 ELSE 0 END) >= 1 THEN 1 ELSE 0 END AS HAS_REFERRAL    
          FROM [YOUR_TABLE]
         GROUP BY USERS) AS MAPPING
 WHERE (MAPPING.HAS_OVERDUE = 1 OR MAPPING.HAS_REFERRAL = 1)
   AND (MAPPING.HAS_LOST + MAPPING.HAS_PROCESSING) < 2

答案 3 :(得分:0)

我只会使用not exists

select distinct t.users 
from table t
where not exists (select 1 
                  from table 
                  where users = t.users and 
                        details in ('lost', 'Processing')
                 );