473,757 Members | 3,768 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Column as result of subtraction of two other columns?

Hi everyone,

I'm trying to calculate an output column which is the difference of two
other columns in the query output; the first column is an aggregate of
items in stock, while the second column is an aggregate of items which
have been used. The third column should should be the difference of the
two values so I can then output all three columns in a table.

Unfortunately I can't get this to work at the moment :(. I've simplified
the query down to the following:

dev=# select 1 as a, 2 as b, (b - a) as c;
ERROR: column "b" does not exist
dev=#

Do I need to create some form of alias so the calculation can see the
other columns? I am using PostgreSQL 7.4.2 on Linux.
Many thanks,

Mark.

---

Mark Cave-Ayland
Webbased Ltd.
Tamar Science Park
Derriford
Plymouth
PL6 8BX
England

Tel: +44 (0)1752 764445
Fax: +44 (0)1752 764446
This email and any attachments are confidential to the intended
recipient and may also be privileged. If you are not the intended
recipient please delete it from your system and notify the sender. You
should not copy it or use it for any purpose nor disclose or distribute
its contents to any other person.

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 23 '05 #1
5 13165
On Fri, Jul 16, 2004 at 15:31:33 +0100,
Mark Cave-Ayland <m.***********@ webbased.co.uk> wrote:
Hi everyone,

I'm trying to calculate an output column which is the difference of two
other columns in the query output; the first column is an aggregate of
items in stock, while the second column is an aggregate of items which
have been used. The third column should should be the difference of the
two values so I can then output all three columns in a table.

Unfortunately I can't get this to work at the moment :(. I've simplified
the query down to the following:

dev=# select 1 as a, 2 as b, (b - a) as c;
ERROR: column "b" does not exist
dev=#

Do I need to create some form of alias so the calculation can see the
other columns? I am using PostgreSQL 7.4.2 on Linux.


You can't use column aliases in other columns; you need to repeat the
column expressions.

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddres sHere" to ma*******@postg resql.org)

Nov 23 '05 #2

On 16/07/2004 15:31 Mark Cave-Ayland wrote:
Hi everyone,

I'm trying to calculate an output column which is the difference of two
other columns in the query output; the first column is an aggregate of
items in stock, while the second column is an aggregate of items which
have been used. The third column should should be the difference of the
two values so I can then output all three columns in a table.

Unfortunately I can't get this to work at the moment :(. I've simplified
the query down to the following:

dev=# select 1 as a, 2 as b, (b - a) as c;
ERROR: column "b" does not exist
dev=#

Do I need to create some form of alias so the calculation can see the
other columns? I am using PostgreSQL 7.4.2 on Linux.


I think you can use a sub-select (this works for me on 7.3.4):

select a, b, (b - a) as c from (select .... as a, .... as b from mytable)
as sub;

HTH

--
Paul Thomas
+------------------------------+---------------------------------------------+
| Thomas Micro Systems Limited | Software Solutions for
Business |
| Computer Consultants |
http://www.thomas-micro-systems-ltd.co.uk |
+------------------------------+---------------------------------------------+

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddres sHere" to ma*******@postg resql.org)

Nov 23 '05 #3
Try

select a, b, (b - a) as diff from (
select 1 as a, 2 as b
) as tmp;

John Sidney-Woollett

Bruno Wolff III wrote:
On Fri, Jul 16, 2004 at 15:31:33 +0100,
Mark Cave-Ayland <m.***********@ webbased.co.uk> wrote:
Hi everyone,

I'm trying to calculate an output column which is the difference of two
other columns in the query output; the first column is an aggregate of
items in stock, while the second column is an aggregate of items which
have been used. The third column should should be the difference of the
two values so I can then output all three columns in a table.

Unfortunate ly I can't get this to work at the moment :(. I've simplified
the query down to the following:

dev=# select 1 as a, 2 as b, (b - a) as c;
ERROR: column "b" does not exist
dev=#

Do I need to create some form of alias so the calculation can see the
other columns? I am using PostgreSQL 7.4.2 on Linux.

You can't use column aliases in other columns; you need to repeat the
column expressions.

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddres sHere" to ma*******@postg resql.org)


---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 23 '05 #4
Mark Cave-Ayland wrote:
Hi everyone,

I'm trying to calculate an output column which is the difference of two
other columns in the query output; the first column is an aggregate of
items in stock, while the second column is an aggregate of items which
have been used. The third column should should be the difference of the
two values so I can then output all three columns in a table.

Unfortunately I can't get this to work at the moment :(. I've simplified
the query down to the following:

dev=# select 1 as a, 2 as b, (b - a) as c;
ERROR: column "b" does not exist
dev=#

Do I need to create some form of alias so the calculation can see the
other columns? I am using PostgreSQL 7.4.2 on Linux.
You can can try:

select a, b, a-b from
( select sum( x) as a, sum( y) as b from whatever group by z);

You can also do:

select sum( x), sum( y), sum(x-y) from whatever group by z;

HTH

Many thanks,

Mark.

---

Mark Cave-Ayland
Webbased Ltd.
Tamar Science Park
Derriford
Plymouth
PL6 8BX
England

Tel: +44 (0)1752 764445
Fax: +44 (0)1752 764446
This email and any attachments are confidential to the intended
recipient and may also be privileged. If you are not the intended
recipient please delete it from your system and notify the sender. You
should not copy it or use it for any purpose nor disclose or distribute
its contents to any other person.

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 23 '05 #5
On Fri, 16 Jul 2004 12:04:54 -0400, Tom Lane <tg*@sss.pgh.pa .us> wrote:
select sum(x), sum(y), sum(x)-sum(y) from ...

At least since 7.4, the system will notice the duplicate aggregates
and run only two summations to compute the above, followed by a single
subtraction at the end. The apparently more intelligent way suggested
by Jean will have to run three summations, and thus end up being a net
loss.


Also note that Jean-Luc's
select sum( x), sum( y), sum(x-y) from whatever group by z;
gives a different result in the presence of NULLs.

Servus
Manfred

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #6

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

Similar topics

6
21893
by: Prit | last post by:
Hi everyone I guess this should be a simple question for the gurus I have a Data in a column which is to be places in 2 columns instead of one. How do i go about doing it in MS SQL server? Could someone please help me. I could do it in access with an update query but things are a little different in SQL server so I am a little lost. Eg. Name John?Doe
3
13366
by: olanorm | last post by:
I have a query where one or more of the columns returned is a result from a subquery. These columns get their own alias. I want to filter out the rows containing NULL from the subqueries but it just won't work. When running columnAlias IS NOT NULL i just get the error "Invalid column name 'columnAlias'. This is the query: SELECT k.UserId, k.Lastname, k.Firstname, (SELECT kscr.answer FROM Results kscr WHERE kscr.UserID =
0
1705
by: andrewcw | last post by:
After I made a nice application with WINFORM I tried to apply much of the same code, but there are lost of differences. I load the grid colors, column width, column header from a xml data file. But when I use datagrid.bind - it pulls in all the data to its own columns. Ex: gridTX.DataSource = ds.Tables; this.AttachXMLdrivenStyle(gridTX,mappTableName); gridTX.DataBind(); // my columns are not mapped over....but stand alone.
0
325
by: moondaddy | last post by:
using vb.net I have a datalist where the Repeat layout has: Columns 2 Direction Horizontal Layout Table I want to put a spacer column inbetween the 2 columns for better control of the distance between these 2 columns. Is this possible? And by the way, I find it a little difficult to control the spacing and alignment of template items (cells). For examle the right cell/column is a little lower than the left one.
1
2103
by: trizub | last post by:
I have a table of populations of cities (identified by country, state, cities) on particular dates. How do I transpose the date values in my rows to a date value column that lists populations for that date. In other words go from this: Date Country State City Population 12/20 US IL Flora 1432 12/19 US IL Flora 1427 12/18 US IL Flora 1425 12/20 US AR Flora 1432
2
5086
by: Dscar | last post by:
Hi, I am a beginner in ACCESS, I've imported data into access, and then realized that I need to split the information in one of my columns into 2 columns. the information looks like this: 00700400000000 00700400001230 00700400102000 I would like 007004 in one column and the rest of the info in another column. I know in excel I could do text to columns, however the is 20,000 records in access and can not export to excel and split the info. ...
2
5717
by: whitsey | last post by:
I have a a table which has the following columns: PART_NUMBER RESULT_TYPE ..... There are 10-15 different "result types", i.e. -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9... What I want is to produce the following type of output:
2
2610
by: nico3334 | last post by:
Hi, I have a SQL table that has data like this: Title Month Info 1 ---- 7 ---- 100 1 ---- 7 ---- 100 1 ---- 8 ---- 200 1 ---- 8 ---- 250 2 ---- 7 ---- 150
1
1112
by: miferdin | last post by:
Hi friends, 1. I have a datagrid with 5 columns, in this when i click edit in a row, all the columns changes into textbox but what i need is to make only a single column into a text box...how to do it... 2. How to edit the datagrid with single edit button above the datagrid and checkboxes in all the datarows. ie, by selecting the checkbox in a particular row and clicking the edit button should make that row editable...
1
3318
by: prasadgopathi | last post by:
hi friends, i have one table is normal column is bit id isnormal 1 0 2 0 3 0 4 1
0
9487
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
9904
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
9884
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
9735
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
8736
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
7285
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
6556
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2697
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.