PostgreSQL query where column is represented as a string - string

I'm using Retool, and trying to run a query where the column value comes from a drop-down list. The value output is a string, so my query looks like this:
select * from accounts where {{dropDownList.value}} ilike {{'%' + account_search_textInput.value + '%'}}
When the query runs, it is as follows:
select * from accounts where "first_name" ilike '%Adam%';
The double quotes around the column name first_name seem to be causing an issue but I don't think I can remove them. Is there any other way to successfully run the query where first_name can represent the column name rather than a string value?

Related

How can I write a query with an order by clause when the field I'm ordering by is based on the value of another column?

I'm trying to write a query where the order by clause depends on the value of a key named sortBy in a JSON column.
const userId = req.params.id
SELECT * FROM users
WHERE ...
ORDER BY (SELECT JSON_EXTRACT(preferences, '$.sortBy') FROM users where id=${userId})
I think the problem is that the subquery, which gets the value "last_active", which is one of the columns in the user table, is a text string enclosed in quotes, but the order by clause requires a string without quotes?

Python SQLite 3 query multiple query

I've a problem to in building a query for Python SQLite3 to do the following:
Count a word which appears in columns, if word appears more than 1 time count one.
I've attached a picture to illustrate my table format.
I tried this but the result still counts duplicate values with same ID.
"SELECT id, value, count(value) FROM table WHERE type like'%hi%' GROUP BY value ORDER BY COUNT(*)<1 DESC"
The result needs to be like:
Hi all you need can be achieved with GROUP BY clause.
This should help:
SELECT
id
,value
,1 AS cnt
FROM table
GROUP BY id, value
ORDER BY id
What you're looking for is DISTINCT clause or GROUP BY as mentioned by Peter.
for GROUP BY use this syntax:
SELECT
id
,value
,1 AS cnt
FROM table
GROUP BY id, value
for DISTINCT use this one:
SELECT DISTINCT
id
,value
,1 AS cnt
FROM table

How to find colon separated values from another Table in oracle sql query where clause

I want to return row value from a Table where another table contain Colon separated value.
Suppose
I have a Table name "Unit Name" that Contain unit_id, unit_name
and Table 2 is User_reg where contain User_id. user Id contain colon separator value. Like as 82:81:80
How can get unit name list from unit_name Table
SELECT
*
FROM
unit_name un
WHERE (select school from user_reg where user_mode = 4) is not null
and un.unit_id in
(SELECT regexp_substr( school, '[^:]+', 1, LEVEL ) FROM USER_REG
CONNECT BY regexp_substr( school, '[^:]+', 1, LEVEL ) IS NOT NULL );
If you run the following query, you'll have a delimited string converted to rows.
select * from table(apex_string.split('82:81:80',':'))

Using result of a set in a select query in python

I have a select query to fetch specific columns from a mysql table. These specific columns must come from the intersection of the two sets.
Suppose I have two sets
A = {'age', 'description', 'name', 'payment_mode', 'id'}
# has all the column names of the table
and
B={'name', 'id', 'class'}
# input column names coming from a file (where values can be changed)
Now I do
inter= A.intersection(B)
# would result in {'name', 'id'}
So I would want the select query to be select {inter} from customers.
How can a write a dynamic query to get the column names to come from the result of the intersection?
Since you already have the desired columns in the set inter, all you need to do is construct the query string.
In Python 3.6 and above, you can use f-strings to generate the query as follows:
query = f"select {', '.join(inter)} from customers"
Or, you can do normal concatenation as follows:
query = 'select ' + ', '.join(inter) + ' from customers'
Both cases will result in query being "select name, id from customers" when inter is {'name', 'id'}

Geting prompt values from another query through functions

I am a beginner in cognos 10. I want to know how to get the value of a prompt from a Query1 and use it on Query2. My requirement is I want a prompt to ask the year for which I want the data in Query1 and in Query2 I want the data relevant to the previous year to that entered in the prompt. How to do that?
You can use the same parameter (tied to the prompt) in filters in both queries. If your parameter is Parameter1 and contains a four-digit year, and your data item in your filter is [Year] then your Query1 filter might look like this:
[Year] = ?Parameter1?
Your Query2 filter would be:
[Year] = ?Parameter1? - 1
Depending on your data source you may have to cast the string parameter to an integer before doing the subtraction though most SQL implementations will implicitly convert the string parameter to an integer for you.

Resources