Backstory,
My company runs redundant call recording servers, each with a list of extensions.
We query these using SQL. I can see there is a 20+ extension difference between the two servers. These are columns that exist in the same table...so essentially I need to do the following:
Compare column1 data from column 2 'server1' in table system.name with column1 data from column 2 'server2' in table system.name and display those that DO NOT exist on both, but exist on one or the other.
Based on what I can undertand from your question
Select column1
from table1 a
where column2 = 'server1'
and not exists
(select *
from table1 b
where a.column1 = b.column1
and b.column2 = 'server2'
)
UNION
Select column1
from table1 a
where column2 = 'server2'
and not exists
(select *
from table1 b
where a.column1 = b.column1
and b.column2 = 'server1'
)
Related
So I have some tables with millions of rows of data, and the current query I have is like the following:
WITH first_table AS
(
SELECT
A.column1, A.column2, B.column1 AS column3, C.column1 AS column4
FROM
tableA AS A
LEFT JOIN
tableB AS B ON A.id = B.id
LEFT JOIN
tableC AS C ON A.id = C.id
UNION ALL
SELECT
D.column1, D.column2, NULL AS column3, D.column4
FROM
tableD AS D
UNION ALL
...
)
SELECT
column1, column2, column3, column4, A.col5, A.col6... until A.col20
FROM
first_table
LEFT JOIN
tableA AS A ON first_table.id = A.id
I'm basically appending two tables at least to table A and then joining again table A in the final SELECT statement. I do this because I need like 30 columns from table A and I don't want to fill with NULL values the append statement since I only need 4 or 5 columns from the tables appended to the main one (tableA).
I was wondering if it would be better to avoid the join and then fill all the columns I need since the WITH statement or should I keep my code as it is. All of this is for query performance and improve execution time.
Thanks.
I have 2 tables. These two tables have one-to-many relations.
TABLE - A
column1 column2
1 label1
2 label2
TABLE - B
Bcolumn1 Bcolumn2 Bcolumn3
1 value1 value4
1 value2 value5
2 value3 value6
RESULT TABLE
column1 column2 json
1 label1 [{"Bcolumn":value1,"Bcolumn":value4},{"Bcolumn":value2,"Bcolumn":value5}]
2 label2 [{"Bcolumn":value3,"Bcolumn":value6}]
I want to get RESULT TABLE1 using TABLE - A and TABLE - B.
how can I get this result?
Thank you.
Use SQLite's JSON1 Extension functions:
SELECT a.column1, a.column2,
json_group_array(json_object('Bcolumn', b.Bcolumn2, 'Bcolumn', b.Bcolumn3)) json
FROM tableA a INNER JOIN tableB b
ON b.Bcolumn1 = a.column1
GROUP BY a.column1;
See the demo.
What you are looking for in sqlite is the group_concat() function. It's tricky, cause you have he values you want to concat in 2 different columns. Basically you can do the following
select
a.column1
, a.column2
, '[{' || group_concat('"Bcolumn":' || b.bcolumn2 || '"Bcolumn":' || b.bcolumn3,'};{') || '}]' as json_output
from tablea a
inner join tableb b on
a.column1 = b.bcolumn1
group by
a.column1
, a.column2
;
I tested this solution with MSSQL 2019 and string_agg(), but from the documentation (https://www.sqlitetutorial.net/sqlite-group_concat/) this should work just as well in sqlite.
The trick is to use '};{' as separator, because like this, you will only have to care about the opening and closing brackets and nothing in the middle.
i am trying to change the IN clause with EXISTS clause but not getting the desired results
OLD SCRIPT
delete from ABC a
where column1 IN (select DISTINCT column1
from BCD b
where b.column2 = a.column2
and b.column3 = 'N')
or a.column3 = "Y";
when i am changing this to
delete from ABC a
where EXISTS (select column1
from BCD b
where b.column2 = a.column2
and b.column3 = 'N')
or a.column3 = "Y";
i am not getting the desired results i have doubt that it is due to "or" condition used in the last.
Need help to resolve this.
The problem is not due to OR condition.
Your OLD script is validating the values against COLUMN1
where column1 IN (select DISTINCT column1 from BCD b where b.column2=a.column2 and b.column3= 'N')
But, the other script checks if any records exist in the subquery.
where EXISTS (select column1 from BCD b where b.column2=a.column2 and b.column3= 'N')
To summarize, query with IN verifies the existence of returned values in column1, while EXISTS just checks if the subquery returns 0 or more rows.
You missed the column1 filter. Remember that the projected column in an exists subquery isn’t doing anything - you’re only checking that the subquery returns rows, not what they are. I typically use select null in (not) exists to make this obvious to the reader
delete from ABC a
where EXISTS
(select null
from BCD b
where b.column2=a.column2
And b.column1 = a.column1
and b.column3= 'N')
or a.column3= "Y";
I have a question about finding identical rows from one table to another. I have a table for users to ask for information. So with that the query will be ran against another table. Both tables are identical except for the ID columns.
The ID columns are not involved in the query except for the
SELECT TOP 1 *
FROM searchTable
ORDER BY searchid DESC
part.
My query looks like this
SELECT TOP 1 *
FROM searchTable
ORDER BY searchid DESC(SELECT A.column1, A.column2,..............
FROM dbo.searchTable A
WHERE EXISTS (SELECT * FROM realTable B
WHERE A.Column1 = B.Column1
AND A.Column2 = B.Column2,
.......
AND A.lastColumn = B.lastColumn))
What I get when running the query is the last entered query from the query table, which is correct, but I get all the rows listed from the realTable as if everything after WHERE EXISTS is pointless. What I need is the single row query from the queryTable to list all the rows that are identical to it from the realTable. Not all the rows the realTable has.
You can use inner join instead of exists.
select B.* from searchTable A
inner join realTable B
on A.Column1 = B.Column1
and A.Column2 = B.Column2
.
.
.
It will return all the records in your realTable which have identical columns with your searchTable.
I was able to get it to work the way I needed by understanding the logic in the last suggestion.
It looks like this.
DECLARE #searchID int = (SELECT MAX(searchID) FROM searchTable)
SELECT Column1, Column2.............LastColumn FROM realTable B
WHERE EXISTS(SELECT * FROM searchTable A WHERE searchID = #searchID AND A.Column1=B.Column1 AND A.Column2=B.Column2................A.LastColumn=B.LastColumn)
Now the last search in the searchTable will give me all the rows in the realTable that match that search.
I have two tables A(column1,column2) and B(column1,column2).
How to ensure that a value from A(column1) is not contained within B(column1) and insert it in this column.
My Query will be like this :
insert into B.column1 values()
where
...
I want to complete B.column1 with data from A.column1
What should i put in the where clause ?
Insert Into B(column1)
Select A.Column1
From A
Where A.Column1 not in (Select Column1 From B)
I would use MINUS command and select all rows from A(column1) which are not in B(column1) and then SELECT INTO result into B table.
insert into B
select a.column1, a.column2 from a
left join b
on a.column1 = b.column1
where b.column1 is null