473,385 Members | 1,409 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,385 software developers and data experts.

Subquery returning a column value

Does db2 support this subquery:

select b.time_ssno, sum(b.time_hours) as hours95,
(select sum(a.time_hours)
from p0230.pyrltime_time_v1 a
where time_date between '2005-01-01' and '2005-03-31'
and time_orgn = '2300'
and time_type = 'REG'
and a.time_ssno = b.time_ssno) as total_hours
from p0230.pyrltime_time_v1 b
where time_date between '2005-01-01' and '2005-03-31'

and time_orgn = '2300' and
time_sub_org ='95' and
time_type = 'REG'
group by time_ssno

Iam trying to determine the total hours within a speific category and
the total in all categories.

I have seen this done in SQL Server.

Thanks for any help, Margo
Nov 12 '05 #1
5 4877
ma**********@comcast.net wrote:
Does db2 support this subquery:

select b.time_ssno, sum(b.time_hours) as hours95,
(select sum(a.time_hours)
from p0230.pyrltime_time_v1 a
where time_date between '2005-01-01' and '2005-03-31'
and time_orgn = '2300'
and time_type = 'REG'
and a.time_ssno = b.time_ssno) as total_hours
from p0230.pyrltime_time_v1 b
where time_date between '2005-01-01' and '2005-03-31'

and time_orgn = '2300' and
time_sub_org ='95' and
time_type = 'REG'
group by time_ssno
What you have above in the SELECT-list is basically a scalar value
(time_ssno), an aggregate function (SUM) and another scalar value
(subselect). Now you only group by the first scalar value. How should DB2
know what to do with the second scalar value? You don't group by it and if
there are different values for each row in each group, there is no way to
know which value to pick. That's why you will get an error for the above
query.
Iam trying to determine the total hours within a speific category and
the total in all categories.


Have a look at ROLLUP here:
http://publib.boulder.ibm.com/infoce...n/r0000875.htm

I believe it does what you want to have.

--
Knut Stolze
Information Integration
IBM Germany / University of Jena
Nov 12 '05 #2
I think db2 support your query.
Please see the following query which has about same structure as yours.
SELECT b.workdept, SUM(b.salary) salary_F
, (SELECT SUM(a.salary)
FROM Employee a
WHERE edlevel between 15 and 18
AND a.workdept = b.workdept
) AS salary_total
FROM Employee b
WHERE edlevel between 15 and 18
AND b.sex = 'F'
GROUP BY workdept;
------------------------------------------------------------------------------

WORKDEPT SALARY_F SALARY_TOTAL

-------- ---------------------------------
---------------------------------
A00 52750.00
52750.00
C01 52220.00
52220.00
D11 73430.00
222100.00
D21 80800.00
128740.00
E11 56000.00
56000.00

5 record(s) selected.

Nov 12 '05 #3
Tonkuma wrote:
I think db2 support your query.
Please see the following query which has about same structure as yours.
SELECT b.workdept, SUM(b.salary) salary_F
, (SELECT SUM(a.salary)
FROM Employee a
WHERE edlevel between 15 and 18
AND a.workdept = b.workdept
) AS salary_total
FROM Employee b
WHERE edlevel between 15 and 18
AND b.sex = 'F'
GROUP BY workdept;


I big difference might be that the OP had a correlated subquery...

--
Knut Stolze
Information Integration
IBM Germany / University of Jena
Nov 12 '05 #4
Another way to do it may be:

select time_ssno, hours95m total_hours
from
(select time_ssno, time_sub_org, hours_sub as hours95,
sum(hours_sub) over(partition by time_ssno) as total_hours
from
(select time_ssno, time_sub_org, sum(time_hours) as hours_sub,
from p0230.pyrltime_time_v1 n
where time_date between '2005-01-01' and '2005-03-31'
and time_orgn = '2300'
and time_type = 'REG'
group by time_ssno, time_sub_org) AS T1
) AS T2
where time_sub_org ='95'

or even (seems to be too simple to be true.....)

select time_ssno,
sum(case when time_sub_org ='95'
then time_hours else 0 end) as hours95,
sum(time_hours) as total_hours
from p0230.pyrltime_time_v1 n
where time_date between '2005-01-01' and '2005-03-31'
and time_orgn = '2300'
and time_type = 'REG'
group by time_ssno

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #5
The case statment worked just fine. Thanks you all for your help.

Margo

Nov 12 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Nachi | last post by:
Hi, Urgent Help appreciated.... I am getting resultset with first condition and when try to get the resutlset from second condition i am getting the above error in SQL200. I know that i am...
0
by: Greg Stark | last post by:
Postgresql 7.4b2 (approximately, compiled out of CVS) When I have a subquery that has a complex subquery as one of the result columns, and then that result column is used multiple times in the...
4
by: Kenny G | last post by:
Below is a query that I currently have. I need to produce a subquery so that the top five of the CodeCount is returned. I appreciate your help. SELECT .PX_SURGEON, .PX_CODE, Count(.PX_CODE)...
4
by: sql_server_user | last post by:
Hi, I have a history table with about 400 million rows, with a unique composite nonclustered index on two columns (object id and time period) which is used for most of the queries into the...
8
by: kingskippus | last post by:
I don't know if this is possible, but I haven't been able to find any information. I have two tables, for example: Table 1 (two columns, id and foo) id foo --- ----- 1 foo_a 2 foo_b
5
by: Anne | last post by:
Hello! Here is the statement in question: --STATEMENT A SELECT * FROM dbo.myTable WHERE colX in (SELECT colX FROM dbo.sourceTable) The problem with Statement A is that 'colX' does not exist...
1
by: Aleck | last post by:
Hie. I have a trigger that monitors changes to my table fields but I get an error saying subquery returned more than one value.Below is the code for my trigger, hope you will figure out whats...
3
by: javakid | last post by:
Hi MySQL Gurus, I have a query as in following format: update tbl1, ( subquery )as tbl2 set tbl1.col1 = IFNULL(tbl2.col1,0) where tbl1.col2 = tbl2.col2Here, Actually i am returning a...
0
by: sqldood | last post by:
Hi, I'm in a scenario where I have to update and select the same value at the same time on a delete and update event on a gridview. Like on row delete, update table set column=(column-1)...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...

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.