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.
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 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";
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'
)
I have the following tables:
Table A
Field Name Field Value
Column1 Column1value
Column2 Column2value
Column3 Column3value
Column4 Column4value
Table B
Column1 Column2 Column3 Column4
Column1value Column2value Column3value Column4value
How do I write a query to generate Table B from Table A?
If you need, using the next query you can get the column names from TableA:
SELECT
name
FROM
syscolumns
WHERE
id = (SELECT id FROM sysobjects WHERE xtype='U' and NAME='TableA')
but if you just want to copy TableA to TableB, use the
SELECT
*
INTO TableB
FROM TableA
You could use SELECT INTO and the resulting TableB below will have the same data and column types as the source table:
SELECT
Column1value,
Column2value,
Column3value,
Column4value
INTO TableB
FROM TableA
I have two tables with columns of my interests as Table1.Column1 and Table2.Column2
Table1 is Kind of a Group Table and Table2 is items table. The join query of these two tables gets the data in the followinf format
Column1 Column2
A 1
A 2
B 1
B 2
B 3
What I want is to get data in the following format:
Column1 Column2
A 0
A 1
A 2
B 0
B 1
B 2
B 3
i.e. getting extra 0 for each group at the start each time. The 0 does not exits in the database.
Does anyone know how to achive this in SQL?
Many Thanks,
This is one way to do it.
SELECT DISTINCT Column1, [Column2] = 0
FROM (
YourOriginalQuery
) q
UNION ALL
YourOriginalQuery
Most likely, there are better solutions by incorporating this requirement into your original query. If you post your query, we can come up with better alternatives.
Or something like:
select C.CategoryId, drv.CategoryGroupId from Category as C
cross join (
select 0 as CategoryGroupId
UNION
select CG.CategoryGroupId from CategoryGroup as CG
)drv order by CategoryId, CategoryGroupId