I want to add two columns values of my table and sort it in descending order. E.g:
int_id int_test_one int_test_2
1 25 13
2 12 45
3 25 15
Considering the table above, I want a SQL query which give me the result like below:
int_id sum(int_test_one,int_test_two)
2 57
3 40
1 38
Is there any sql query to do this?
There is not built in function for this kind of horizontal aggregation, you can just do...
SELECT INT_ID, INT_TEST_ONE + INT_TEST_TWO AS SUM FROM TABLE
Did you try what you describe? This works:
SELECT int_id , ( int_test_one + int_test_two ) as s FROM mytable ORDER BY s DESC
You can ommit the "as" keyword if you want.
Try this
SELECT
int_id,
(int_test_one + int_test_two) AS [Total]
FROM
mytable
ORDER BY
[Total] DESC
Related
My table contains 113 people.
48 of them are 20 years old. Now I am just selecting all people like
select * from persons
this will get me all persons, but 20 yr old are not the first 48 people.
I need the 20 yr old to be first 48 in 113 results.
something like
20 year ols ( 48 of them ), after that ..... all the rest in the table
How can I query this using PostgreSQL.
EDIT : there are age less than 20 too. after getting the first 48 , 20 yr olds, I dont care rest of the order I am getting the 48 to 113 people.
Just use order by :
select *
from persons
order by age
You can use asc or desc but because default is asc you do not need to put it in your example.
select *
from persons
order by age desc
After the comment from OP here is the new code(I do not know why but my firs assumption was that the value 20 is the lowest possible value... bad assumption):
select *
from persons
order by case when age = 20 then 1 else 2 end
OR
select *
from persons
order by (age = 20) desc
Here is a demo
If 20 is not your minimum age, you can use the CASE statement inside the ORDER BY clause, like this:
SELECT
*
FROM
persons
ORDER BY
CASE WHEN age = 20 THEN 0
ELSE 1
END ASC
I have a table like below.
Year AccountGroupA AccountGroup B RowSum
2004 15 23 38
2005 10 10 20
2006 8 15 23
2007 16 14 30
ColumnSum 49 62 111
I need grand sum of rows and colums ex:- I need result as 111 in sql query.
The Result needs to be returned from a sql function so ideally should be a single query.
Something like this:
select year,
sum(accountGroupA) as accountGroupA,
sum(AccountGroupB)as accountGroupb,
sum(accountGroupA + AccountGroupB) as rowsum
from the_table
group by year with rollup
This assumes that you only have one row per year or if you have more than one that you actually want to group by year. If neither is the case, you can use a union to achieve the same:
select year, accountGroupA, AccountGroupB, rowsum
from (
select year, accountGroupA, AccountGroupB, accountGroupA + AccountGroupB as rowsum, 0 as union_order
from the_table
union all
select null, sum(accountGroupA), sum(AccountGroupB), sum(accountGroupA + AccountGroupB), 1
from the_table
) t
order by union_order, year
SQLFiddle example: http://sqlfiddle.com/#!6/bd4bc/2
For the ColumnSum the SQL is easy:
SELECT sum(AccountGroupA), sum(AccountGroupB) from YOUR_TABLE
The row sums have to be coded like this:
SELECT (AccountGroupA + AccountGroupB) as RowSum from YOUR_TABLE
Forget about getting the desired result in one SQL statment. Although that is certainly possible with a nasty nested SQL I would not recommend it.
Look at using the ROLLUP Operator.
MSDN Reference: http://technet.microsoft.com/en-us/library/ms189305(v=sql.90).aspx
I've created a SQL fiddle here: http://sqlfiddle.com/#!6/df41d/7
More specifically, the code is here:
select
case when (Grouping(Year)) = 1
then 'ColumnSum'
else isnull(Year, 'Unknown') end as Year,
sum(AccountGroupA) as AccountGroupA,
sum(AccountGroupB) as AccountGroupB,
sum(AccountGroupA + AccountGroupB) as RowSum
from TestTable
group by Year with ROLLUP
I am dealing with a poorly designed database column which has values like this
ID cid Score
1 1 3 out of 3
2 1 1 out of 5
3 2 3 out of 6
4 3 7 out of 10
I want the aggregate sum and percentage of Score column grouped on cid like this
cid sum percentage
1 4 out of 8 50
2 3 out of 6 50
3 7 out of 10 70
How do I do this?
You can try this way :
select
t.cid
, cast(sum(s.a) as varchar(5)) +
' out of ' +
cast(sum(s.b) as varchar(5)) as sum
, ((cast(sum(s.a) as decimal))/sum(s.b))*100 as percentage
from MyTable t
inner join
(select
id
, cast(substring(score,0,2) as Int) a
, cast(substring(score,charindex('out of', score)+7,len(score)) as int) b
from MyTable
) s on s.id = t.id
group by t.cid
[SQLFiddle Demo]
Redesign the table, but on-the-fly as a CTE. Here's a solution that's not as short as you could make it, but that takes advantage of the handy SQL Server function PARSENAME. You may need to tweak the percentage calculation if you want to truncate rather than round, or if you want it to be a decimal value, not an int.
In this or most any solution, you have to count on the column values for Score to be in the very specific format you show. If you have the slightest doubt, you should run some other checks so you don't miss or misinterpret anything.
with
P(ID, cid, Score2Parse) as (
select
ID,
cid,
replace(Score,space(1),'.')
from scores
),
S(ID,cid,pts,tot) as (
select
ID,
cid,
cast(parsename(Score2Parse,4) as int),
cast(parsename(Score2Parse,1) as int)
from P
)
select
cid, cast(round(100e0*sum(pts)/sum(tot),0) as int) as percentage
from S
group by cid;
I am using SQLite and am trying to return the total of one column buy_price in the column TOTAL while at the same time returning all of the data. I do not want/need to group the data as I need to have the data in each returned row.
id date pool_name pool_id buy_price TOTAL
1 09/01/12 azp 5 20
2 09/02/12 mmp 6 10
3 09/03/12 pbp 4 5
4 09/04/12 azp 7 20
5 09/05/12 nyp 8 5 60
When I include something like SUM(buy_price) as TOTAL it only returns one row. I need all rows returned along with the total of all buy_price entries.
It sounds like this is what you are looking for:
select id,
dt,
pool_name,
pool_id,
buy_price,
(select sum(buy_price) from yourtable) total
from yourtable
see SQL Fiddle with Demo
Select * from yourtable
union
select 'Total',
' ',
' ',
' ',
sum(buy_price)
from yourtable
you can add a row on the bottom like this instead of adding a new column...
Is there a way to keep the order when using SELECT WHERE IN() in Sybase
There are two examples in mysql:
SELECT * FROM table WHERE id IN (118,17,113,23,72)
ORDER BY FIELD(id,118,17,113,23,72)
SELECT * FROM table WHERE id IN (118,17,113,23,72)
ORDER BY FIND_IN_SET(id, '118,17,113,23,72')
I need it in Sybase Ase.
You could break down the argument list with a case statement the explicitly assigns an ascending counter to the arguments and order according to that:
SELECT *
FROM sometable
WHERE id IN (118,17,113,23,72)
ORDER BY CASE id WHEN 118 THEN 1
WHEN 17 THEN 2
WHEN 113 THEN 3
WHEN 23 THEN 4
WHEN 72 THEN 5
END ASC
It's clunky as hell, but it should work.