Selecting few columns as table - sql

I have a problem in designing a query:
I have to select few records based on criteria
(SELECT COL_1, COL_2,COL_3 FROM TABLE_1 WHERE COL_3 = 'CND')
Now I need to select records from two databases based on these results
(SELECT XX_1, XX_2
FROM TABLE_2 WHERE TABLE_2.XX1 = TABLE_1.COL1
(from filtered results in step 1)
(SELECT YY_1, YY_2, YY_3
FROM TABLE_3 WHERE TABLE_3.YY_2 = TABLE_1.COL2)
(from filtered results in step 1)
I need results in single table view
XX_1, XX_2, YY_1, YY_2, YY_3
mentioned columns must be equal to be in result and only record with such equality should be fetched.
I need to run this on millions of records, so performance is considered
It's gonna be used in Java classes, so please don't suggest me any db specific/sql commands which can't be executed since I don't hold any db permissions other than read.
Hope I am clear. In case not, I will explain the doubts.
I tried something like this
SELECT *
FROM TABLE_2
JOIN
(SELECT COL_1,
COL_2,
COL_3
FROM TABLE_1
WHERE COL_3 = 'CND'
GROUP BY COL_1) TMP_TABLE
ON (TMP_TABLE.COL_1 = TABLE2.XX_1)
But I got view/table doesn't exists - oracle error.

I think you need to use a subquery,just like this
select col_1,col_2
from(
select col_1,col_2
from (
select col_1,col_2 from table_1
)tbl1
left join table_2 tbl2 on tbl2.col_1 = tbl1.col_1
)tbl3
left join table_3 tbl3 on tbl4.col_1 = tbl3.col_1

with usedrows as
( select a.Col_1,a.Col_2 FROM table1 a left JOIN table2 b ON a.Col_1=b.Col_2)
select Col_1,C0l_2 from usedrows
This is just an example where usedrows is a virtual table made after join.and u can select the columns from that join table as u select from other table.

Related

How can I divide two columns and show results per row?

I would not be able to do what I want :(, I explain:
I have two tables:
Table1
Table2
I am looking for a query where I can get FALSE/(FALSE+TRUE).
So far I managed to do it over the total using this:
select
(select count(*) from (select Drink from Table1
where Drink=False)) as F,
(select count(*) from (select Drink from Table1
where Drink=True)) as T,
(F/(F+T)) as total
With that I can get the total of the column I want, I just have to change, if I want, Drink-->Food.
But now I would like the final result by Country, that is, something similar to this:
Expected result
In order not to break the forum rules, I'll leave what I've tried so far:
select * from (select distinct t2.Country_Name, count(t1.Drink)
from Table 1 t1
left join Table 2 t2 on t2.Country_ID=t1.Country_ID
where Drink=False
group by t2.Country_Name) as F
union
select * from (select distinct t2.Country_Name, count(t1.Drink)
from Table 1 t1
left join Table 2 t2 on t2.Country_ID=t1.Country_ID
where Drink=True
group by t2.Country_Name) as T
But it doesn't even bring me what I expect :(
I don't know if it helps much, but I'm using Snowflake
Any help is welcome. Thank you!
The first query could be simpliefied using COUNT_IF:
SELECT COUNT_IF(Drink=False)/COUNT(*)
FROM Table1;
Grouped by Country:
SELECT t2.Country_Name, COUNT_IF(t1.Drink=False)/COUNT(*)
FROM Table1 AS t1
JOIN Table2 AS t2
ON t2.Country_ID=t1.Country_ID
GROUP BY t2.Country_Name;

Select from different tables using CONDITION using PL SQL

So I have a table with info that I want
TABLE_1
id
name
And a lot of other tables with the same type of data
TABLE_x
id
order
TABLE_y
id
order
...
TABLE_z
id
order
What I want: depending on the name from TABLE_1 I want to acess the att order of other table.
Example:
TABLE_1
name = x
I want to access TABLE_x and check the order.
But if
TABLE 1
name = z
I want to access TABLE_z etc.
I thought about this piece:
SELECT *
FROM TABLE_1
INNER JOIN (
CASE (SELECT name FROM TABLE_1)
WHEN 'x' THEN TABLE_x on TABLE_1.id=TABLE_x.id
WHEN 'y' THEN TABLE_y on TABLE_1.id=TABLE_y.id
WHEN 'z' THEN TABLE_z on TABLE_1.id=TABLE_z.id)
I can't use the case statement in here.
Need your help!
I don't recommend what immediately follows, but look closely at one possible solution:
-- NOT RECOMMENDED...
WITH t1 AS (
SELECT MIN(name) name FROM table_1
)
SELECT table_x.ordr FROM t1 JOIN table_x ON ('x' = t1.name)
UNION ALL
SELECT table_y.ordr FROM t1 JOIN table_y ON ('y' = t1.name)
UNION ALL
SELECT table_z.ordr FROM t1 JOIN table_z ON ('z' = t1.name)
/
If you think about what the database needs to do to satisfy that query, you'll realize it's a lot of individual queries, with at most 1 of them returning data. That could be a lot of work, which is why it's not recommended. You're likely much better off running an initial query against table_1, with the result driving the target of a subsequent query. With sqlplus, you might opt for something like:
COLUMN name NEW_VALUE name NOPRINT
SELECT MIN(name) name FROM table_1
/
SELECT ordr FROM table_&&name
/
Do give some thought to error situations, such as when the name from query 1 doesn't match any existing table, or if there are no rows in table_1.

using sql create a new table with 2 fields, field1 from tableA and field1 from table B

Am new to SQL and am stuck here with a very simple-looking query request.
I have 2 tables, both having exactly the same structure (IE same no. of columns, same no. Of rows) except for the actual contents. so for example,tableA has 2 columns called col1&col2; tableB has 2 columns too called col1&col2. Now I want to create a 3rd new tale, where 1st column is tableA's col1, and 2nd column is tableB's col1. preferably the name of the 1st column is fromTableA, and name of 2nd column is fromTableC. How do I achieve this please? I tried all the following ways but I always get the same error: "number of query values and destination fields are not the same."
variation 1:
insert into newTable(fromTable1,fromTable2)
select col1 from table1
select col1 from table2
variation 2:
insert into newTable(fromTable1,fromTable2)
select col1 from table1,col1 from table2
variation 3:
insert into newTable(fromTable1,fromTable2)
select col1 from table1, table2
Presumably you have fields in the two tables that can be joined, so this:
insert into newtable (romTable1,fromTable2)
select a.col1, b.col1
from table1 a, table2 b
where a.col1 = b.col1;
The a/b are aliases that differentiate between the two columns in each table. If you don't have fields to join then whatever you're trying to do probably needs a rethink.
You may try following sql query to achieve your purpose:
with OrderedTableA as (
select row_number() over (order by Col1) RowNum, *
from TableA (nolock)
),
OrderedTableB as (
select row_number() over (order by Col1) RowNum, *
from TableB (nolock)
)
select T1.Col1, T2.Col2 into TableC
from OrderedTableA T1
full outer join OrderedTableB T2 on T1.RowNum = T2.RowNum
Above query will create a new table as TableC with column col1 from TableA and col2 from TableB. You may change the queries to your need.
I hope you will understand the above queries. Give it a try.

How do I merge data from two tables in a single database call into the same columns?

If I run the two statements in batch will they return one table to two to my sqlcommand object with the data merged. What I am trying to do is optimize a search by searching twice, the first time on one set of data and then a second on another. They have the same fields and I’d like to have all the records from both tables show and be added to each other. I need this so that I can sort the data between both sets of data but short of writing a stored procedure I can’t think of a way of doing this.
Eg. Table 1 has columns A and B, Table 2 has these same columns but different data source. I then wan to merge them so that if a only exists in one column it is added to the result set and if both exist it eh tables the column B will be summed between the two.
Please note that this is not the same as a full outer join operation as that does not merge the data.
[EDIT]
Here's what the code looks like:
Select * From
(Select ID,COUNT(*) AS Count From [Table1]) as T1
full outer join
(Select ID,COUNT(*) AS Count From [Table2]) as T2
on t1.ID = T2.ID
Perhaps you're looking for UNION?
IE:
SELECT A, B FROM Table1
UNION
SELECT A, B FROM Table2
Possibly:
select table1.a, table1.b
from table1
where table1.a not in (select a from table2)
union all
select table1.a, table1.b+table2.b as b
from table1
inner join table2 on table1.a = table2.a
edit: perhaps you would benefit from unioning the tables before counting. e.g.
select id, count() as count from
(select id from table1
union all
select id from table2)
I'm not sure if I understand completely but you seem to be asking about a UNION
SELECT A,B
FROM tableX
UNION ALL
SELECT A,B
FROM tableY
To do it, you would go:
SELECT * INTO TABLE3 FROM TABLE1
UNION
SELECT * FROM TABLE2
Provided both tables have the same columns
I think what you are looking for is this, but I am not sure I am understanding your language correctly.
select id, sum(count) as count
from (
select id, count() as count
from table1
union all
select id, count() as count
from table2
) a
group by id

An issue possibly related to Cursor/Join

Here is my situation:
Table one contains a set of data that uses an id for an unique identifier. This table has a one to many relationship with about 6 other tables such that.
Given Table 1 with Id of 001:
Table 2 might have 3 rows with foreign key: 001
Table 3 might have 12 rows with foreign key: 001
Table 4 might have 0 rows with foreign key: 001
Table 5 might have 28 rows with foreign key: 001
I need to write a report that lists all of the rows from Table 1 for a specified time frame followed by all of the data contained in the handful of tables that reference it.
My current approach in pseudo code would look like this:
select * from table 1
foreach(result) {
print result;
select * from table 2 where id = result.id;
foreach(result2) {
print result2;
}
select * from table 3 where id = result.id
foreach(result3) {
print result3;
}
//continued for each table
}
This means that the single report can run in the neighbor hood of 1000 queries. I know this is excessive however my sql-fu is a little weak and I could use some help.
LEFT OUTER JOIN Tables2-N on Table1
SELECT Table1.*, Table2.*, Table3.*, Table4.*, Table5.*
FROM Table1
LEFT OUTER JOIN Table2 ON Table1.ID = Table2.ID
LEFT OUTER JOIN Table3 ON Table1.ID = Table3.ID
LEFT OUTER JOIN Table4 ON Table1.ID = Table4.ID
LEFT OUTER JOIN Table5 ON Table1.ID = Table5.ID
WHERE (CRITERIA)
Join doesn't do it for me. I hate having to de-tangle the data on the client side. All those nulls from left-joining.
Here's a set-based solution that doesn't use Joins.
INSERT INTO #LocalCollection (theKey)
SELECT id
FROM Table1
WHERE ...
SELECT * FROM Table1 WHERE id in (SELECT theKey FROM #LocalCollection)
SELECT * FROM Table2 WHERE id in (SELECT theKey FROM #LocalCollection)
SELECT * FROM Table3 WHERE id in (SELECT theKey FROM #LocalCollection)
SELECT * FROM Table4 WHERE id in (SELECT theKey FROM #LocalCollection)
SELECT * FROM Table5 WHERE id in (SELECT theKey FROM #LocalCollection)
Ah! Procedural! My SQL would look like this, if you needed to order the results from the other tables after the results from the first table.
Insert Into #rows Select id from Table1 where date between '12/30' and '12/31'
Select * from Table1 t join #rows r on t.id = r.id
Select * from Table2 t join #rows r on t.id = r.id
--etc
If you wanted to group the results by the initial ID, use a Left Outer Join, as mentioned previously.
You may be best off to use a reporting tool like Crystal or Jasper, or even XSL-FO if you are feeling bold. They have things built in to handle specifically this. This is not something the would work well in raw SQL.
If the format of all of the rows (the headers as well as all of the details) is the same, it would also be pretty easy to do it as a stored procedure.
What I would do: Do it as a join, so you will have the header data on every row, then use a reporting tool to do the grouping.
SELECT * FROM table1 t1
INNER JOIN table2 t2 ON t1.id = t2.resultid -- this could be a left join if the table is not guaranteed to have entries for t1.id
INNER JOIN table2 t3 ON t1.id = t3.resultid -- etc
OR if the data is all in the same format you could do.
SELECT cola,colb FROM table1 WHERE id = #id
UNION ALL
SELECT cola,colb FROM table2 WHERE resultid = #id
UNION ALL
SELECT cola,colb FROM table3 WHERE resultid = #id
It really depends on the format you require the data in for output to the report.
If you can give a sample of how you would like the output I could probably help more.
Join all of the tables together.
select * from table_1 left join table_2 using(id) left join table_3 using(id);
Then, you'll want to roll up the columns in code to format your report as you see fit.
What I would do is open up cursors on the following queries:
SELECT * from table1 order by id
SELECT * from table1 r, table2 t where t.table1_id = r.id order by r.id
SELECT * from table1 r, table3 t where t.table1_id = r.id order by r.id
And then I would walk those cursors in parallel, printing your results. You can do this because all appear in the same order. (Note that I would suggest that while the primary ID for table1 might be named id, it won't have that name in the other tables.)
Do all the tables have the same format? If not, then if you have to have a report that can display the n different types of rows. If you are only interested in the same columns then it is easier.
Most databases have some form of dynamic SQL. In that case you can do the following:
create temporary table from
select * from table1 where rows within time frame
x integer
sql varchar(something)
x = 1
while x <= numresults {
sql = 'SELECT * from table' + CAST(X as varchar) + ' where id in (select id from temporary table'
execute sql
x = x + 1
}
But I mean basically here you are running one query on your main table to get the rows that you need, then running one query for each sub table to get rows that match your main table.
If the report requires the same 2 or 3 columns for each table you could change the select * from tablex to be an insert into and get a single result set at the end...