Sql查询修改 [英] Sql Query Modification

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

问题描述

大家好,



祝大家好日子。我是SQL查询的新手,我想在现有的查询中提出一个问题。

  SELECT  
SUM (SPEND)SPEND1,
FROM
TIME
WHERE LTM = 1





现在我需要修改此查询以添加另一个名为 SPEND2 的列,但这一次,它的where子句不应该是 LTM = 1 ,它应该是

 L4M = 1< br /> 



我做不到 LTM = 1 L4M = 1 因为where子句将适用于 SPEND1 SPEND2

Spend1应该是 LTM = 1 而Spend2应该是

 L4M = 1< br /> 



他们应该在同一个查询中。



我想这样做:



 选择 
总和(支出) Spend1 其中 LTM = 1
总和(支出) as Spend2 其中 L4M = 1





我想感谢你的预先帮助,如果我的问题令人困惑,请告诉我。我会很乐意和不懈地澄清。

解决方案

你们已经非常接近...试试这个

 选择 '  Spend1'  as  SpendType,Sum(Spend) as 花费其中 LTM =  1  
UNION
选择 ' Spend2',Sum(支出)其中 L4M = 1



这将为您提供两行结果:



 SpendType支出
-----------------------
支出1 9999
支出2 9999


Hi Everyone,

a good day to all of you. I am a newbie in SQL queries and I would like to ask a question in an existing query I have.

SELECT 
 SUM (SPEND) SPEND1,
 FROM 
 TIME
 WHERE LTM = 1



Now I need to modify this query to add another column called SPEND2 but this time, it's where clause shouldn't be LTM=1, it should be

L4M = 1<br />


I can't do LTM=1 and L4M = 1 because the where clause will apply to both SPEND1 and SPEND2.
Spend1 where should be LTM=1 and Spend2 should be

L4M=1<br />


they should be in the same query.

I would like to do this :

Select
Sum (Spend) as Spend1 where LTM = 1
Sum (Spend) as Spend2 where L4M = 1



I want to thank you for your advance help and do let me know if my question is confusing and I will gladly and immidiately clarify.

解决方案

You were already very close ... try this

Select 'Spend1' as SpendType, Sum(Spend) as Spend where LTM = 1
UNION
Select 'Spend2', Sum(Spend) where L4M = 1


This will give you two rows of results:

SpendType         Spend
-----------------------
Spend1            9999
Spend2            9999


这篇关于Sql查询修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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