Monday, March 19, 2012
Add two result values to a new result?
This is probably simple (or impossible) but I can't figure out how to add to
values into 1 in a result.
Something like this:
select
sum( lala ) as sum_1,
sum( lele ) as sum_2,
add( sum_1 + sum_2) as sum_tot
from myTable
I could ofcourse use lala and lele in a new sum statement, but both can be
quite complicated statements and I thought there might be an easier way
around?
Regards,
PeterSELECT sum_1,
sum_2,
sum_1+sum_2 As sum_tot
FROM
(SELECT sum(lala) as sum_1,
sum (lele) as sum_2
FROM mytable ) X
"Peter Hartln" <peter@.data.se> wrote in message
news:ugHGLt$JGHA.2828@.TK2MSFTNGP12.phx.gbl...
> Hi!
> This is probably simple (or impossible) but I can't figure out how to add
> to values into 1 in a result.
> Something like this:
> select
> sum( lala ) as sum_1,
> sum( lele ) as sum_2,
> add( sum_1 + sum_2) as sum_tot
> from myTable
> I could ofcourse use lala and lele in a new sum statement, but both can be
> quite complicated statements and I thought there might be an easier way
> around?
> Regards,
> Peter
>
>|||Hi
CREATE TABLE Test
(
col1 INT,
col2 INT
)
INSERT INTO Test VALUES (1,5)
INSERT INTO Test VALUES (20,30)
CREATE FUNCTION dbo.calc_sum
(
@.arg1 AS int,
@.arg2 AS int
)
RETURNS int
AS
BEGIN
RETURN (SELECT @.arg1+@.arg2)
END
GO
SELECT SUM(col1),SUM(col2),dbo.calc_sum(SUM(col1),SUM(col2))
FROM Test
"Peter Hartln" <peter@.data.se> wrote in message
news:ugHGLt$JGHA.2828@.TK2MSFTNGP12.phx.gbl...
> Hi!
> This is probably simple (or impossible) but I can't figure out how to add
> to values into 1 in a result.
> Something like this:
> select
> sum( lala ) as sum_1,
> sum( lele ) as sum_2,
> add( sum_1 + sum_2) as sum_tot
> from myTable
> I could ofcourse use lala and lele in a new sum statement, but both can be
> quite complicated statements and I thought there might be an easier way
> around?
> Regards,
> Peter
>
>|||"Peter Hartln" <peter@.data.se> wrote in message
news:ugHGLt$JGHA.2828@.TK2MSFTNGP12.phx.gbl...
> Hi!
> This is probably simple (or impossible) but I can't figure out how to add
> to values into 1 in a result.
> Something like this:
> select
> sum( lala ) as sum_1,
> sum( lele ) as sum_2,
> add( sum_1 + sum_2) as sum_tot
> from myTable
> I could ofcourse use lala and lele in a new sum statement, but both can be
> quite complicated statements and I thought there might be an easier way
> around?
> Regards,
> Pete
The easiest way would be to do the addition in the client application.
Otherwise, no, you have to repeat the SUMs.|||Try this
SELECT sum_lala, sum_lele, sum_lala + sum_lele AS sum_lala_lele
FROM (SELECT SUM(lala) AS sum_lala, SUM(lele) AS sum_lele
FROM andys_test_table) summary
Monday, February 13, 2012
Add a column to a table.
I know this should be stupidly easy to do but I'm pulling my hair out trying to figure out how to write a SQL query to add a column to an existing table with SQL Server 2005. Every single reference I've found says to use "ALTER TABLE", yet when I do SQL says...
"The ALTER TABLE SQL construct or statement is not supported."
So how the $%^$ am I supposed to add a column? I've wasted about 3 hours on this simple task and it's really starting to piss me off.
Hi,
from where are you firing your query ? iring the query from the query ane of the Managment studio just works fine for me with the following script.
USE ADVENTUREWORKS
GO
DROP TABLE SomeTable
GO
CREATE TABLE SomeTable
(
SomeColumn INT
)
ALTER TABLE SomeTable
ADD SomeCOlumn2 INT
HTH, Jens Suessmeyer.
http://www.sqlserver2005.de
That's where it gives me the error. A simple statement like this does it.
ALTER TABLE History ADD Mar06 int
Even if I just do this query it says it too.
ALTER TABLE History
In Management Studio it pops up the error message and gives me a choice of clicking "Continue" or "Cancel". If I click "Contine" it does the query and it works, but if I do the exact same query from a PHP script it doesn't work, even through the rest of the queries in the script work just fine. I don't get it at all. The full query that I want to run is...
ALTER TABLE History ADD Mar06 int DEFAULT 0 NOT NULL
Edit: Okay, I finally figured it out and it seems the user I was logged in as didn't have ALTER permissions. That error message was making me think it was something wrong with my query so that's what I was focused on fixing. It looks to be working fine now, though that error message still pops up.