Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
389 views
in Technique[技术] by (71.8m points)

mysql - SQL GROUPBY & SUM OF A COLUMN

I have a table with the below structure

+------------+--------+------------------+-----+
|    E_DATE    | CLIENT |       TIME       | INS |
+------------+--------+------------------+-----+
| 2015-06-04 | comA   | 0.00478515625    |  a1 |
| 2015-06-04 | comA   | 0.0025390625     |  a1 |
| 2015-06-04 | comA   | 0.0              | a1  |
| 2015-06-04 | comA   | 0.0              | a1  |
| 2015-06-04 | comB   | 0.0115234375     | a2  |
| 2015-06-04 | comB   | 1.953125E-4      | a2  |
| 2015-06-04 | comB   | 0.0103515625     | a3  |
| 2015-06-04 | comB   | 0.0              | a3  |
| 2015-06-05 | comA   | 0.00478515625    | a4  |
| 2015-06-05 | comA   | 0.0025390625     | a4  |
| 2015-06-05 | comA   | 0.0              | a1  |
| 2015-06-05 | comA   | 0.0              | a2  |
| 2015-06-05 | comB   | 0.010351         | a1  |
| 2015-06-05 | comB   | 0.05625          | a1  |
+------------+--------+------------------+-----+

I am looking to get the following output -

+------------+--------+-----+-----------------------------------------------------------+ 
|    E_DATE    | CLIENT | INS |                        TOTAL_TIME                         |
+------------+--------+-----+-----------------------------------------------------------+
| 2015-06-04 | comA   | a1  | SUM of TIME for a1 for comA for the date in 'E_DATE' column |
| 2015-06-04 | comB   | a2  | SUM of TIME for a2 for comA for the date in 'E_DATE' column |
| 2015-06-04 | comB   | a3  | SUM of TIME for a3 for comA for the date in 'E_DATE' column |
| 2015-06-05 | comA   | a1  | SUM of TIME for a1 for comA for the date in 'E_DATE' column |
| 2015-06-05 | comA   | a2  | SUM of TIME for a2 for comA for the date in 'E_DATE' column |
| 2015-06-05 | comA   | a4  | SUM of TIME for a2 for comA for the date in 'E_DATE' column |
| 2015-06-05 | comB   | a1  | SUM of TIME for a1 for comA for the date in 'E_DATE' column |
+------------+--------+-----+-----------------------------------------------------------+

Is this the right query to achieve this?

select E_DATE, CLIENT,INS,SUM(INS) AS TOTAL_TIME GROUP BY E_DATE,CLIENT
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If I understood it correctly then this should be the answer otherwise I/we will be needing the actual figures of TOTAL_TIME column in your expected result :

SELECT E_DATE,
       CLIENT,
       INS,
       (SELECT SUM(time)
        FROM   mytable b
        WHERE  a.E_DATE = b.E_DATE
               AND a.INS = b.INS
               AND b.CLIENT = 'comA') TOTAL_TIME
FROM   mytable a
GROUP  BY E_DATE,CLIENT,INS 

NOTE : Assumed that INS in 2nd last row is a4 as per logic.

SQL Fiddle


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...