Attach query string to results - sql

When doing a sql query with something like
SELECT * FROM table1 WHERE column1 LIKE 'searchstring';
Is there a way to attach the query string, here 'searchstring' to the results? I ask because in this case
SELECT * FROM table1 WHERE column1 LIKE '%searchstring1%' OR column1 LIKE '%searchstring2%';
you can get multiple rows back in the result, and you may not know which rows go with which query string.
Is there a way to attach the query strings to their associated rows in another column? For example,
result_col1 result_col2 searched
a b searchstring1
c d searchstring1
f g searchstring2

You can include the searchstring as a column in your query and obtain them in your results:
SELECT 'searchstring' AS querystring, * FROM table1 WHERE column1 LIKE '%searchstring%';

Related

How to get the header out of Select query execution in snowflake

How to get header name from select query execution in snowflake. Currently I am getting only values out of select query execution. is there way to get column name as well. I need to group by and aggregate function on top of the select query result.
Code tried
sql10 = f"""SELECT col1,col2,col3,col4 FROM tablename ORDER BY col4 ;"""
select_snow =cs.execute(sql10).fetchall()
snow_col = [(c[1],c[2]) for c in select_snow]
how to get the columns name and mapped to particular column value.
Output
select snow: [('value1','value12','value3','value4'), ('value1','value12','value3','value4'), ('value11','value12','value13','value14'), ('value21','value22','value23','value24')]

How do I select column values by selective pattern matching from a subquery

##Using SQL server
How do I select column values by selective pattern matching from a subquery?
For Example, I have data in the format column1 = '123/2b3/abcdefghw1/stuvwxyz28' I would like to get output from below query with matching values in subquery as column1 like '%abcdefgh%stuvwxyz%'
my sql query is something like this:
select * from table1
where column1 in (select coulmn1 from table2
where column2 is not null)
so, the above subquery would be returning multiple values of column1 in the format '123/2b3/abcdefghw1/stuvwxyz28', then outer query should be returning values from table1 where column1 matches pattern '%abcdefgh%stuvwxyz%'
Now I can do this for single value but I would like to generalize this pattern.
I am using below to extract subparts of columns values as :
left(split(column1,'/')[2],8) which gives output as 'abcdefgh' for above example
left(split(column1,'/')[3],8) which gives output as 'stuvwxyz' for above example
I am sure I should be using concat with above logics to generate the pattern but I am not quite sure how to do that.
*****************************************************************z
Please refer below example to better understand my goal:
There are three tables:
TableA
row_numA,Column1,column2,column3
1,101/212B/EDSVVJABHG1/WDNHNJCNHG3,4,a1
2,101/4VC3/PHLAPACBHG1/PHLAPA40HG2,null,a3
3,701/11G5/LSAJCAWZPQ2/LSANCARCPL1,6,b1
4,102/2NJGF/AMSTNLLRHG2/AMSTNLPXHG1,2,b4
5,101/VKYR/JRCYNJ67WDD/NYCMNYZRWDN,null,c5
TableB
row_numB,name,column1,id
1,b4,101/212B/EDSVVJABHG1/WDNHNJCNHG3,null
2,c5,101/4VC3/PHLAPACBHG1/PHLAPA40HG2,id2
3,a1,701/11G5/LSAJCAWZPQ2/LSANCARCPL1,null
4,a3,102/2NJGF/AMSTNLLRHG2/AMSTNLPXHG1,null
5,b1,101/VKYR/JRCYNJ67WDD/NYCMNYZRWDN,id5
6,b2,101/212B/EDSVVJABHJ1/WDNHNJCNHB3,id6
7,a4,701/11G5/LSAJCAWZPR4/LSANCARCPF2,id7
8,c2,701/11G5/LSAJCAWZPU3/LSANCARCPS5,null
9,d1,102/2NJGF/AMSTNLLRHK4/AMSTNLPXHH7,id9
TableC
row_numC,c_id,c_len
1,id3,null
2,id4,11
3,null,23
4,id5,null
5,id6,7
6,null,null
7,null,5
8,id7,10
9,id8,null
Goal_1: select coulmn1 from tableA where column2 is not null
Expected output:
Column1
101/212B/EDSVVJABHG1/WDNHNJCNHG3
701/11G5/LSAJCAWZPQ2/LSANCARCPL1
102/2NJGF/AMSTNLLRHG2/AMSTNLPXHG1
Goal_2: Using above output match with column1 in tableB using string matching like: %EDSVVJAB%WDNHNJCN% ("%"<"first 8 values after second /">%<"first 8 values after third /">%)
Expected output from tableB:
row_numB,name,column1,id
1,b4,101/212B/EDSVVJABHG1/WDNHNJCNHG3,null
6,b2,761/325L/EDSVVJABHJ1/WDNHNJCNHB3,id6
4,a3,158/277GF/AMSTNLLRHG2/AMSTNLPXHG1,null
9,d1,102/2NJGF/AMSTNLLRHK4/AMSTNLPXHH7,id9
3,a1,901/11G5/LSAJCAWZPQ2/LSANCARCPL1,null
7,a4,725/12K5/LSAJCAWZPR4/LSANCARCPF2,id7
8,c2,881/12LP/LSAJCAWZPU3/LSANCARCPS5,null
Goal_3: select data Where id field is populated for common highlighted values of column1
Expected output:
row_numB,name,column1,id
6,b2,101/212B/EDSVVJABHJ1/WDNHNJCNHB3,id6
9,d1,102/2NJGF/AMSTNLLRHK4/AMSTNLPXHH7,id9
7,a4,701/11G5/LSAJCAWZPR4/LSANCARCPF2,id7
Goal_4:
Join above output with TableC to get c_len not null values
Expected output from tableC:
row_numC,c_id,c_len
5,id6,7
8,id7,10
This is the final result set required.
I am seeking help in achieving goal_2 and goal_3 (preferably in single query)

How to search a row to see if any of the columns contain a certain string? In SQL

I have a table with 10 columns that each contain string values. I want to run a query that will return any of the rows which have any column value that matches a given string or set of strings.
Is there any way to do this?
The DBMS is MsSQL
if you want exact match, you can use IN keyword and check in all columns
SELECT *
FROM tablename
WHERE 'your string' IN (column1, column2, )
if you want partial match then you have to use LIKE
SELECT *
FROM tablename
WHERE column1 LIKE '%your string%' or column2 LIKE '%your string%' ...
or you can add all columns and do one LIKE check
SELECT *
FROM tablename
WHERE CONCAT(column1,'#',column2,'#',column3,'#',...) LIKE '%your string%'

SQL selection from one of four columns?

I have a database table with four columns:
Column 1 | Column 2 | Column 3 | Column 4
The value in each of those four columns will either be a price such as ($3.99) or will have the value 'Listed'.
I would like to know the best SQL query to find the following:
Get all rows where all four rows equal 'Listed'
Get all rows where some of the columns have prices and some have the value 'Listed'
Try this Query !
When all four rows equals to 'Listed' :
SELECT *
FROM [table name]
WHERE
Column1='Listed'
AND
Column2='Listed'
AND
Column3='Listed'
AND
Column4='Listed'
When some of the columns have prices and some have the value 'Listed' :
SELECT *
FROM [table name]
WHERE
(
Column1='Listed'
OR
Column2='Listed'
OR
Column3='Listed'
OR
Column4='Listed'
)
AND
(
Column1 LIKE '$%.%'
OR
Column2 LIKE '$%.%'
OR
Column3 LIKE '$%.%'
OR
Column4 LIKE '$%.%'
)
These are actually fairly simple queries
Select where all 4 columns are listed.
SELECT * FROM {table}
WHERE Column1='Listed' AND Column2='Listed' AND Column3='Listed' AND Column4='Listed'
Select where at least one column is listed and at least one column is a price(numeric)
SELECT * FROM {table}
WHERE (Column1='Listed' OR Column2='Listed' OR Column3='Listed' OR Column4='Listed')
AND (ISNUMERIC(Column1) OR ISNUMERIC(Column2) OR ISNUMERIC(Column3) OR ISNUMERIC(Column4))
Something like these should give you what you want, no guarantees though since different database engines use some slightly different SQL.
I think you should do something like this:
SELECT * FROM `testing` WHERE Col1='Listed' AND Col2='Listed' AND Col3='Listed' AND Col4='Listed'
And for the "OR" statement:
SELECT * FROM `testing` WHERE Col1='Listed' OR Col2='Listed' OR Col3='Listed' OR Col4='Listed'
Of cours you'll need to replace Col1, Col2, Col3 and Col4 with your Column names. Also change your table name.

Minus operator in sql

I am trying to create a sql query with minus.
I have query1 which returns 28 rows with 2 columns
I have query2 which returns 22 row2 with same 2 columns in query 2.
when I create a query query1 minus query 2 it should have only show the 28-22=6 rows.
But it showing up all the 28 rows returned by query1.
Please advise.
Try using EXCEPT instead of MINUS. For Example:
Lets consider a case where you want to find out what tasks are in a table that haven't been assigned to you(So basically you are trying to find what tasks could be available to do).
SELECT TaskID, TaskType
FROM Tasks
EXCEPT
SELECT TaskID, TaskType
FROM Tasks
WHERE Username = 'Vidya'
That would return all the tasks that haven't been assigned to you. Hope that helps.
If MINUS won't work for you, the general form you want is the main query in the outer select and a variation of the other query in a not exists clause.
select <insert list of fields here>
from mytable a
join myothertable b
on b.aId = a.aid
where not exists (select * from tablec c where a.aid = c.aid)
The fields might not be exactly alike. may be one of the fields is char(10) and the other is char(20) and they both have the string "TEST" in them. They might "look" the same.
If the database you are working on supports "INTERSECT", try this query and see how many are perfectly matching results.
select field1, field2 from table1
intersect
select field1, field2 from table2
To get the results you are expecting, this query should give you 22 rows.
something like this:
select field1, field2, . field_n
from tables
MINUS
select field1, field2, . field_n
from tables;
MINUS works on the same principle as it does in the set operations. Suppose if you have set A and B,
A = {1,2,3,4}; B = {3,5,6}
then, A-B = {1,2,4}
If A = {1,3,5} and B = {2,4,6}
then, A-B = {1,3,5}. Here the count(A) before and after the MINUS operation will be the same, as it does not contain any overlapping terms with set B.
On similar lines, may be the result set obtained in query 2 may not have matching terms with the result of query1. Hence you are still getting 28 instead of 6 rows.
Hope this helps.
It returns the difference records in the upper query which are not contained by the second query.
In your case for example
A={1,2,3,4,5...28} AND B={29,30} then A-B={1,2,3....28}