fetch value from multiple table - sql

I have a table t1:
c1 c2
1 a
2 b
3 a
table t2:
c3 c4
4 1
5 2
6 3
I am writing a procedure:
select c1 from t1 where c2 = 'a';
which will give multiple outputs. Use that output (1,3)
select c3 from t2 where c4 = 1,
select c3 from t2 where c4 = 3 -- out put of first query;
How do I implement it ? Should i use any loops? If so, how do I loop or how do I write a procedure which returns the result of it?

you can use join
select c1,c3 from t1
inner join t2 on t1.c1=t2.c4
where c2='a'

You might have to work more on it but this is how i think this can be implemented.Inside the SP create a table data type where you can insert the multiple values fetched and then you can pass these values to another SP as a table value parameters where you can do the rest of the operations.

You can use in
select c3 from t2 where c4 in
(select c1 from t1 where c2 = 'a')

select c1,c3 from t1,t2
where t1.c1=t2.c4 and c2='a'

Related

hive query to map keys to multiple values

I have a table with a structure similar to the one below:
|C1|C2|C3|
|K1|V1|??|
|K1|V2|??|
|K1|V3|??|
|K2|V2|??|
I need to write a query that checks if the key(lets say K1) maps to a specific value in any row (say V2). If it does the value in column C3 is taken as 1 otherwise its 0.
I'd appreciate any help.
The folloiwng query should give the results you want. The subquery identifies all C1 values which map to a certain value in C2 at least once. If so, then we render the C3 values as 1, otherwise we show 0.
SELECT
t1.C1,
t1.C2,
CASE WHEN t2.C1 IS NOT NULL THEN 1 ELSE 0 END AS C3
FROM yourTable t1
LEFT JOIN
(
SELECT C1
FROM yourTable
GROUP BY C1
HAVING SUM(CASE WHEN C2 = 'V1' THEN 1 ELSE 0 END) > 0
) t2
ON t1.C1 = t2.C1

combine two oracle sql results into single dataset

I have two selects and I want to combine them in such a way, that only one row that has key column matched in both selects are returned(one row in first select and one row in 2nd select). Is there any built-in way in Oracle 10g to achieve this?
I have two sql as below
Query 1:
select c11, c12 from table t1
where c11=1000
Query 2:
select c21, c22
from t2
where c21=1000
I want to combine both query 1 and query 2 on key columns(OPTYREVN_OPTY_XI, OPTYREVN_SEGMENT_XI and OPTYREVN_OPTYREVNCRM_ID). My output should contain only the only one row which found in results of query 1 and query 2.
I am not sure to use UNION or Intersect or left outer join.
Kindly suggest me some solution which will be helpful in this scenario. Thanks.
So, If I got you right, you want to have c1 , c2 , c33 , c21 , c22 and c 23 on one line, if both queries return only one line and no information can link them, this should work...
SELECT a.* , b.*
FROM (select c1, c2, c33
from t1, t3
where c1= 1000 and c33 is null) a ,
(select c21, c22, c23
from t2
where c21= 1000) b
/*WHERE...*/ --you could always use some condition linking a and b
I think you would want to use a join:
SELECT c1, c2, c33, c21, c22, c23
FROM t1 INNER JOIN t3 ON <key columns>
INNER JOIN t2
ON t1.c1 = t2.c21
WHERE t1.c1 = 1000
AND t3.c33 IS NULL;

SQL assign value for join purpose to null field without update

With this sample data...
c1 c2 c3
10 a 10a
11 a **NULL**
12 a **NULL**
13 b 13b
13 b **NULL**
etc..
I want to assign c3 value to rows where it is NULL and c2 value is the same, but do not wish to actually update that value in table, only use it to join to another table while select runs.
I am able to do it using inner select and join, but I want to save as much processing power as possible because amount of data is huge and thought that some use of ISNULL or COALESCE should be able to do it, but I don't have enough experience to figure it on my own yet, or even say if it is possible. What do you think?
select
a.c1,
a.c2,
coalesce( a.c3, b.c3) as c3
from table1 a
left join table1 b
on a.c2 = b.c2
an a.c3 is null
and b.c3 is not null
this will work with the data that you have however if there is more than one row that is not null for c3 for a given c2 it will cause problems
If your DB supports max() over you can also do this
SELECT c1,
c2,
coalesce(c3, MAX(C3) over (partition by c2)) c3
from table1
demo
A sample pseudo code which i often use in my Query.
DECLARE #USER_ID AS VARCHAR(256)
SELECT #USER_ID = 'abc'
select *
from CC
INNER JOIN adm_co_users CR_US WITH(NOLOCK) ON ISNULL(CR_US.original_userID,'') =
(CASE ISNULL(CC.Createdby,'') WHEN '' THEN #USER_ID
ELSE CC.Createdby END )

Comma Separated list of rows of a column with group by on other columns

Below is the structure of table I have: -
Table T1
C1 C2 C3
----------
X P A
X P B
Y Q C
Y Q D
Desired output: -
C1 C2 C3
------------
X P A,B
Y Q C,D
Note: - I know i can do the same with For XML('') with group by on C1 and C2, but the main problem in my case is that the table T1 here must be a physical table object (either permanent or temp or table var or CTE) in DB. But in my case it's a derived table and when i am using the below query it's saying invalid object.
In my case it's not good to replace the derived table with temp# tables or fixed tables or even with CTE or table variable because it will take a great effort.
SELECT
b.C1, b.C2, Stuff((',' + a.C3 from t1 a where a.c1 = b.c1 for XML PATH('')),1,1,'') FROM
T1 b group by b.c1,b.c2
I did not have T1 as fixed table. Please consider it as derived table only.
I need the solution with existing derived table.
Please help.
Below is the query with derived table: -
Please consider this only as a demo query. It's not as simple as given below and a lot of calculations have done to get the derived tables and 4 levels of derived tables have been used.
SELECT C1, C2, Stuff((',' + a.C3 from A B where a.c1 = b.c1 for XML PATH('')),1,1,'')
FROM
(
SELECT C1, C2, C3 FROM T1 WHERE C1 IS NOT NULL--and a lot of calculation also
)A
Please mind that T1 is not just below one step, in my case T1 the actual physical table is 4 level downs by derived tables.
If you can post the query the produces derived table, we can help you work it out, but as of the moment try substituting table1 with the derived query.
;WITH Table1
AS
(
SELECT C1, C2, C3 FROM T1 WHERE C1 IS NOT NULL--and a lot of calculation also
)
SELECT
C1,C2,
STUFF(
(SELECT ',' + C3
FROM Table1
WHERE C1 = a.C1 AND C2 = a.C2
FOR XML PATH (''))
, 1, 1, '') AS NamesList
FROM Table1 AS a
GROUP BY C1,C2
SQLFiddle Demo

Compare Every Record from Two Tables

Assuming I have two SQL tables consisting of a single column,
i.e.
Table 1 Table 2
a1 a2
b1 b2
c1 c2
Is there a succinct SQL command to compare each record in one table against each record in the other? (and return true if any record from table 1 matches any record from table 2)
i.e.
if( a1 = a2 OR a1 = b2 OR a1 = c2 OR b1 = a2 OR b1 = b2...)
I want
If any record from table a matches table b (i.e., in a table of ints, they are the same int), return true.
Why not simply
if exists (select 1 from T1 inner join TB on T1.Col = T2.Col)
A full join is well suited to finding differences. To find rows that are missing in either table:
select *
from t1
full join
t2
on t1.col1 = t2.col1
where t1.col1 is null
or t2.col1 is null
This assumes that the single column is unique (i.e. has no duplicate values.)