根据特定条件在列中显示日期

时间:2019-07-20 04:34:32

标签: sql-server tsql

运行以下代码时,出现错误:

  

子查询返回了多个值。当   子查询紧随!=<<=>>=(select top 1 convert(varchar,PaymentDate,103) from tblUTRDetail where CCN_No=CR.CCN_NO and CR.Claim_Status='Y' order by PaymentDate desc union all select top 1 convert(varchar,CreatedDate,103) from tblOpenCloseTrans where S_NO=CR.S_NO and OpenCloseType='C' and CR.Claim_Status='C' order by CreatedDate desc union all select top 1 convert(varchar,GENERATED_DATE,103) from tbldeficiencyletterTrans where S_NO=CR.S_NO and DEFICIENT_REPUDATION_FLAG='NC' and CR.Claim_Status='NC' order by GENERATED_DATE desc union all select top 1 convert(varchar,GENERATED_DATE,103) from tbldeficiencyletterTrans where S_NO=CR.S_NO and DEFICIENT_REPUDATION_FLAG='R' and CR.Claim_Status='RC' order by GENERATED_DATE desc union (select GENERATED_DATE from (Select s_no, convert (varchar,(GENERATED_DATE),103)as GENERATED_DATE ,row_number() over(partition by s_no order by s_no asc )as rw from tblDeficiencyLetterTrans b where DEFICIENT_REPUDATION_FLAG='NC' and s_no=CR.S_NO) T where rw=1) ) as SETTLEMENT_DATE, 或将子查询用作   表达式

当我添加最后一段代码时(请在并集后参阅)

start_date = driver.find_element_by_id('DayEndSumDate1').send_keys("30062019")

2 个答案:

答案 0 :(得分:0)

您的查询如下:

SELECT *

,(select 1 union all select 2
union all select 3
union all select 3
union (select 4)) as SETTLEMENT_DATE
FROM sys.tables

您正在选择一些列值,对于其中一个,您最多可以为一列SETTLEMENT_DATE返回4个值。

因此,您可以有多个值,其中一个值是期望值,这就是错误对您的意思。您必须选择:

  • 如果要查看所有值,只需像这样为每列指定一个单独的名称

    SELECT *
    
    ,select 1 as SETTLEMENT_DATE1
    ,
    select 2 as SETTLEMENT_DATE2
    ,
    select 3 as SETTLEMENT_DATE3
    ,
    select 3 as SETTLEMENT_DATE4
    ,
    (select 4) as SETTLEMENT_DATE5
    FROM sys.tables
    
  • 添加更多逻辑,以便每次从此子查询中仅返回一个值

    (
        SELECT TOP 1 date
        FROM
        (
    
            (select top 1 convert(varchar,PaymentDate,103) from tblUTRDetail where CCN_No=CR.CCN_NO and CR.Claim_Status='Y' order by PaymentDate desc
            union all
            select top 1 convert(varchar,CreatedDate,103) from tblOpenCloseTrans where S_NO=CR.S_NO and OpenCloseType='C' and CR.Claim_Status='C' order by CreatedDate desc
            union all
            select top 1 convert(varchar,GENERATED_DATE,103) from tbldeficiencyletterTrans where S_NO=CR.S_NO and DEFICIENT_REPUDATION_FLAG='NC' and CR.Claim_Status='NC' order by GENERATED_DATE desc
            union all
            select top 1 convert(varchar,GENERATED_DATE,103) from tbldeficiencyletterTrans where S_NO=CR.S_NO and DEFICIENT_REPUDATION_FLAG='R' and CR.Claim_Status='RC' order by GENERATED_DATE desc
            union 
            (select GENERATED_DATE from  (Select s_no, convert (varchar,(GENERATED_DATE),103)as GENERATED_DATE ,row_number()
            over(partition by s_no order by s_no asc )as rw from tblDeficiencyLetterTrans b
            where DEFICIENT_REPUDATION_FLAG='NC' and s_no=CR.S_NO) T where rw=1)
            ) 
        )  DS (date)
        ORDER BY date
    )
    as SETTLEMENT_DATE,
    

答案 1 :(得分:0)

create table #tempT (Material varchar(10),yyyyww  int,item_type  varchar(10),TPT int    )
-------------Insert---------------
insert into #tempT values('E123'    ,    201919 , 'Assembly'  , 7)
insert into #tempT values('E123'    ,    201919 , 'Assembly'  , 6)
insert into #tempT values('E123'    ,    201919 , 'Assembly'  , 7)
insert into #tempT values('E123'    ,    201919 , 'Assembly'  , 7)
insert into #tempT values('E123'    ,    201919 , 'FG'        , 2)
insert into #tempT values('E123'    ,    201919 , 'FG'        , 2)
insert into #tempT values('E123'    ,    201919 , 'FG'        , 1)
insert into #tempT values('E123'    ,    201919 , 'TEST'      , 7)
insert into #tempT values('E123'    ,    201919 , 'TEST'      , 4)

------------Output-------------
select Material ,ceiling(((sum((MaxTPT)))/7.00)+1)MaxTPT  
 from(select Material,item_type,case when item_type='Assembly' then max(TPT)
when item_type='fg' then max(TPT)
when item_type='TEST' then max(TPT) end MaxTPT from #tempT
group by Material,item_type)a`enter code here`
group by Material