The minimum of 3 columns (phpmyAdmin SQL database) - sql

I have 3 integer columns on the same table that I need to select only the minimum on a select
(price 1) (price 2) (price 3)
result1: 3 4 5
result2: 8 1 9
.
How I can get only the minimum number of each result?
I work with phpMyAdmin.
thanks

Use the least function
select result, least (price1,price2,price3) from yourtable

try this
select least('price1','price2','price3') from table1

SELECT LEAST(price1, price2, price3)
FROM Table

Related

hoe to make sum of one sql table column with ignoring duplicate values?

quoteId price
1 50
1 50
2 10
3 40
3 40
3 40
4 10
In this table I always get the same price for each quoteId.
Example: quoteId = 1 has multiple entries with the price of 50.
By using SUM I get the total sum of the price column which is:50 + 50 + 10 + 40 + 40 + 40 + 10 = 240
However, I only want to sum the unique price for quoteId which is:
50+10+40+10 = 110
How can I approch this?
Another option is DISTINCT
Example
Select MyTotal = sum(price)
from (Select Distinct quoteId,price From YourTable) A
Returns
MyTotal
110
Following query will work:
select sum(price)
from yourTablename
group by quoteId,price;
You need a nested query to compute an intermediate value by quoteId using avg ( or max or min with your data)
But you need know why you have duplicate value by quotedId, may be you have a mistake before.
select sum(price) from (
select
quoteId,
avg(price) price,
from
your_table
group by
quoteId
) as x
This query is compliant with ISO standard SQL and will works with several database engine

Querying Data Backward in ORACLE SQL

I have a simple question regarding oracle sql. So i have this table
WEEKNUM DATA
1 10
2 4
3 6
4 7
So i want to make a view that shows like this,
WEEKNUM DATA ACCUM_DATE
1 10 10
2 4 14
3 6 20
4 7 27
I spend hours on this simple one but couldnt get any luck
thanks a lot
SELECT weeknum,
data,
sum(data) over (order by weeknum) accum_data
FROM your_table_name
should work. I'm using the sum analytic function here and assuming that you want to start with the smallest weeknum value and keep increasing the running total as the weeknum values increase. I'm also assuming that you never want to reset the accumulated sum. If you're trying to do something like generating an accumulated sum that restarts each year, you'd want to add a partition by to the analytic function.
You could use a Cross JOin in this case
Query:
select
A.WEEKNUM
, A.DATA
, SUM(B.DATA) DA
from table1 A
cross join table1 B
WHERE A.WEEKNUM>=B.WeekNUM
GROUP BY A.WEEKNUM
, A.DATA
order by A.WEEKNUM
Result:
WEEKNUM DATA DA
1 10 10
2 4 14
3 6 20
4 7 27
Thanks guys but i just found out this method works perfectly,
OVER (ORDER BY WEEKNUM ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS CUMULATIVE_WEIGHT
Or use a sub-select to calculate:
select WEEKNUM, DATA, (select sum(DATA) from tablename t2
where t2.weeknum <= t1.weeknum) as ACCUM_DATE
from tablename t1

PL SQL How to eliminate duplication with two columns in a table and

There is a similar question already asked, but I have couple of differences
I have this table>>
No1 No2 Data Customer
1 2 01.01.2013 120000
2 1 01.02.2013 100000
3 4 03.06.2011 150000
4 3 05.09.2010 160000
5 6 15.02.2013 110000
6 5 29.06.2014 190000
1 6 19.05.2013 100000
6 1 04.08.2013 120000
9 2 01.07.2011 100000
What I want is to eliminate rows that have the same value but in the other field. For me No1=1 No2=2 and No1=2 No2=1 is the same thing. So when this sort of combination ocures it should give back just one row.
At the end I want this as result>>
No1 No2 Data Customer
1 2 01.01.2013 120000
3 4 03.06.2011 150000
5 6 15.02.2013 110000
1 6 19.05.2013 100000
9 2 01.07.2011 100000
I have found a solution but just for the first two columns
select distinct least(no1, no2), greatest(no1, no2)
from t
but I need the 'Data' and 'Customer' columns also
if I try with
select distinct least(no1, no2), greatest(no1, no2), max(Data), max(Customer)
from t
it will give me the maximal from Data and Customer but I want the values to correspond/match the row..
My real table is actually select from lots of tables with subqueries, so getting a result is time consuming thing, that's why I want the simplest and fastest solution.
Any hint/advice is appreciated
thanks
IV
--EDIT--/6 Hours later/
I forgot to mention that I have rows which are single without combination like the one I just puted in the original table(the last one outside the table :) )
so I found hear one answer that helped me do this
select t1.*
from MyTable t1
left outer join MyTable t2 on t1.No1 = t2.No2 and t1.No2 = t2.No1
where t2.No2 is null --- this will give me the singles
or t1.No1 <= t2.No1 --- and this will give me one row where the combination occures
thanks a lot all of you
IV
Something like this should work with Oracle. Don't have a db to test, so it will contain syntax errors.
select * from (
select least(no1, no2), greatest(no1, no2), Data, Customer,
rank() over (partition by least(no1, no2), greatest(no1, no2)
order by Customer desc) r
from t
) where r = 1
select *
from the_table
where (no1, no2) in (select distinct least(no1, no2), greatest(no1, no2)
from the_table);
This would however return duplicates if the combination (no1, no2) is not unique e.g. if there is more than one row with no1=1 and no2=2
You can use MINUS, this will get the last not coupled row
SELECT *
FROM Table1
WHERE (No1, No2) IN (SELECT No1, No2
FROM Table1
MINUS
SELECT No2, No1
FROM Table1
WHERE No1 < No2);
SQLFiddle demo

SQLite: Get Total/Sum of Column

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...

sql query adding column values

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