在if函数中面对SQL查询中的问题 [英] Facing Problem in sql Query in if function

查看:66
本文介绍了在if函数中面对SQL查询中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//下面是我的解决方案的代码

  if  SELECT  Student_ID,Fee_Type,SUM(金额) AS 付款 FROM  FeePaid_Details < span class =code-keyword> WHERE (Student_ID = '  PS20130001')< span class =code-keyword> AND (Fee_Type = ' 入场费' AND (Month_Details = '  jan' GROUP   BY  Student_ID,Fee_Type) SELECT  Student_ID,Fee_Type,SUM(金额) AS 付费 FROM  FeePaid_Details  WHERE (Student_ID = '  PS20130001' AND (Fee_Type = ' 入场费' AND (Month_Details = '  feb' GROUP   BY  Student_ID,Fee_Type)
开始
选择 q.Student_ID,q.Fee_Type,q.amount,q.amount-p.paid as Current_month_due,q.amount-g.paid as previous_month_due,(q.amount-g.paid)+(q.amount-p.paid) as total_amount 来自 Student_Fee_Quotations q join 选择 d.Student_ID,d.Fee_Type,sum(da mount) as 付费来自 FeePaid_Details d 其中 d.Student_ID = ' PS20130001' d.Fee_Type = ' 入场费' d.Month_Details = ' jan' group by d.Student_ID,d.Fee_Type) as g on q.Student_ID = ' PS20130001' q.Fee_Type = ' 入场费' join 选择 d.Student_ID,d.Fee_Type ,sum(d.amount) as 付款来自 FeePaid_Details d 其中 d.Student_ID = ' PS20130001' d.Fee_Type = ' 入场费' d.Month_Details = ' feb' group by d.Student_ID,d.Fee_Type) as p on q.Student_ID = ' PS20130001' q.Fee_Type = ' 入场费'
end
else if SELECT Student_ID,Fee_Type,SUM(金额) AS paid FROM FeePaid_Details WHERE (Student_ID = ' PS20130001' AND (Fee_Type = ' 入场费' AND (Month_Details = ' jan' GROUP BY Student_ID,Fee_Type) SELECT Student_ID,Fee_Type,SUM(金额) AS 付费 FROM FeePaid_Details WHERE (Student_ID = ' PS20130001' AND (Fee_Type = ' 入场费' AND (Month_Details = ' < span class =code-string> feb') GROUP BY Student_ID,Fee_Type)
开始
if SELECT Student_ID,Fee_Type,SUM(金额) AS 付费 FROM FeePaid_Details WHERE (Student_ID = ' PS20130001' AND (Fee_Type = ' 入场费' AND (Month_Details = ' jan' GROUP BY Student_ID,Fee_Type)
开始
选择 q.Student_ID ,q.Fee_Type,q.amount,q.amount as Current_month_due,q.amount-g.paid as previous_month_due,(q.amount-g.paid)+(q.amount) as total_amount 来自 Student_Fee_Quotations q join 选择 d.Student_ID,d.Fee_Type,sum(d.amount) 支付来自 FeePaid_Details d 其中 d.Student_ID = ' PS20130001' d.Fee_Type = ' 入场费 d.Month_Details = ' jan' group by d.Student_ID,d.Fee_Type) as g on q.Student_ID = ' PS20130001' q.Fee_Type = ' 入场费'
结束
else SELECT Student_ID,Fee_Type,SUM(金额) AS 付费 FROM FeePaid_Details WHERE (Student_ID = ' PS20130001' AND (Fee_Type = ' 入场费' AND ( Month_Details = ' feb' GROUP < span class =code-keyword> BY Student_ID,Fee_Type)
开始
选择 q.Student_ID,q.Fee_Type,q.amount,q.amount-p.paid as Current_month_due, 0 previous_month_due,(q.amount-p.paid) as total_amount < span class =code-keyword> from Student_Fee_Quotations q join select d.Student_ID ,d.Fee_Type,sum(d.amount) as 付费来自 FeePaid_Details d 其中​​ d.Student_ID = ' PS20130001' d.Fee_Type = ' 入场费 d.Month_Details = ' feb' group by d.Student_ID,d.Fee_Type) as p on q.Student_ID = ' PS20130001' q.Fee_Type = ' 入场费'
结束
结束
其他
开始
选择 Student_ID,Fee_Type,金额,金额 as Current_month_due, 0 as previous_month_due,amount as total_amount 来自 Student_Fee_Quotations 其中 Student_ID = ' PS20130001' Fee_Type = ' 入场费'
end





//执行此操作后这些是我面临的错误



消息4145,等级15,状态1,行1

在预期条件的上下文中指定的非布尔类型的表达式,在''和''附近。 />
消息156,等级15,状态1,行5

关键字''else''附近的语法不正确。

消息4145,等级15,状态1,第5行

在预期条件的上下文中指定的非布尔类型的表达式,接近''或''。

消息4145,级别15,状态1,行8
在预期条件的上下文中指定的非布尔类型的表达式,在''begin'附近。

消息156,级别15,状态1,行11

关键字''else''附近的语法不正确。

消息156,级别15,状态1,行16

关键字附近的语法不正确'else''。

解决方案

您好

您在哪里比较价值......?并且您的查询还返回多个列



 如果(< span class =code-keyword> SELECT   Student_ID,Fee_Type,SUM(金额) AS 已付 
< span class =code-keyword> FROM FeePaid_Details WHERE (Student_ID = ' PS20130001' AND (Fee_Type = ' 入场费' AND (Month_Details = ' < span class =code-string> jan')
GROUP BY Student_ID,Fee_Type) =?
SELECT Student_ID,Fee_Type,SUM(金额) AS 已付款
FROM FeePaid_Details
WHERE (Student_ID = ' PS20130001' AND (Fee_Type = ' 入场费' AND (Month_Details = ' feb'
GROUP BY Student_ID,Fee_Type) =?





您的查询本身就是错误的...... 。



查看链接...

IF ... ELSE(Transact-SQL) [ ^ ]

SQL IF ... ELSE声明 [ ^ ]



问候,

GVPrabu


如果存在((SELECT Student_ID,Fee_Type,SUM(金额)AS从FeePaid_Details支付WHERE(Student_ID =''PS20130001'')和(Fee_Type =''入场费'')和(Month_Details =''jan'')GROUP BY Student_ID,Fee_Type)和(SELECT Student_ID,Fee_Type, SUM(金额)AS从FeePaid_Details支付WHERE(Student_ID ='''PS20130001'')和(Fee_Type =''入场费'')和(Month_Details =''feb'')GROUP BY Student_ID,Fee_Type))

begin

选择q.Student_ID,q.Fee_Type,q.amount,q.amount-p.paid as Current_month_due,q.amount-g.paid as previous_month_due,(q.amount -g.paid)+(q.amount-p.paid)作为来自Student_Fee_Quotations的join_amount q join(选择d.Student_ID,d.Fee_Type,sum(d.amount)从FeePaid_Details d支付,其中d.Studen t_ID ='''PS20130001''和d.Fee_Type =''入场费''和d.Month_Details =''jan''组由d.Student_ID,d.Fee_Type)为g on q.Student_ID =''PS20130001''和q.Fee_Type =''入场费''加入(选择d.Student_ID,d.Fee_Type,sum(d.amount)从FeePaid_Details d支付,其中d.Student_ID =''PS20130001''和d.Fee_Type =''入场费''和d.Month_Details =''feb''组由d.Student_ID,d.Fee_Type)作为p on q.Student_ID ='''PS20130001''和q.Fee_Type =''入场费''br / >
结束

否则存在((SELECT Student_ID,Fee_Type,SUM(金额)AS从FeePaid_Details支付WHERE(Student_ID ='''PS20130001'')和(Fee_Type =''入场费'')AND(Month_Details =''jan'')GROUP BY Student_ID,Fee_Type)或(SELECT Student_ID,Fee_Type,SUM(金额)AS从FeePaid_Details支付WHERE(Student_ID ='''PS20130001'')和(Fee_Type =''入场费'')和(Month_Details =''f eb'')GROUP BY Student_ID,Fee_Type))

begin

如果存在(SELECT Student_ID,Fee_Type,SUM(金额)AS从FeePaid_Details支付WHERE(Student_ID ='' PS20130001'')AND(Fee_Type =''入场费'')和(Month_Details =''jan'')GROUP BY Student_ID,Fee_Type)

begin

select q .Student_ID,q.Fee_Type,q.amount,q.amount as Current_month_due,q.amount-g.paid as previous_month_due,(q.amount-g.paid)+(q.amount)as total_amount from Student_Fee_Quotations q join(select d.Student_ID,d.Fee_Type,sum(d.amount)从FeePaid_Details d支付,其中d.Student_ID =''PS20130001''和d.Fee_Type =''入场费''和d.Month_Details =''jan'' d.Student_ID,d.Fee_Type)分组为q.Student_ID =''PS20130001''和q.Fee_Type =''入场费''

结束

否则如果存在(SELECT Student_ID,Fee_Type,SUM(金额)AS从FeePaid_Details WHERE(Student_ID ='''PS20130001'')和(Fee_Ty)支付pe =''入场费'')AND(Month_Details =''feb'')GROUP BY Student_ID,Fee_Type)

begin

select q.Student_ID,q.Fee_Type ,q.amount,q.amount-p.paid作为Current_month_due,0作为previous_month_due,(q.amount-p.paid)作为来自Student_Fee_Quotations的join_amount q join(选择d.Student_ID,d.Fee_Type,sum(d.amount)从FeePaid_Details d支付,其中d.Student_ID =''PS20130001''和d.Fee_Type =''入场费''和d.Month_Details =''feb''组由d.Student_ID,d.Fee_Type)作为p on q .Student_ID ='''PS20130001''和q.Fee_Type =''入场费''

结束

结束

其他

begin

选择Student_ID,Fee_Type,金额,金额为Current_month_due,0为previous_month_due,金额为来自Student_Fee_Quotations的total_amount,其中Student_ID ='''PS20130001''和Fee_Type =''入场费''

结束


//Below is my code for my solution

if (SELECT Student_ID,Fee_Type, SUM(amount) AS paid FROM FeePaid_Details WHERE (Student_ID = 'PS20130001') AND (Fee_Type = 'Admission Fee') AND (Month_Details = 'jan') GROUP BY Student_ID, Fee_Type) and (SELECT Student_ID,Fee_Type, SUM(amount) AS paid FROM FeePaid_Details WHERE (Student_ID = 'PS20130001') AND (Fee_Type = 'Admission Fee') AND (Month_Details = 'feb') GROUP BY Student_ID, Fee_Type)
begin
select q.Student_ID,q.Fee_Type,q.amount,q.amount-p.paid as Current_month_due,q.amount-g.paid as previous_month_due,(q.amount-g.paid)+(q.amount-p.paid) as total_amount from Student_Fee_Quotations q join(select d.Student_ID,d.Fee_Type,sum(d.amount) as paid from FeePaid_Details d where d.Student_ID='PS20130001' and d.Fee_Type='Admission Fee' and d.Month_Details='jan' group by d.Student_ID,d.Fee_Type )as g on q.Student_ID='PS20130001' and q.Fee_Type='Admission Fee' join (select d.Student_ID,d.Fee_Type,sum(d.amount) as paid from FeePaid_Details d where d.Student_ID='PS20130001' and d.Fee_Type='Admission Fee' and d.Month_Details='feb' group by d.Student_ID,d.Fee_Type ) as p on q.Student_ID='PS20130001' and q.Fee_Type='Admission Fee'
end
else if (SELECT Student_ID,Fee_Type, SUM(amount) AS paid FROM FeePaid_Details WHERE (Student_ID = 'PS20130001') AND (Fee_Type = 'Admission Fee') AND (Month_Details = 'jan') GROUP BY Student_ID, Fee_Type) or (SELECT Student_ID,Fee_Type, SUM(amount) AS paid FROM FeePaid_Details WHERE (Student_ID = 'PS20130001') AND (Fee_Type = 'Admission Fee') AND (Month_Details = 'feb') GROUP BY Student_ID, Fee_Type)
begin
if (SELECT Student_ID,Fee_Type, SUM(amount) AS paid FROM FeePaid_Details WHERE (Student_ID = 'PS20130001') AND (Fee_Type = 'Admission Fee') AND (Month_Details = 'jan') GROUP BY Student_ID, Fee_Type)
begin
select q.Student_ID,q.Fee_Type,q.amount,q.amount as Current_month_due,q.amount-g.paid as previous_month_due,(q.amount-g.paid)+(q.amount) as total_amount from Student_Fee_Quotations q join(select d.Student_ID,d.Fee_Type,sum(d.amount) as paid from FeePaid_Details d where d.Student_ID='PS20130001' and d.Fee_Type='Admission Fee' and d.Month_Details='jan' group by d.Student_ID,d.Fee_Type )as g on q.Student_ID='PS20130001' and q.Fee_Type='Admission Fee'
end
else(SELECT Student_ID,Fee_Type, SUM(amount) AS paid FROM FeePaid_Details WHERE (Student_ID = 'PS20130001') AND (Fee_Type = 'Admission Fee') AND (Month_Details = 'feb') GROUP BY Student_ID, Fee_Type)
begin
select q.Student_ID,q.Fee_Type,q.amount,q.amount-p.paid as Current_month_due,0 as previous_month_due,(q.amount-p.paid) as total_amount from Student_Fee_Quotations q join(select d.Student_ID,d.Fee_Type,sum(d.amount) as paid from FeePaid_Details d where d.Student_ID='PS20130001' and d.Fee_Type='Admission Fee' and d.Month_Details='feb' group by d.Student_ID,d.Fee_Type ) as p on q.Student_ID='PS20130001' and q.Fee_Type='Admission Fee'
end
end
else
begin
select Student_ID,Fee_Type,amount,amount as Current_month_due,0 as previous_month_due,amount as total_amount from Student_Fee_Quotations where  Student_ID='PS20130001' and Fee_Type='Admission Fee'
end




//after executing this these are the errors i am facing

Msg 4145, Level 15, State 1, Line 1
An expression of non-boolean type specified in a context where a condition is expected, near ''and''.
Msg 156, Level 15, State 1, Line 5
Incorrect syntax near the keyword ''else''.
Msg 4145, Level 15, State 1, Line 5
An expression of non-boolean type specified in a context where a condition is expected, near ''or''.
Msg 4145, Level 15, State 1, Line 8
An expression of non-boolean type specified in a context where a condition is expected, near ''begin''.
Msg 156, Level 15, State 1, Line 11
Incorrect syntax near the keyword ''else''.
Msg 156, Level 15, State 1, Line 16
Incorrect syntax near the keyword ''else''.

解决方案

Hi
Where you are comparing values... ? and your Query also return More than one columns

if (SELECT Student_ID,Fee_Type, SUM(amount) AS paid 
    FROM FeePaid_Details WHERE (Student_ID = 'PS20130001') AND (Fee_Type = 'Admission Fee') AND (Month_Details = 'jan') 
    GROUP BY Student_ID, Fee_Type) =? 
and (SELECT Student_ID,Fee_Type, SUM(amount) AS paid 
     FROM FeePaid_Details 
     WHERE (Student_ID = 'PS20130001') AND (Fee_Type = 'Admission Fee') AND (Month_Details = 'feb') 
     GROUP BY Student_ID, Fee_Type) =?



Your Query it self wrong....

Check the links...
IF...ELSE (Transact-SQL)[^]
SQL IF...ELSE Statement[^]

Regards,
GVPrabu


if exists((SELECT Student_ID,Fee_Type, SUM(amount) AS paid FROM FeePaid_Details WHERE (Student_ID = ''PS20130001'') AND (Fee_Type = ''Admission Fee'') AND (Month_Details = ''jan'') GROUP BY Student_ID, Fee_Type) and (SELECT Student_ID,Fee_Type, SUM(amount) AS paid FROM FeePaid_Details WHERE (Student_ID = ''PS20130001'') AND (Fee_Type = ''Admission Fee'') AND (Month_Details = ''feb'') GROUP BY Student_ID, Fee_Type))
begin
select q.Student_ID,q.Fee_Type,q.amount,q.amount-p.paid as Current_month_due,q.amount-g.paid as previous_month_due,(q.amount-g.paid)+(q.amount-p.paid) as total_amount from Student_Fee_Quotations q join(select d.Student_ID,d.Fee_Type,sum(d.amount) as paid from FeePaid_Details d where d.Student_ID=''PS20130001'' and d.Fee_Type=''Admission Fee'' and d.Month_Details=''jan'' group by d.Student_ID,d.Fee_Type )as g on q.Student_ID=''PS20130001'' and q.Fee_Type=''Admission Fee'' join (select d.Student_ID,d.Fee_Type,sum(d.amount) as paid from FeePaid_Details d where d.Student_ID=''PS20130001'' and d.Fee_Type=''Admission Fee'' and d.Month_Details=''feb'' group by d.Student_ID,d.Fee_Type ) as p on q.Student_ID=''PS20130001'' and q.Fee_Type=''Admission Fee''
end
else if exists((SELECT Student_ID,Fee_Type, SUM(amount) AS paid FROM FeePaid_Details WHERE (Student_ID = ''PS20130001'') AND (Fee_Type = ''Admission Fee'') AND (Month_Details = ''jan'') GROUP BY Student_ID, Fee_Type) or (SELECT Student_ID,Fee_Type, SUM(amount) AS paid FROM FeePaid_Details WHERE (Student_ID = ''PS20130001'') AND (Fee_Type = ''Admission Fee'') AND (Month_Details = ''feb'') GROUP BY Student_ID, Fee_Type))
begin
if exists(SELECT Student_ID,Fee_Type, SUM(amount) AS paid FROM FeePaid_Details WHERE (Student_ID = ''PS20130001'') AND (Fee_Type = ''Admission Fee'') AND (Month_Details = ''jan'') GROUP BY Student_ID, Fee_Type)
begin
select q.Student_ID,q.Fee_Type,q.amount,q.amount as Current_month_due,q.amount-g.paid as previous_month_due,(q.amount-g.paid)+(q.amount) as total_amount from Student_Fee_Quotations q join(select d.Student_ID,d.Fee_Type,sum(d.amount) as paid from FeePaid_Details d where d.Student_ID=''PS20130001'' and d.Fee_Type=''Admission Fee'' and d.Month_Details=''jan'' group by d.Student_ID,d.Fee_Type )as g on q.Student_ID=''PS20130001'' and q.Fee_Type=''Admission Fee''
end
else if exists(SELECT Student_ID,Fee_Type, SUM(amount) AS paid FROM FeePaid_Details WHERE (Student_ID = ''PS20130001'') AND (Fee_Type = ''Admission Fee'') AND (Month_Details = ''feb'') GROUP BY Student_ID, Fee_Type)
begin
select q.Student_ID,q.Fee_Type,q.amount,q.amount-p.paid as Current_month_due,0 as previous_month_due,(q.amount-p.paid) as total_amount from Student_Fee_Quotations q join(select d.Student_ID,d.Fee_Type,sum(d.amount) as paid from FeePaid_Details d where d.Student_ID=''PS20130001'' and d.Fee_Type=''Admission Fee'' and d.Month_Details=''feb'' group by d.Student_ID,d.Fee_Type ) as p on q.Student_ID=''PS20130001'' and q.Fee_Type=''Admission Fee''
end
end
else
begin
select Student_ID,Fee_Type,amount,amount as Current_month_due,0 as previous_month_due,amount as total_amount from Student_Fee_Quotations where Student_ID=''PS20130001'' and Fee_Type=''Admission Fee''
end


这篇关于在if函数中面对SQL查询中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆