SELECT with where子句条件

时间:2014-04-23 16:10:09

标签: mysql sql oracle

在我的sql oracle表中,我有类似的东西

CREATE TABLE Project (
P#      NUMBER(10)  NOT NULL, /* Project number     */
PTitle      VARCHAR2(30)    NOT NULL, /* Project title      */
Sponsor     VARCHAR2(30),         /* Project sponsor name   */
D#      NUMBER(5)   NOT NULL, /* Department number      */
Budget      NUMBER(10,2)    NOT NULL, /* Project budget     */
CONSTRAINT Project_PK PRIMARY KEY(P#),
CONSTRAINT Project_FK FOREIGN KEY (D#) REFERENCES Department(D#),
CONSTRAINT Project_CK UNIQUE (PTitle)
);

INSERT INTO Project VALUES (1001, 'Computation', 'Microsoft', 1, 25000);
INSERT INTO Project VALUES (1002, 'Study methods', 'Education committee', 3, 15000);
INSERT INTO Project VALUES (1003, 'Racing car', 'Cloud Pty Ltd', 3, 225000);
INSERT INTO Project VALUES (1004, 'Football', 'Football club', 5, 35000);
INSERT INTO Project VALUES (1005, 'Swimming', 'Education committee', 5, 125000);

我必须为每个赞助商选择赞助商名称和total_budget。 所以当我选择赞助商,预算时。我应该在什么条件下让教育委员会只展示一次,预算只显示(15000 + 125000)= 140000? 输出应该像

 SPONSOR           total_budget
 -------------------------------
Education Commitee    140000
Cloud Pty Ltd         225000
etc....

2 个答案:

答案 0 :(得分:2)

这是GROUP BY运营商的典型用例:

SELECT sponsor, sum(budget) as total_budget
FROM Project
GROUP BY sponsor

答案 1 :(得分:0)

试试这个:

 SELECT sponsor_name,sum(total_budget) from tablename group by  sponsor_name 

探索“分组依据” http://www.w3schools.com/sql/sql_groupby.asp