473,386 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Combine records into one by specific columns not all , not Group By

dlite922
1,584 Expert 1GB
The problem i'm facing I have not come across on Google or don't know what it's called, so instead of explaining what i'm trying to do, showing you is a lot better.

I have a table that calculates hours for a specific activity.

Expand|Select|Wrap|Line Numbers
  1. activity---------type------------hours
  2. AB123         NORMAL     8.3
  3. AB123         BREAK       0.4
  4. AB123         NORMAL     4.5
  5. CD456         NORMAL     3.7
  6. CD456         BREAK       2.1

What I want to do is group by the activity, BUT display the type on different columns like so (Wanted Output):

Expand|Select|Wrap|Line Numbers
  1. Activity       break         total
  2. AB123        0.4            13.2   
  3. CD456        2.1            5.8

The break is the sum of ALL TYPE = BREAK, while total is just SUM of all hours regardless of type.

Is this even possible without procedures?

Brain-locked here guys, appreciate any eye-openers,


Cheers,




Dan
Oct 10 '08 #1
3 1901
dlite922
1,584 Expert 1GB
More INFO:

Here's the table:

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. SELECT activity, type, hours FROM timeSheet;
  4. +----------+--------+-------+
  5. | activity | type   | hours |
  6. +----------+--------+-------+
  7. | BGRBB    | NORMAL |   7.1 |
  8. | CDRBB    | BREAK  |   1.2 |
  9. | BGRBB    | BREAK  |   0.5 |
  10. | CDRBB    | NORMAL |   5.9 |
  11. +----------+--------+-------+
  12. 4 rows in set (0.00 sec)
  13.  
  14.  

Here's my effort:

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT 
  3. activity, 
  4. ROUND((SELECT SUM(hours) FROM timeSheet WHERE type = 'BREAK'),2) AS "Total Break", 
  5. ROUND(SUM(hours),2) AS "Total Hours" 
  6. FROM timeSheet 
  7. GROUP BY activity;
  8. +----------+-------------+-------------+
  9. | activity | Total Break | Total Hours |
  10. +----------+-------------+-------------+
  11. | BGRBB    |        1.70 |        7.60 |
  12. | CDRBB    |        1.70 |        7.10 |
  13. +----------+-------------+-------------+
  14.  
  15.  
As you can see they both show 1.7 for break time although BGRBB should have only 0.5 break time and CDRBB should have 1.2 as the break time.

I guess I need a way to tell the inner select to grab the activity it should sum on.

I hope that helps further explain what I'm attempting to do.

Temporary tables?




Dan
Oct 10 '08 #2
Atli
5,058 Expert 4TB
You can use a Correlated Subquery.
That's basically a subquery that references the outer table.

Like:
Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.     tsout.activity,
  3.     (    SELECT SUM(tssub .`hours`) 
  4.         FROM timeSheet AS tssub 
  5.         WHERE tsout.activity = tssub.activity
  6.         AND tssub.`type` = 'BREAK'
  7.     ) AS 'Break',
  8.     SUM(tsout.`hours`) AS 'Total'
  9. FROM timeSheet AS tsout
  10. GROUP BY tsout.activity
  11.  
Oct 11 '08 #3
dlite922
1,584 Expert 1GB
You can use a Correlated Subquery.
That's basically a subquery that references the outer table.

Like:
Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.     tsout.activity,
  3.     (    SELECT SUM(tssub .`hours`) 
  4.         FROM timeSheet AS tssub 
  5.         WHERE tsout.activity = tssub.activity
  6.         AND tssub.`type` = 'BREAK'
  7.     ) AS 'Break',
  8.     SUM(tsout.`hours`) AS 'Total'
  9. FROM timeSheet AS tsout
  10. GROUP BY tsout.activity
  11.  

Thank you! I'll try this.


UPDATE: Worked like a charm!
Oct 12 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Martin Feuersteiner | last post by:
Dear Group I wonder whether you can give me a syntax example for a SQL Statement. Lets assume I've a table containing three columns ContactID (Primary Key), Firstname and Lastname. I would...
8
by: mark | last post by:
Access2000 How do I write a query that combines the CTC field from each record below into one record? I need to concatenate the CTC field with a separator, like below: ...
1
by: mojo | last post by:
We have set up an Access database with tables x,y, & z where we would like to have multiple people entering data into a table x. Each person has an identical copy of the database on their PC's. ...
24
by: clare at snyder.on.ca | last post by:
I have a SQL query I need to design to select name and email addresses for policies that are due and not renewed in a given time period. The problem is, the database keeps the information for every...
2
by: Carroll | last post by:
I'm looking for a way in SQL to find duplicate records in a single table, that are the same based on 3 columns, regardless of what is in the other columns in the duplicate records. I would like to...
1
by: NumberCruncher | last post by:
Hi All, I am struggling with setting up my first system of tables, forms,and reports, and could use your help! I am setting up a database to keep track of the production of a produced item. The...
4
by: rn5a | last post by:
A MS-Access DB has 3 tables - Teacher, Class & TeacherClass. The Teacher table has 2 columns - TeacherID & TeacherName (TeacherID being the primary key). The Class table too has 2 columns - ClassID...
2
denny1824
by: denny1824 | last post by:
Hi everyone, Here is my problem. I have a Table Variable (I could easily turn it into a Temp Table instead) that will sometimes have rows that are identical except for one specific column. The...
1
by: bluereign | last post by:
Thank you for your assistance. I am a novice looking to JOIN and append or combine records from 2 current Tables into 2 new Tables named below. I have been able to JOIN Tables with the script...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.