473,783 Members | 2,287 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Summing a column as a seperate column in the same query

1 New Member
Hi all, im fairly new to SQL and im stuck half way through a query,
im using DB2 here is what im tryng to do.
i have a query that brings back an item number , shelf req, sum of all orders columns, based on the shelf req column the item number column has more than one row for the same item number therefore my sum of all orders is summing them based on shelf req which i need, what i cant get is how to keep this info plus get the sum of all orders for each item number reqardeless in the same query.
in another word, how do i get the sumallordrs column to sum Sumofallordrs column as seperate column.

ITEM Shelf Req Sumofallordrs SumallItems
1111 20 300 ?
2222 10 200
3333 25 75
1111 25 100
Jul 23 '07 #1
3 3561
Purple
404 Recognized Expert Contributor
Hi NewlytoSQL and welcome to TSDN DB2 Forum

I have moved your post the from Articles into the forum

Can you post the SQL query you have so far to allow the experts a better opportunity of providing a relevant answer.

Regards Purple

Moderator
Aug 15 '07 #2
cburnett
57 New Member
If I understand you correctly, I think the following is what you are after. The key feature here is the ROLLUP function which subtotals the orders by item number. This is the row with the null shelf_req. If you want some other value, use the coalesce function (e.g. coalesce(shelf_ req,9999) in the select list.

Note that you can ignore everything up to the select statement - the with clause just creates a temporary tabe for testing the query out.

Expand|Select|Wrap|Line Numbers
  1. with orders(item, shelf_req, order) as (values (1111,20,100),(1111,20,200),(1111,25,100),(2222,10,200),(3333,25,50),(3333,25,25))
  2. select item, shelf_req, sum(order) as sumofallordrs
  3.   from orders
  4.  group by item, rollup(shelf_req)
  5.  order by 1,2 
Aug 16 '07 #3
cburnett
57 New Member
Oh, and if you want the data on the same row, then you need to use OLAP functions:

Expand|Select|Wrap|Line Numbers
  1. with orders(item, shelf_req, order) as (values (1111,20,100),(1111,20,200),(1111,25,100),(2222,10,200),(3333,25,50),(3333,25,25))
  2. select distinct item, shelf_req, sum(order) over(partition by item, shelf_req) as sumbyshelf, sum(order) over(partition by item) as sumbyitem
  3.   from orders
  4.  order by 1,2 
Aug 16 '07 #4

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

Similar topics

2
3921
by: Joe Gazda | last post by:
I'm a relative newbie to PHP, but have been able to put together some PHP code to generate a CSV/XLS file from a Query result. Now, I would like to include custom column names instead of the MySQL column table names. I know that there are codes to generate tabs and carriage returns, but can't find anything about including "commas" in a string to output to the file to separate the custom field names. I'd appreciate some help with a line of...
11
17638
by: csomberg | last post by:
SQL Server 2000 I will to compare a normal table with a replicated audit table having identical columns. I wish to report on the differences between the data. How can I loop though a "column list" in TSQL rather than explicitly naming each column to compare ? Thanks,
6
54318
by: deko | last post by:
Can I reference a column other than the bound column of a ComboBox in a query? SELECT ... WHERE CtlID = Forms!frmMain!cbxCtlID.Column(1); (this does not seem to work) Or must I add a hidden text box on frmMain and use this: Me.txtCtlID = Forms!frmMain!cbxCtlID.Column(1)
0
1722
by: ChuckB | last post by:
How would one fill a datagrid column by column? I have a function that propagates an array, as array1 runs I want to have the values fill column 1, when it completes, array2 runs and fills column 2... so when it's complete row 1 will be array1 in column 1, and array2 will be in column 2. I can't seem to figure out the code to write to a specific column, and not a whole row.
0
1627
by: Rudi Hausmann | last post by:
Hi! I have a column with a date. In another column I want to show the weekday of this date. I have following code: <asp:BoundField DataField="myDate" HeaderText="WeekDay" SortExpression="weekday" DataFormatString="{0:dddd}" HtmlEncode="False" ApplyFormatInEditMode="True" /> <asp:BoundField DataField="myDate" HeaderText="Day" SortExpression="weekday" DataFormatString="{0:d}" HtmlEncode="False"
2
1636
by: JHNielson | last post by:
I Know I've posted an Urgent message before. But I'm in the middle of system testing, and these little stupid bugs are killing me...... I have a query that checks that a set of values (the values in 6 fields) are actually equal to the total they submitted on the form. The fields are set to Double with Auto for # decimal places. But I've also tried it with them set to 2-Decimal places. The SQL query looks like this: UPDATE ETL_TBL...
16
2159
by: kinkin83 | last post by:
Dear All, i would like to get different result from 1 column in 1 query. the details result is below database name: Data table name: table1 column: id, phoneno, timeframe, mo if MO=1 means MO else MO=0 means MT
25
2545
by: Darsin | last post by:
Hi all I need to perform a summation on a column of a table based on a criteria given in another column in the same table. The catch is i need to perform different sums according to the number of criterias in the criteria column, in a single query. the structure of the table is somethinmg like this (only relevant columns are shown) TABLE1 Value - numeric(20,6) Month - int
0
1216
by: dehneli | last post by:
I need to take data from listview, column by column, how cant I do that?
0
9643
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10081
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9946
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2875
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.