How to make derived column in Oracle and then use it? - oracle10g

How I can make/declare/define a derived column in select query and then use it in where clause?

To define a column in an SQL query, you can use pretty much any SQL operation that returns a single value (including select statements). Here are some examples:
select 'Y' from dual;
select (5 * 3) cal_col from dual;
select (select min(col1) from table 2) calc_col from dual;
select nvl(col1, 'N') has_value from mytable;
From my experience, if you want to use a derived column in a select query, then you must define the column as part of an inner select. Here is an example:
select *
from (
select (col1 * col2) calc_col
from mytable
) data
where data.calc_col > 30
Another alternative is use the calculation within the where clause itself:
select (col1 * col2) calc_col
from mytable t
where (col1 * col2) > 30
If you are performing a count(*) operation, then you can also leverage the HAVING clause:
select field1, count(*)
from mytable
having count(*) > 3

Related

what's the outcome of select *, count(*) from aTable

Wonder what would happen if i select both * and an aggregate together.
Will it be just one row or multiple rows?
example:
select *, count(*) from employee

Sum of a column within the subquery in Postgresql

I have a Postgresql table where I have 2 fields i.e. ID and Name ie column1 and column2 in the SQLFiddle. The default record_count I put for a particular ID is 1. I want to get the record_count for column 1 and sum that record_count by column1.
I tried to use this query but somehow its showing some error.
select sum(column_record) group by column_record ,
* from (select column1,1::int4 as column_record from test) a
Also find the Input/Output screenshot in the form of excel below :
SQL Fiddle for the same :
http://sqlfiddle.com/#!15/12fe9/1
If you're using a window function (you may want to use normal grouping, which is "a lot" more faster and performant), this is the way to do it:
-- create temp table test as (select * from (values ('a', 'b'), ('c', 'd')) a(column1, column2));
select sum(column_record) over (partition by column_record),
* from (select column1, 1::int4 as column_record from test) a;

SQL Server SUM() for DISTINCT records

I have a field called "Users", and I want to run SUM() on that field that returns the sum of all DISTINCT records. I thought that this would work:
SELECT SUM(DISTINCT table_name.users)
FROM table_name
But it's not selecting DISTINCT records, it's just running as if I had run SUM(table_name.users).
What would I have to do to add only the distinct records from this field?
Use count()
SELECT count(DISTINCT table_name.users)
FROM table_name
SQLFiddle demo
This code seems to indicate sum(distinct ) and sum() return different values.
with t as (
select 1 as a
union all
select '1'
union all
select '2'
union all
select '4'
)
select sum(distinct a) as DistinctSum, sum(a) as allSum, count(distinct a) as distinctCount, count(a) as allCount from t
Do you actually have non-distinct values?
select count(1), users
from table_name
group by users
having count(1) > 1
If not, the sums will be identical.
You can see for yourself that distinct works with the following example. Here I create a subquery with duplicate values, then I do a sum distinct on those values.
select DistinctSum=sum(distinct x), RegularSum=Sum(x)
from
(
select x=1
union All
select 1
union All
select 2
union All
select 2
) x
You can see that the distinct sum column returns 3 and the regular sum returns 6 in this example.
You can use a sub-query:
select sum(users)
from (select distinct users from table_name);
SUM(DISTINCTROW table_name.something)
It worked for me (innodb).
Description - "DISTINCTROW omits data based on entire duplicate records, not just duplicate fields." http://office.microsoft.com/en-001/access-help/all-distinct-distinctrow-top-predicates-HA001231351.aspx
;WITH cte
as
(
SELECT table_name.users , rn = ROW_NUMBER() OVER (PARTITION BY users ORDER BY users)
FROM table_name
)
SELECT SUM(users)
FROM cte
WHERE rn = 1
SQL Fiddle
Try here yourself
TEST
DECLARE #table_name Table (Users INT );
INSERT INTO #table_name Values (1),(1),(1),(3),(3),(5),(5);
;WITH cte
as
(
SELECT users , rn = ROW_NUMBER() OVER (PARTITION BY users ORDER BY users)
FROM #table_name
)
SELECT SUM(users) DisSum
FROM cte
WHERE rn = 1
Result
DisSum
9
If circumstances make it difficult to weave a "distinct" into the sum clause, it will usually be possible to add an extra "where" clause to the entire query - something like:
select sum(t.ColToSum)
from SomeTable t
where (select count(*) from SomeTable t1 where t1.ColToSum = t.ColToSum and t1.ID < t.ID) = 0
May be a duplicate to
Trying to sum distinct values SQL
As per Declan_K's answer:
Get the distinct list first...
SELECT SUM(SQ.COST)
FROM
(SELECT DISTINCT [Tracking #] as TRACK,[Ship Cost] as COST FROM YourTable) SQ

Calling a function on every row returned by a subquery

I need to run the following query to extract the values of my raster records in a specific point.
select st_value((select rast from mytable),
(select st_GeomFromText('POINT(30.424 -1.978)', 4326)))
But I encounter with the following error:
ERROR: more than one row returned by a subquery used as an expression
SQL state: 21000
It needs just one record for this function but I need to extract values of all of records.
If a subquery returns multiple rows, you must either use it in a common table expression (CTE / WITH query) and FROM alias, or use FROM (SELECT ...) alias. In this case, though, it looks like it's simpler than that:
select st_value(rast, st_GeomFromText('POINT(30.424 -1.978)', 4326))
FROM mytable;
Both subqueries appear to be unnecessary.
If you truly needed the subquery you'd write something syntactically like:
WITH sq(rast) AS ( SELECT rast FROM mytable )
SELECT st_value(rast, st_GeomFromText('POINT(30.424 -1.978)', 4326))
FROM sq;
or
SELECT st_value(rast, st_GeomFromText('POINT(30.424 -1.978)', 4326))
FROM (SELECT rast FROM mytable) sq(rast);
Try:
Select st_value(rast),
st_GeomFromText('POINT(30.424 -1.978)', 4326)
from mytable
If you have a function with multiple columns, you can do something like this
SELECT (info).column1, (info).column2, (info).column3
FROM (select st_value(rast, st_GeomFromText('POINT(30.424 -1.978)', 4326)) AS info
FROM mytable
) AS foo

Select distinct from another select results

I want to select distinct results from another select statement results
e.g;
select distinct from(select * from table)
following is result of inner select
testing department 9998901036 GOLD
testing department 9998901036 GOLD
I want to get distinct from above select result.
From your example, you could just do
select distinct * from table
But say you had some scenario where you wanted to distinct on some other results set, you could do
select distinct column1, column2 from (select * from table) T
Note that you have to alias your inner select
select distinct *
from
(select * from table) t
Works - You just need to give your sub select a table alias.
You can also use a CTE.
;WITH t AS
(
SELECT *
FROM table
)
SELECT DISTINCT *
FROM t