SQL query to find greatest in columns and rows - sql

How does one in oracle find the greatest of three columns in say 10 rows?
The requirement is I have three dates column and I have need to find greatest of three columns in 10 rows. I know greatest will find in one row.
How?

How about
select max(greatest(date1, date2, date3, date4)) from my_table;

you can use greatest again in your order by
select * from (
select greatest(c1,c2,c3,c4) from mytable
order by greatest(c1,c2,c3,c4) desc
) t1 where rownum = 1

With data as(
Select col1 dt from table union all
Select col2 from table union all
Select col3 from table union all
Select col4 from table
)
Select max(dt) max_dt
from data
/
Assuming the 4 columns are DATE data type.
It only uses MAX, and not GREATEST.
Update : Expanding a good point mentioned by #Thorsten in below comment
The issue with GREATEST() function is that, whenever you need to handle the NULL values being passed to it, you must use NVL to get proper output. There have had been numerous questions on the subject, like "How to avoid NULL values with GREATEST function" etc.
Note : From performance point of view, please test it before applying such logic in your production environment.

Here are two ways to circumvent GREATEST's NULL problem (i.e. the problem that GREATEST not only returns NULL when all values are NULL, but already when at least one value is null, which makes working with GREATEST often a nuisance).
For n columns you can use n COALESCE expressions. Each expression must contain all columns, each beginning with a different column.
select
max(
greatest(
coalesce(col1,col2,col3,col4),
coalesce(col2,col3,col4,col1),
coalesce(col3,col4,col1,col2),
coalesce(col4,col1,col2,col3)
)
)
from mytable;
An alternative is not to use GREATEST at all, but compare with CASE and COALESCE. Each value gets compared with all other values. col1 >= coalesce(col2,col1) ensures that col1 is regarded greater or equal the expresson, when col2 is NULL, as long as col1 itself is not NULL. In CASE all columns are NULL the CASE expression defaults to NULL. (One could add else NULL to make this visible to the unexperienced reader.)
select
max(
case
when col1 >= coalesce(col2,col1) and col1 >= coalesce(col3,col1) and col1 >= coalesce(col4,col1) then col1
when col2 >= coalesce(col1,col2) and col1 >= coalesce(col3,col2) and col1 >= coalesce(col4,col2) then col2
when col3 >= coalesce(col1,col3) and col1 >= coalesce(col2,col3) and col1 >= coalesce(col4,col3) then col3
when col4 >= coalesce(col1,col4) and col1 >= coalesce(col2,col4) and col1 >= coalesce(col3,col4) then col4
end
)
from mytable;

Related

display a value only if none of the dependent values are nul

I am quite new to SQL and would need some suggestions to write a query to select the value of Col1 only if none of the value of status of Col2 (Col3) is null in plsql.
In the above, I am expecting the result to return only A2 as in A1, there is a null value in col3.
There are many ways to skin this cat but this should return you all the A2 rows:
select *
from yourTable d
where d.col1 in (select col1
from (select col1
,sum(case when col3 is null then 1 else 0 end) null_values
from yourTable
group by col1
)
where null_values = 0
)
To explain the approach briefly: The inner query counts the number of null col3 values for each col1 value; then the middle query returns the only the distinct col1 values having no associated null col3 values, and the outer query returns the all detail for all rows containing the col1 value.
The above is SQL of course. As you have specified PL/SQL, you could include something like a cursor or the bulk collect into clause as desired to work with the query result set.
As mentioned in my comment below, this is just one way of achieving the output - Oracle analytic functions may also prove helpful. Also depending on the size of your data set, you may want to consider the performance aspect of your chosen method.

Count number of entries where Col1 has multiple entries with different data in Col2

I have a table with two Columns, Col1 and Col2. I want to retrieve only the data when the Col1 has multiple entries with different data in Col2.
For example, I want to retain:
Col1 Col2 Col1 Col2
AB-123456 AP-321654 AB-123456 AP-321654
AB-123456 AP-321789 AB-123456 AP-321789
AB-123456 AC-321456 AB-123456 AC-321456
AB-951357 AP-989898 AB-456851 AP-110211
AB-753159 AC-956854 AB-456851 AP-110279
AB-456851 AP-110211
AB-456851 AP-110279
I created through the Report feature a report that groups by Col1 and creates a subcount on the number of entries in Col2. Using the report, I seem to be having a problem with using the subtotal counter. If the subtotal counter is > 1, I want to report, otherwise skip and go to the next Col1 data.
My next option was to write VBA code to read through the table and output the multiples to a new data, then I could run that data through the report to format, etc. To count the multiples and use criteria, I thought perhaps the DCount function would work. I have tried different variations, but to no avail.
Ex:
NUM_OF_MULTS = DCount("Col1", "TBL_Of_Col1_Col2", Current_Col2 = Prev_Col2)
For Index1 = 1 to NUM_OF_MULTS ......
I tried different criteria, but it is either all the records or none.
I think the following should perform as you require:
select t1.* from YourTable t1
where exists (select 1 from YourTable t2 where t1.col1 = t2.col1 and t1.col2 <> t2.col2)
Change both occurrences of YourTable to suit your table name.
For every record, this uses a correlated subquery to test whether another record exists in the dataset with the same col1 value as the current record, but a different col2 value, thus meeting your criteria.
The use of select 1 is purely for optimisation: we don't care what data is returned by the subquery, only that it has records.
If you want to do it with SQL code you can use EXISTS:
select t.* from tablename as t
where exists (
select 1 from tablename
where Col1 = t.Col1 and Col2 <> t.Col2
)
and another option with the IN operator:
select * from tablename
where Col1 in (
select Col1
from (select distinct Col1, Col2 from tablename)
group by Col1
having count(*) > 1
)

Is there a way to compare 2 columns of table in SQL Server

Can somebody give me an idea to solve this? Finding 2 columns in a table has same data, we don't have idea about the columns to be same.
Can I move partial data into excel to check?
I have columns of about 39 and rows of 2B
Col1 equal to col3
col2 equal to col6
col4 not equal col5
Output should show only columns that are common or some output which are common
Null Values are bothering me.
Thanks in advance.
If you have 39 columns and want to evaluate each against every other that gives you 741 column pairings to evaluate. This is possible to do in a concise manner but I wouldn't recommend this for 2 billion rows!
SELECT V1.name,
V2.name
FROM YourTable T WITH (TABLOCKX)
CROSS APPLY (SELECT (SELECT T.*
FOR xml path('row'), elements, type)) CA(X)
CROSS APPLY CA.X.nodes('/row/*') N1(n)
CROSS APPLY CA.X.nodes('/row/*') N2(n)
CROSS APPLY (VALUES(n1.n.value('local-name(.)', 'sysname'), n1.n.value('.', 'nvarchar(4000)') )) V1(name, val)
CROSS APPLY (VALUES(n2.n.value('local-name(.)', 'sysname'), n2.n.value('.', 'nvarchar(4000)') )) V2(name, val)
WHERE V2.name < V1.name
AND V1.val = V2.val
GROUP BY V1.name,
V2.name
HAVING COUNT(*) = (SELECT COUNT(*)
FROM YourTable)
You should first profile the values in your columns. Get the MIN, MAX and COUNT of all columns (and potentially other aggregate data too for numeric columns). Discard any columns where COUNT is not equal to the whole row count as these won't match anything with your desired treatment of NULLs and identify sets of columns with the same MIN and MAX for further investigation.
If you do that with your example data you will see that the only pairs worth investigating are Col1 <-> col3 and Col2 <-> col6. So you can then do a much more focused query to determine whether this is actually the case.
SELECT COUNT(*),
SUM(CASE WHEN col1 = col3 THEN 1 ELSE 0 END) AS count_rows_same_1_3,
SUM(CASE WHEN col2 = col6 THEN 1 ELSE 0 END) AS count_rows_same_2_6
FROM YourTable T

Is there a SQL Statement that allows me to copy and insert existing rows but with one column change?

You might not understood what I want to ask from the title but ,here is the explanation.
I have a data in Oracle database table. What I wanted to do is insert a new data to the table. This new data is based on the existing data but I have to change the value of one columns. So if I have 10 rows in the database after the insertion i will have 20 rows but the new 10 rows contain the same data except on of the columns is changed.
E.g table before insertion a new data
Col1 Col2 Col3
a b AA
1 2 33
table after insertion a new data
Col1 Col2 Col3
a b **BB**
1 2 **44**
Provided that you can encode what the new value should be; yes.
INSERT INTO
myTable (
Col1,
Col2,
Col3
)
SELECT
Col1,
Col2, -- This is a specific example based on your comment.
Col3 + 6 -- This just adds 6 to the existing value, but any SQL
FROM -- could actually go here, such as a CASE statement...
myTable
So, the question becomes; Do you have rules that you can implement in SQL for calculating the new value for Col3?
The rules could be something basic like...
CASE WHEN Col3 = 'AA' THEN '**BB**'
WHEN Col3 = '33' THEN '**44**'
ELSE 'Unknown'
END,
Or you could have all the new values in another table and look them up using a join...
INSERT INTO
myTable (
Col1,
Col2,
Col3
)
SELECT
OldTable.Col1,
OldTable.Col2,
COALESCE(NewTable.Col3, 'Unknown')
FROM
myTable AS OldTable
LEFT JOIN
lookup AS NewTable
ON OldTable.Col1 = NewTable.Col1
AND OldTable.Col2 = NewTable.Col2
Or a whole bunch of other options.
It will depend on how you determine how to change the data. How do you know, for example, that AA should become BB or that 33 should become 44?
Something like this will work for the two cases you posted. You can adapt it to whatever rule you want by changing the CASE statement to compute the new value differently.
INSERT INTO table_name( col1, col2, col3 )
SELECT col1,
col2,
(CASE WHEN col3 = 'AA'
THEN 'BB'
WHEN col3 = '33'
THEN '44'
ELSE null
END)
FROM table_name;

SQL query grouped parameter maximum

Let's say I had two columns in a database, col1 and col2. Column 2 is the time, Column 1 something. In my query, I want to do the following:
I want to SELECT * from my table and group the results by col1. However, I only want those entries where for the grouped col1 there is no value of col2 higher than a certain value. Meaning that, I only want those col1-s for which col2 does not exceed a certain value.
If, for instance, I had three rows, as follows:
ROW1: col1 = val1, col2 = 3
ROW2: col1 = val1, col2 = 5
ROW3: col1 = val2, col2 = 3
ROW4: col1 = val2, col2 = 4
And I do not want the time for any of them to exceed 4, then, as a result, I would only want ROW3 or ROW4, which, does not matter, for col1 is the same and is grouped. But in rows 1 and 2, that are grouped by col1's value "val1", in one of them col2 DOES exceed 4, therefore, I do not want any of them.
SELECT col1 FROM table GROUP BY col1 HAVING MAX(col2) <= 4
Because you want only the common value (col1) from the group, you can use GROUP BY. When you do a GROUP BY (aggregate) query, you can use the HAVING clause to apply a filter to the aggregated data set.
I am not use I got the point (my english is not good).
I think sub-query is the best choice.
Note: this example should work with mySql ...
SELECT *
FROM table
WHERE col1 IN
(SELECT col1 FROM table WHERE col2 < 5 GROUP BY col1)
ORDER BY col1
CREATE TABLE x (
t TIME NOT NULL,
v INT NOT NULL );
INSERT INTO x VALUES
('13:14:00', 24),
('13:14:00', 27),
('13:14:00', 29),
('17:12:00', 14),
('17:12:00', 20),
('17:12:00', 24);
SELECT t, MAX(v) AS mv FROM x
GROUP BY t
HAVING mv <= 25;
Or do I misunderstand the question?