如何编写一个case语句,以根据一组条件为一组唯一的记录返回一条消息?

时间:2019-04-01 22:35:39

标签: sql sql-server

表1:

AccountID  AccountNumber  AccountTypeNumber
1          50             100
2          50             250
3          60             100
4          60             200

表2:

AccountNumber  AccountName  AccountStatus
50             School       Active
60             Work         Active
70             School       Active

结果表:

AccountNumber  AccountVerification
50             Pass
60             Pass
70             Fail

我希望结果表为每个唯一的'Pass'返回一个'Fail' / AccountNumber结果。 'Pass'适用于AccountStatus = Active中具有Table2且至少AccountTypeNumber = 100中具有Table1的记录。其他所有,返回'Fail'

我当前的结果仅在表1中显示AccountNumbers。它们不包含AccountNumbers中的Table2而不是Table1中的SELECT DISTINCT Table1.AccountNumber CASE WHEN Count (*) OVER (PARTITION BY Table1.AccountNumber) > 1 THEN 'Pass' ELSE 'Fail' END AS 'AccountVerification' FROM Table1 WHERE Table1.AccountTypeNumber = '100' INNER JOIN Table2 ON Table2.AccountNumber = Table1.AccountNumber WHERE Table2.AccountStatus = 'Active'

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common import by
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Firefox(executable_path='C:\webdrive\geckodriver.exe')

url= "https://b2b-sso.bmw.com/login/login_b2b.fcc? TYPE=33619969&REALMOID=06-88882215-3ed6-4d48-a202-c1198bb66e4d&GUID=&SMAUTHREASON=0&METHOD=GET&SMAGENTNAME=$SM$fF%2fInZ1t%2b%2f1x7LPZ9ZpPr3Jmh3cKinaBuNkYKXWRFpXzW38w4hPWpxtCselZqile&TARGET=$SM$HTTPS%3a%2f%2fb2b%2ebmw%2ecom%2fgroup%2fb2b%3flang%3den"

driver.get(url)


try:
element = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.xpath('//*[@id="mainContent"]/div[2]/ul/li[2]/a')))
)
finally:

us =     driver.find_element_by_xpath('/html/body/div/form/div/div[5]/input[1]')

2 个答案:

答案 0 :(得分:2)

您可以使用带有内联相关子查询的CASE构造来检查相关记录是否存在于table1中,例如:

SELECT
    t2.AccountNumber,
    CASE WHEN
        t2.AccountStatus = 'Active'
        AND EXISTS (
            SELECT 1
            FROM table1 t1
            WHERE t1.AccountNumber = t2.AccountNumber AND t1.AccountTypeNumber=100
        )
    THEN 'Pass' ELSE 'Fail' END AS AccountVerification
FROM table2 t2

这将正确处理一个帐户在table2中但在table1中不存在的用例,并且如果table1中有多个匹配记录,也可以避免重复。但是,如果您的帐号在table2中出现多次,那么您将需要使用DISTINCT(由于这种情况不会出现在示例数据中,因此我没有使用它,请放心使用添加它(如果需要)。

demo on DB Fiddle 及其示例数据将返回:

AccountNumber | AccountVerification
------------: | :------------------
           50 | Pass               
           60 | Pass               
           70 | Fail               

答案 1 :(得分:0)

Table2Table1的左联接中,排除Table1行中的where accounttypenumber <> 100行:

select
  t2.accountnumber,
  case 
    when t2.accountstatus = 'Active' and t1.accountnumber is not null then 'Pass'
    else 'Fail'
  end accountverification
from table2 t2 left join (
  select accountnumber from table1
  where accounttypenumber = 100
) t1
on t1.accountnumber = t2.accountnumber