I have four Select queries for four different tables, each extracting only one record. For example:
Select * from table where col1 = 'something'
gives one row having 3 columns.
The second select query also gives one record having two columns(fields). Same for third and fourth select query.
I want to combine all four result sets into one having one row. How is it possible?
I will write the queries for you.
1st one:
Select Top 1 column1, column2
from table 1
where column 1 = 'something'
and col1 = (Select max(col1) where column 1 = 'something')
2nd query:
Select Top 1 column1, column3
from table 2
where column 1 = 'something'
and column3 = (Select max(column3) where column 1 = 'something')
3rd query uses the result obtained from query 2:
Select column4
from table 3
where column3 = (obtained from 2nd query) (there is only one row)
4th:
Select column5
from table 4
where column3 = (obtained from 2nd query) (there is only one row)
This means I have to join 2nd, 3rd, 4th query, then resulting set in 1st.
I can't use union since columns are different.
So only problem is with joining the result set.
You can use CROSS JOINs to accomplish this.
CREATE TABLE table1 (id int, column1 varchar(5), column2 varchar(15));
CREATE TABLE table2 (column3 varchar(5), column4 varchar(15));
CREATE TABLE table3 (id int, column5 varchar(5), column6 varchar(15));
INSERT INTO table1 VALUES (1, 'aaa', 'row1')
INSERT INTO table2 VALUES ('bbb', 'table2')
INSERT INTO table3 VALUES (1, 'ccc', 'table3')
INSERT INTO table1 VALUES (1, 'ddd', 'table1')
SELECT * FROM (SELECT * FROM table1) a
CROSS JOIN (SELECT * FROM table2) b
CROSS JOIN (SELECT * FROM table3) c
Result:
id column1 column2 column3 column4 id column5 column6
1 aaa row1 bbb table2 1 ccc table3
1 ddd table1 bbb table2 1 ccc table3
Update after clarification:
CREATE TABLE table1
(
id int IDENTITY(1,1)
, searchstring nvarchar(25)
);
CREATE TABLE table2
(
id2 int IDENTITY(10, 10)
, searchstring2 nvarchar(25)
, newsearchstring nvarchar(50)
);
CREATE TABLE table3
(
id3 int IDENTITY(100, 100)
, id2 int
, table3srow nvarchar(25)
)
INSERT INTO table1 VALUES ('something');
INSERT INTO table1 VALUES ('something else');
INSERT INTO table1 VALUES ('something'); -- ID = 3, this row will be selected by 1st query
INSERT INTO table2 VALUES ('something', 'newvalue1');
INSERT INTO table2 VALUES ('something else', 'this will not be shown');
INSERT INTO table2 VALUES ('something', 'this will be returned by query 2'); -- ID = 30, this row will be selected by 2nd query
INSERT INTO table3 VALUES (10, 'not relevant');
INSERT INTO table3 VALUES (20, 'not relevant');
INSERT INTO table3 VALUES (30, 'This is from table 3'); -- This row will be returned by 3rd query
SELECT * FROM
(SELECT TOP 1 id, searchstring FROM table1 WHERE searchstring = 'something' and id = (SELECT MAX(id) FROM table1 WHERE searchstring = 'something')) AS query1,
(SELECT TOP 1 id2, newsearchstring FROM table2 WHERE searchstring2 = 'something' and id2 = (SELECT MAX(id2) FROM table2 WHERE searchstring2 = 'something')) AS query2,
(SELECT id2, table3srow FROM table3) as query3
WHERE query3.id2 = query2.id2
Use the same approach for table4 as indicated for table3.
Related
Table 1:
Id1 Data1
123123 David
123124 Jan
1231344 Juro
1234126 Marco
Table 2:
Id2 Data2
1231230 Info 1
1231231 Info 2
1231232 Info 3
1231240 Info 4
1231241 Info 5
1231242 Info 6
Each id from Table 1 can have 1 or more matches in Table 2 based on first 6 digits.
For example 123123 from Table 1 matches 1231230, 1231231 and 1231232 in Table 2.
I'm trying to create join to match maximum id2 from Table 2 based on id1 from Table 1.
I would just join using LIKE:
SELECT
tb1.id1,
tb1.data1
MAX(tb2.[id2]) AS id2
FROM [dbo].[table1] tb1
LEFT JOIN [dbo].[table2] tb2
ON tb2.[id2] LIKE CONCAT(tb1.[id1], '%')
GROUP BY
tb1.id1,
tb1.data1
ORDER BY
tb1.id1 DESC;
This approach might still leave open the possibility of using an index on the second table. In any case, it is slightly easier to read than your version.
This is working solution:
SELECT tb1.*,
MAX(tb2.[id2]) as id2
FROM [dbo].[table1] tb1
LEFT JOIN [dbo].[table2] tb2
ON CASE
WHEN LEN(tb1.[id1]) = 7 and tb1.[id1] = tb2.[id2] THEN 1
WHEN LEN(tb1.[id1]) = 6 and tb1.[id1] = SUBSTRING(tb2.[id2],1,6) THEN 1
ELSE 0
END = 1
GROUP BY tb1.id1
,tb1.data1
ORDER BY tb1.id1 desc
You can try this as well:
Declare #t table (id1 varchar(50) , data1 varchar(50))
insert into #t values (123123,'David')
insert into #t values (123124,'Jan')
insert into #t values (1231344,'Juro')
Declare #t1 table (id2 varchar(50) , data2 varchar(50))
insert into #t1 values (1231230,'Info 1')
insert into #t1 values (1231231,'Info 2')
insert into #t1 values (1231232,'Info 3')
insert into #t1 values (1231240,'Info 4')
insert into #t1 values (1231241,'Info 5')
insert into #t1 values (1231242,'Info 6')
select * from #t a JOIN #t1 B
on b.id2 like '%' + a.id1 + '%'
I have below tables:
table1 #TempImagepath
column1 Path nvarchare(800)
table2 SiteImage
column1 SiteID bigint,
column2 Facebookurl nvarchare(800),
column3 Twitterurl nvarchare(800),
column4 Instaurl nvarchare(800)
I want to insert data from table2 as different rows into table1 for (Facebookurl,Twitterurl,Instaurl) Where SiteID='10'
Lets say there is one record in table2(SiteImage) as:
(10,"/uploads/Sites/1/CategoryImages/WebImages/7ec79e1a-92c2-4d7c-9139-6d177004d766-201701311804409066.jpg","/uploads/Sites/1/CategoryImages/MobileImages/e5ae525f-7dcf-4051-8463-6bb15f520860-201701311804425434.jpg","/uploads/Sites/1/CategoryImages/MobileImages/31d89a5e-5593-4074-881f-d3326b5cf105-201701311804444181.jpg")
Then my result shoul give records for table1(#TempImagepath) Something Like:
"/uploads/Sites/1/CategoryImages/WebImages/7ec79e1a-92c2-4d7c-9139-6d177004d766-201701311804409066.jpg"
"/uploads/Sites/1/CategoryImages/MobileImages/e5ae525f-7dcf-4051-8463-6bb15f520860-201701311804425434.jpg"
"/uploads/Sites/1/CategoryImages/MobileImages/31d89a5e-5593-4074-881f-d3326b5cf105-201701311804444181.jpg"
Try this,i think this might be useful to you
IF OBJECT_ID('Tempdb..#TempImagepath')IS NOT NULL
DROP TABLE #TempImagepath
IF OBJECT_ID('dbo.SiteImage')IS NOT NULL
DROP TABLE SiteImage
CREATE TABLE #TempImagepath
([Path] nvarchar(800))
CREATE TABLE SiteImage
(
SiteID bigint IDENTITY,
Facebookurl nvarchar(800),
Twitterurl nvarchar(800),
Instaurl nvarchar(800)
)
INSERT INTO SiteImage
SELECT 'Facebookurl','Twitterurl','Instaur'
INSERT INTO #TempImagepath
SELECT 'Row'+ CAST(ROW_NUMBER()OVER(ORDER BY (SELECT 1))AS Varchar(10))+': '+ [Path]
FROM SiteImage
CROSS APPLY (VALUES (Facebookurl),(Twitterurl),(Instaurl)
)AS A ([Path])
SELECT * FROM #TempImagepath
Result
Path
------------------
Row1: Facebookurl
Row2: Twitterurl
Row3: Instaur
I tried different ways and searched the web, but no luck so far.
I have query like this
SELECT *
FROM MainTable
WHERE exists (SELECT NULL FROM Table1.Column1 = MainTable.Column1 OR MainTable.Column1 is NULL)
AND exists (SELECT NULL FROM Table2.Column2 = MainTable.Column2 OR MainTable.Column2 is NULL)
AND exists (SELECT NULL FROM Table3.Column3 = MainTable.Column3 OR MainTable.Column3 is NULL)
AND exists (SELECT NULL FROM Table4.Column4 = MainTable.Column4 OR MainTable.Column4 is NULL)
This works fine when Table1, Table2, Table3 and Table4 are not empty. The real problem arises when any of the Table1, Table2, Table3 and Table4 is empty and its corresponding ManinTable column is NULL, then that record gets ignored. The record is considered as does not exist because the Is Null condition will not execute. I cannot even take Is Null condition out of exists () as well because then query will return wrong result.
I want to get records from MainTable which exists in Table1, Table2, Table3 and Table4 and also if corresponding column is null.
Edit: the following is the sample data and query which you can test and execute by yourself. To reproduce my problem just comment out the insertion query into Table4 and then execute, now it won't return any record because Table4 is empty hence Is Null for MainTable won't execute. So I will get no result.
CREATE Table #MainTable (Column1 INT NULL, Column2 INT NULL, Column3 INT NULL, Column4 INT NULL)
CREATE Table #Table1 (Column1 INT, Column2 INT, Column3 INT, Column4 INT)
CREATE Table #Table2 (Column1 INT, Column2 INT, Column3 INT, Column4 INT)
CREATE Table #Table3 (Column1 INT, Column2 INT, Column3 INT, Column4 INT)
CREATE Table #Table4 (Column1 INT, Column2 INT, Column3 INT, Column4 INT)
INSERT INTO #MainTable VALUES(1,2,3,NULL)
INSERT INTO #Table1 VALUES(1,2,3,4)
INSERT INTO #Table2 VALUES(1,2,3,4)
INSERT INTO #Table3 VALUES(1,2,3,4)
INSERT INTO #Table4 VALUES(1,2,3,4)
SELECT *
FROM #MainTable
WHERE exists (SELECT NULL FROM #Table1 WHERE Column1 = #MainTable.Column1 OR #MainTable.Column1 is NULL)
AND exists (SELECT NULL FROM #Table2 WHERE Column2 = #MainTable.Column2 OR #MainTable.Column2 is NULL)
AND exists (SELECT NULL FROM #Table3 WHERE Column3 = #MainTable.Column3 OR #MainTable.Column3 is NULL)
AND exists (SELECT NULL FROM #Table4 WHERE Column4 = #MainTable.Column4 OR #MainTable.Column4 is NULL)
Drop TABLE #MainTable
Drop TABLE #Table1
Drop TABLE #Table2
Drop TABLE #Table3
Drop TABLE #Table4
If I understand correct what you want, than you have to change your query like this:
SELECT *
FROM #MainTable
WHERE (Column1 is null or exists (SELECT NULL FROM #Table1 WHERE Column1 = #MainTable.Column1))
AND (Column2 is null or exists (SELECT NULL FROM #Table2 WHERE Column2 = #MainTable.Column2))
AND (Column3 is null or exists (SELECT NULL FROM #Table3 WHERE Column3 = #MainTable.Column3))
AND (Column4 is null or exists (SELECT NULL FROM #Table4 WHERE Column4 = #MainTable.Column4))
The problem with the original query is that is when the maintable.Column4 is null it was doing
WHERE Column4 = #MainTable.Column4
which is translated to
WHERE Column4 = NULL
and you cannot do = on a null value
I have 3 columns in Oracle database having table mytable and i want records having only duplicate values in 2nd and 3rd column.
SQL> select * from mytable ;
column1 column2 column3
A 50 50----required output
A 10 20----have different values i.e. 10 and 20
A 50 50----required output
A 30 70----have different values i.e. 30 and 70
B 20 20----required output
B 40 30----have different values i.e. 40 and 30
I want the following output with count(*):
column1 column2 column3
A 50 50
A 50 50
B 20 20
Any help is much appreciated
select column1, count (*)
from mytable
where column2 = column3
group by column1, column2;
From your question it is not clear about primary key as A in First Column is being repeated many times.
You can try the following:
select column1, column2, column3, count(*) from
mytable where column2 = column3 group by column1, column2, column3;
Here are sample example , i am doing this SQL Server but i am sure this query work in ORACLE also
EXAMPLE :
Create table #Test (colA int not null, colB int not null, colC int not null, id int not null identity) on [Primary]
GO
INSERT INTO #Test (colA,colB,colC) VALUES (1,1,1)
INSERT INTO #Test (colA,colB,colC) VALUES (1,1,1)
INSERT INTO #Test (colA,colB,colC) VALUES (1,1,1)
INSERT INTO #Test (colA,colB,colC) VALUES (1,2,3)
INSERT INTO #Test (colA,colB,colC) VALUES (1,2,3)
INSERT INTO #Test (colA,colB,colC) VALUES (1,2,3)
INSERT INTO #Test (colA,colB,colC) VALUES (4,5,6)
GO
Select * from #Test
GO
select count(colA) as tot_duplicate_count , colA ,colB ,colC from #Test where id <=
(Select Max(id) from #Test t where #Test.colA = t.colA and
#Test.colB = t.colB and
#Test.colC = t.colC)
group by colA ,colB ,colC
having count(colA) > 1
This query this total count of duplicate record per data row
When I execute the following code, I'm getting results such as:
ID column1 column2
34 NULL NULL
34 Org13 Org13
36 NULL NULL
36 NULL Org2
36 Org4 NULL
41 NULL NULL
41 NULL Org5
41 Org3 NULL
I want my results to look like:
ID column1 column2
34 Org13 Org13
36 Org4 Org2
41 Org3 Org5
I've got two tables: Table1 and Table2. Table2 is a lookup table with the following fields: id, name
Table1 has the following fields (id, column1, column2). column1 and column2 both have foreign key relationships to the lookup table:
FK_1: Table1.column1-Table2.id
FK_2: Table1.column2-Table2.id
Since I want to pull out the values for column1 and column2, and since both of these values are lookups on the same field (Table2.name), I suspect I need to do inner Selects.
My code is below. How can I change this so that it produces the results desired, instead of the ones I'm getting? Thanks in advance!
DECLARE #value INT
SET #value = 14
SELECT DISTINCT
Table1.[id] AS ID
, ( SELECT DISTINCT
Table2.[name]
WHERE
Table1.column1 =
Table2.id ) AS column1
, ( SELECT DISTINCT
Table2.[name]
WHERE
Table1.column2 =
Table2.id ) AS column2
FROM
Table1
,Table2
WHERE
Table1.[id] = #value
/*
create table table1(id int, col1 int, col2 int);
create table table2(id int, name varchar(10) );
insert into table2 values(1, 'org 1');
insert into table2 values(2, 'org 2');
insert into table2 values(3, 'org 3');
insert into table2 values(4, 'org 4');
insert into table1 values(1, 1, 2);
insert into table1 values(2, 2, 2);
insert into table1 values(3, 2, 3);
insert into table1 values(4, 4, 1);
*/
select
a.id,
b.name as column1,
c.name as column2
from
table1 a
join table2 b on b.id = a.col1
join table2 c on c.id = a.col2;
id column1 column2
----- ---------- ----------
1 org 1 org 2
2 org 2 org 2
3 org 2 org 3
4 org 4 org 1
4 record(s) selected [Fetch MetaData: 3/ms] [Fetch Data: 0/ms]
[Executed: 7/7/09 4:07:25 PM EDT ] [Execution: 1/ms]
gbn, I think you meant to write
DECLARE #value INT
SET #value = 1
SELECT --??? DISTINCT
t1.[id] AS ID, --- missed comma
table2a.name,
table2b.name
FROM
Table1 t1
JOIN Table2 table2a ON t1.column1 = table2a.id
JOIN Table2 table2b ON t1.column2 = table2b.id -- you have t1.column1 oops
WHERE
t1.[id] = #value
DECLARE #value INT
SET #value = 14
SELECT
t1.[id] AS ID
MAX(t2a.name),
MAX(t2b.name)
FROM
Table1 t1
LEFT JOIN
Table2 t2a ON t1.column1 = t2a.id
LEFT JOIN
Table2 t2b ON t1.column2 = t2b.id
WHERE
t1.[id] = #value
GROUP BY
t1.[id]