Oracle SQL: Self join and self lookup - self-join

Need help. I am putting my requirement in simple steps here. I have a data like below.
with x as (
select 'a=x AND b=y AND c=z' C1, 'a' C2, '100' C3 from dual union all
select 'a=x AND b=y AND c=z' C1, 'b' C2, '200' C3 from dual union all
select 'a=x AND b=y AND c=z' C1, 'c' C2, '300' C3 from dual union all
select 'a=x AND d=y AND c=z' C1, 'd' C2, '400' C3 from dual union all
select 'a=x AND e=y AND c=z' C1, 'e' C2, '500' C3 from dual
)
select * from x;
My output looks like below:
C1 C2 C3
------------------------------
a=x AND b=y AND c=z a 100
a=x AND b=y AND c=z b 200
a=x AND b=y AND c=z c 300
a=x AND d=y AND c=z d 400
a=x AND e=y AND c=z e 500
I am looking for a query to get the output like below. I have a condition in one column (C1), Also I have look-up data in same table in different columns (C2 and C3). I want to replace the values in C1 if any of the string exists in column C2 with the value from column C3.
100=x AND 200=y AND 300=z a 100
100=x AND 200=y AND 300=z b 200
100=x AND 200=y AND 300=z c 300
100=x AND 400=y AND 300=z d 400
100=x AND 500=y AND 300=z e 500
My exact requirement is, I have a table with a column contains WHERE condition (C1) like above. They used business column names in condition and there is second column with business name (C2) and there is third column with actual physical column name in DB (C3) in the same table. I am looking for the query which can replace the business names in C1, by looking at column C2 with the corresponding value in column C3.

Related

SQLite dummy value in select output on empty table

I have a SQLite database with this sample table:
CREATE TABLE PROVA
(
c1 TEXT NOT NULL UNIQUE,
c2 TEXT NOT NULL,
c3 TEXT NOT NULL
);
This table will be empty at certain times (e.g. during initialization) and I need a query which can return an output like this without having to specify the columns name (a dummy value for every column).
c1 c2 c3
-------- -------- --------
dummy dummy dummy
We could use a union trick here with the help of RANK:
WITH cte AS (
SELECT c1, c2, c3, 1 AS pos FROM PROVA
UNION ALL
SELECT 'dummy', 'dummy', 'dummy', 2
),
cte2 AS (
SELECT c1, c2, c3, RANK() OVER (ORDER BY pos) rn
FROM cte
)
SELECT c1, c2, c3
FROM cte2
WHERE rn = 1;

informix 11.5 How to count the number of members of a column separately

I have a table that have a column like this:
table1:
c1 c2 c3
. a .
. a .
. a .
a
b
b
c
How to get a result like the following?:
-- a b c
count(a) count(b) count(c)
Of course, there is an auxiliary table like the one below:
--field table
d1 d2
a
b
c
Transferring comments into an answer.
If there was an entry in table1.c2 with d as the value, is it correct to guess/assume that you'd want a fourth column of output with the name d and the count of the number of d values as the value. And there'd be an extra row in the auxilliary table too. That's pretty tricky.
You'd probably be better off with a result table with N rows, one for each value in the table1.c2 column, with the first column identifying the value and the second the count:
SELECT c2, COUNT(c2) FROM table1 GROUP BY c2 ORDER BY c2
To generate a single row with the names and counts as shown requires a dynamically built SQL statement — you write an SQL statement that generates the SQL (or the key components of the SQL) for a second statement that you actually execute to get the result. The main reason for it being dynamic like that is that the number of columns in the result set is not known until you run a query that determines which values exist in table1.c2. That's non-trivial — doable, but non-trivial.
I forget whether 11.50 has a built-in sysmaster:sysdual table. I ordinarily use a regular one-column, one-row table called dual. You can get the result you want, if your Table1.C2 has values a through e in it, with:
SELECT (SELECT COUNT(*) FROM Table1 WHERE c2 = 'a') AS a,
(SELECT COUNT(*) FROM Table1 WHERE c2 = 'b') AS b,
(SELECT COUNT(*) FROM Table1 WHERE c2 = 'c') AS c,
(SELECT COUNT(*) FROM Table1 WHERE c2 = 'd') AS d,
(SELECT COUNT(*) FROM Table1 WHERE c2 = 'e') AS e
FROM dual;
This gets the information you need. I don't think it is elegant, but "works" beats "doesn't work".

some registers can't get with sql

I'm trying to get a count with these query. I would like that it shows me all nameHost (althought the count was 0) but with these query, the nameHost that the count is 0 not shows me nameHost.
Could you help me please?
select a.nameHost, count(b.job_name) as cuenta
from jobsDefinition b
right join cmdmzpre.nodes a
on b.node_id in (a.nodeid,a.nameHost)
where b.app not like 'UNPLAN' group by a.nameHost order by a.nameHost desc;
Example of tables:
nodes
=======
nameHost nodeid
--------- -------
a a
b b
b f
e g
jobsDefinition
================
node_id job_name
---------- -----------
a fruit
b apple
c iron
a banana
f orange
The output would be:
a 2 (fruit,banana)
b 2 (apple,orange)
e 0
As mentioned in my comment, From your dataset, **a** should be **2**.
Use this query:
---Table Data start ----
WITH nodes (nameHost, nodeid)
AS (SELECT 'a', 'a' FROM DUAL
UNION ALL
SELECT 'b', 'b' FROM DUAL
UNION ALL
SELECT 'b', 'f' FROM DUAL
UNION ALL
SELECT 'e', 'g' FROM DUAL),
jobdef (node_id, job_name)
AS (SELECT 'a', 'fruit' FROM DUAL
UNION ALL
SELECT 'b', 'apple' FROM DUAL
UNION ALL
SELECT 'c', 'iron' FROM DUAL
UNION ALL
SELECT 'a', 'banana' FROM DUAL
UNION ALL
SELECT 'f', 'orange' FROM DUAL)
--Table data end---
--Actual Query
SELECT n.namehost,
COUNT (jd.node_id) AS Cnt,
LISTAGG (jd.job_name, ',') WITHIN GROUP (ORDER BY 1) JB_NM
FROM nodes n
LEFT JOIN jobdef jd
ON n.nodeid = jd.node_id
GROUP BY n.namehost
ORDER BY namehost;

sqlite select difference between 2 tables

table1 has columns A1,B1,C1 and table2 has columns A2,B2,C2 where A1 and A2 refer to the same thing, and B1 and B2 refer to the same thing.
How do I find rows in table1 such that the equivalent rows in table2 (A2=A1, B2=B1) are not in table1, and vice versa?
is it an EXCEPT?
You can use NOT EXISTS like
select A1, B1, C1
from table1
where not exists
(
select 1 from table2
where A2 = table1.A1
and B2 = table1.B1
)

sqlite select minimum and maximum from two tables

I have two tables
table1
c1t1 c2t1
1 saanu
3 abc
table2
c1t2 c2t2
2 val2
4 val4
I have to find out the values of c2t1 and c2t2 for the minimum and maximum value of c1t1 and c1t2 with one line command.
For the above example I have to find saanu and val4
I had a very similar problem and solved it with UNION ALL. The minimum of the aColumn column in tables aTable1, ... , aTableN can be computed as:
SELECT Min(aColumn)
FROM (
SELECT aColumn FROM aTable1 UNION ALL
SELECT aColumn FROM aTable2 UNION ALL
...
SELECT aColumn FROM aTableN) t;
You should be able to do Min in each of the inner selects, but I haven't found out how to do that!
One approach:
select max(case c1 when min1 then c2 end) c2_first,
max(case c1 when max1 then c2 end) c2_last
from (select c1t1 c1, c2t1 c2 from table1
union all
select c1t2 c1, c2t2 c2 from table2) u
cross join
(select min(min11, min12) min1, max(max11, max12) max1 from
(select min(c1t1) min11, max(c1t1) max11 from table1) t1
cross join
(select min(c1t2) min12, max(c1t2) max12 from table2) t2) m
SQLFiddle here.
1)
SELECT c2t1
FROM table1
ORDER BY c1t1 ASC LIMIT 1
2)
SELECT c2t2
FROM talbe2
ORDER BY c1t2 DESC LIMIT 1

Resources