table contains :
c1 c2 c3 c4 c5
da1 Null Null db1 dc1
da1 dx1 Null db1 dc1
da1 Null dy1 db1 Null
all are varchar fields.
i need a query which results data without any nulls and in a single row
"da1 dx1 dy1 db1 dc1"
select 'da1', 'dx1', 'dt1', 'db1', 'dc1' from thattable limit 1
should work perfectly
With the details delivered:
select max(c1), max(c2), max(c3), max(c4), max(c5)
from yourtable;
See here a simulation by MGA.
Try this.
select * from (select distinct c1, c2, c3, c4, c5 from mytable) as mytab where c1 is not null and c2 is not null and c3 is not null and c4 is not null and c5 is not null
Related
I have a table like this:
C1 C2 C3 C4 C5 C6
INTERESES 40530 5 050405011232011 2013 5
PRINCIPAL 40529 5 050405011232011 2016 3
PRINCIPAL 40530 5 050405011232011 2013 4
And i need to group this rows by C1, C3 and C4 and return in the cases of lane 2 and 3 the C2 column with the max C5. I mean in this case this should return:
C1 C2 C3 C4 C5
INTERESES 40530 5 050405011232011 5
PRINCIPAL 40529 5 050405011232011 7
The second row should have 40529 in the column C2 cause it have the biggest value in the C5 column.
The C5 column should have the sum of the C6 of the previous table.
How can i do this? Thanks and sorry for my english
Instead of aggregation, one method uses window functions:
select c1, c2, c3, c4, sum_c5
from (select t.*,
sum(c5) over (partition by c1, c3, c4) as sum_c5,
row_number() over (partition by c1, c3, c4 order by c5 desc) as seqnum
from t
) t
where seqnum = 1;
Oracle also has keep syntax with allows you to get the "first" value in an aggregation:
select c1,
max(c2) keep (dense_rank first order by c5 desc) as c2,
c3, c4,
sum(c5)
from t
group by c1, c3, c4;
You can do it with NOT EXISTS:
select t.c1, t.c2, t.c3, t.c4
from tablename t
where not exists (
select 1 from tablename
where c1 = t.c1 and c2 = t.c2 and c3 = t.c3 and c5 > t.c5
)
Given the following table with 2 columns:
c1 c2
------------
a1 | b1
a1 | b1
a2 | b2
a2 | b3
a3 | b3
I want to return those values from column c2 where the value of c2 column appears multiple times for the same c1 value. I am doing the following SQL query to return the required result:
SELECT DISTINCT ( c2 ) AS c
FROM ( SELECT c1 , c2 , COUNT (*) AS rowcount
FROM table
GROUP BY c1 , c2 HAVING rowcount > 1 )
Result:
c
---
b1
Is there any alternative SQL statement of the above query?
Based on your description, you can use:
select distinct c1
from (select t.*, count(*) over (partition by c2) as cnt
from t
) t
where cnt >= 2;
Based on your sample results:
select c1
from t
group by c1
having count(*) >= 2;
And based on the revised question:
select c2
from t
group by c2
having count(*) >= 2;
Use count in having clause instead of using subquery:-
select c1
from table
group by c1
having count(c2) > 1
Most answers above will work if you want all the values in c1 that appear more than once in the table (even with the same value on c2).
If you want to measure only values of c1 that may have multiple DISTINCT values on c2 you can use:
SELECT c1
FROM table
GROUP BY c1
HAVING COUNT(DISTINCT c2) > 1
I have a (now thoroughly derived) CTE:
feature_id | function_id | group_id | subgroup_id | type_id
1 1 null 1 null
1 1 null null 14
2 1 null 5 null
2 1 null null 21
3 1 null 7 null
3 1 null null 5
I am trying to collate the rows together using this:
select C1.feature_id, C1.function_Id, C2.Group_ID, C3.Subgroup_ID, C4.Type_id
from CTE C1
left join CTE C2
on C1.feature_id = C2.feature_id
and c1.function_id = c2.function_id
and c2.group_id is not null
left join CTE C3
on C1.feature_id = C3.feature_id
and c1.function_id = c3.function_id
and c3.subgroup_id is not null
left join CTE C4
on C1.feature_id = C4.feature_id
and c1.function_id = c4.function_id
and c4.type_id is not null
This gives me 0 rows...
To validate, I ran:
select *
from CTE C1
236 rows selected
Can anyone help? Surely the rows from C1 should be coming back...
EDIT: Fixed it with the Oracle syntax:
select C1.feature_id, C1.function_Id, C2.Group_ID, C3.Subgroup_ID, C4.Type_id
from CTE C1, CTE C2, CTE C3, CTE C4
where C1.feature_id = C2.feature_id(+)
and c1.function_id = c2.function_id(+)
and C2.group_id(+) is not null
...
(I hate the oracle syntax)
Ok, solved it... Fixed it with the Oracle syntax:
select C1.feature_id, C1.function_Id, C2.Group_ID, C3.Subgroup_ID, C4.Type_id
from CTE C1, CTE C2, CTE C3, CTE C4
where C1.feature_id = C2.feature_id(+)
and c1.function_id = c2.function_id(+)
and C2.group_id(+) is not null
...
(I hate the oracle syntax)
SELECT
Answer_Code AS C0,
Text AS C1,
Price AS C2,
sum(case when Response like '%PAID%' then 1 else 0 end) AS C3,
C2 * C3 AS 'Total' --Invalid column name 'C2'.--
FROM TableA
How to reference C2 and C3, then do multiplication in that position ?
Any techniques ?
You can use inner-select:
SELECT *, C2 * C3 As Total
FROM (
SELECT
Answer_Code AS C0,
Text AS C1,
Price AS C2,
sum(case when Response like '%PAID%' then 1 else 0 end) AS C3,
FROM TableA) DT;
or use a CTE like this:
;WITH t AS (
SELECT
Answer_Code AS C0,
Text AS C1,
Price AS C2,
sum(case when Response like '%PAID%' then 1 else 0 end) AS C3,
FROM TableA)
SELECT *, C2 * C3 As Total
FROM t;
You can use with clause or a sub query.
;with tmp_tbl as (
SELECT
Answer_Code AS C0,
Text AS C1,
Price AS C2,
sum(case when Response like '%PAID%' then 1 else 0 end) AS C3
--C2 * C3 AS 'Total' --Invalid column name 'C2'.--
FROM TableA
)
select
C0,
C1,
C2,
C3,
C2 * C3 AS 'Total'
from tmp_tbl;
In advance, I would like to say thanks for the help. This is a great community and I've found many programming answers here.
I have a table with multiple columns, 5 of which contain dates or null.
I would like to write an sql query that essentially coalesces the 5 columns into 1 column, with the condition that if 1 of the five columns contains a "NULL" value, the returned value is null. Essentially the opposite of the coalesce condition of returning the first non-null, I want to return the first null. If none are null, returning the greatest of the 5 dates would be optimal, however I can settle with returning any one of the 5 dates.
C1 C2 C3 C4 C5
-- -- -- -- --
1/1/1991 1/1/1991 1/1/1991 1/1/1991 2/2/1992
NULL 1/1/1991 1/1/1991 1/1/1991 1/1/1991
Query Returns:
C1
--
2/2/1992
NULL
Thank you very much.
(Server is MSSQL2008)
select greatest(c1, c2, c3, c4, c5)
from table;
Life can be so easy :-)
(edit: works on Oracle)
Without overthinking it:
SELECT
CASE WHEN c1 is null or c2 is null or c3 is null or c4 is null or c5 is null
THEN null
ELSE c1
END
FROM mytable
My edit is as follows:
CASE
WHEN (c1 >= c2 AND c1 >= c3) THEN c1
WHEN (c2 >= c1 AND c2 >= c3) THEN c2
WHEN (c3 >= c1 AND c3 >= c2) THEN c3
END
Try this:
SELECT
CASE WHEN t1.SomeDate IS NULL THEN NULL ELSE MAX(t1.SomeDate) END AS TheVal
FROM
(
SELECT C1 AS SomeDate FROM Table_1
UNION ALL
SELECT C2 AS SomeDate FROM Table_1
UNION ALL
SELECT C3 AS SomeDate FROM Table_1
UNION ALL
SELECT C4 AS SomeDate FROM Table_1
UNION ALL
SELECT C5 AS SomeDate FROM Table_1
) t1
GROUP BY
t1.SomeDate
perhaps a variation on coalesce (replace -1 with an invalid value)?
SELECT CASE WHEN COALESCE(C1,C2,C3,C4,C5,-1) = -1 THEN NULL ELSE COALESCE(C1,C2,C3,C4,C5) END
Maybe with LEAST?
I don't know how this works with NULL.
SELECT
CASE WHEN C1 IS NULL THEN C2 WHEN C1 IS NULL AND C2 IS NULL THEN C3 WHEN C1 IS NULL AND C2 IS NULL AND C3 IS NULL THEN C4 WHEN C1 IS NULL AND C2 IS NULL AND C3 IS NULL AND C4 IS NULL THEN C5 ELSE C1 END AS REQUIREDNOTNULLVALUE
FROM
TABLE1