SELECT id, name::tsvector ## 'substring1 & substring2'::tsquery
FROM table
I have this. The result of this query has 2 fields: id, ?column?
The ?column? field value is true or false.
how can I get only the true results?
SELECT * FROM (
SELECT id, name::tsvector ## 'substring1 & substring2'::tsquery
FROM table
) AS search
WHERE column = 't'
You can also use WITH: http://www.postgresql.org/docs/9.1/static/queries-with.html
use WHERE clause.... (if i understand your need correctly)
WHERE condition = true
Add a WHERE clausure to fill this values. Something like this.
SELECT id, name::tsvector ## 'substring1 & substring2'::tsquery FROM table WHERE ?column? = 1 (or True)
use alias to reference your column
SELECT id, name::tsvector ## 'substring1 & substring2'::tsquery AS condition
FROM table
WHERE condition = true
Related
I am working with the JSON_VALUE function and I need a kind of dynamic query
I have a column called Criteria and sometimes it has 1 value but sometimes it has 2 or 3 vales like:
Example of 1 value: $.IRId = 1
Example of 2 values: $.IROwner = 'james.jonson#domain.com' AND DaysTillDue < 10
So in order to read the values from a JSON column and taking the Criteria column I am using this logic:
DECLARE #CriteriaValue int
,#CriteriaStatement VARCHAR(50)
SELECT #CriteriaValue=SUBSTRING(Criteria, CHARINDEX('=',Criteria)+1, len(Criteria)) FROM #SubscriptionCriteria;
SELECT #CriteriaStatement= SUBSTRING(Criteria,0, CHARINDEX('=',Criteria)) FROM #SubscriptionCriteria;
SELECT #CriteriaValue,#CriteriaStatement
SELECT *
FROM [SAAS].[ObjectEvent]
WHERE
JSON_VALUE(JSONMessageData, #CriteriaStatement) = #CriteriaValue
That SQL code is taking only the Criteria Column with only 1 value ($.IRId = 1), but the idea is to have something that reads the criteria no matter the different filters and apply them into the final query. The idea I have is that the query would look like this:
SELECT *
FROM [SAAS].[ObjectEvent]
WHERE
JSON_VALUE(JSONMessageData, #CriteriaStatement1) = #CriteriaValue1 ADN JSON_VALUE(JSONMessageData, #CriteriaStatement2) = #CriteriaValue2 AND
JSON_VALUE(JSONMessageData, #CriteriaStatement3) = #CriteriaValue3
ETC
Any suggestion?
I am trying to write a google BigQuery to select some records based on this condition:
if ID_parameter is not null:
SELECT * WHERE ID=ID_parameter
else:
SELECT * WHERE country_id=country_id_parameter and (name LIKE %name_parameter% OR name is null)
The name_parameter should be optional but one of the ID_parameter or country_id_parameter must be provided.
Sorry, I am a newbie with SQL but I hope I stated what I want clearly.
consider below approach
select *
from your_table
where case
when not id_parameter is null then id = id_parameter
else country_id = country_id_parameter and (regexp_contains(name, name_parameter) or name is null)
end
I miss the table name in the query, let's assume it's 'personnel'.
Suppose ID-parameter = 200, country_id_parameter = 'NL', name_parameter = '%bart%'
Maybe like this when the name is 'bart':
select * from personnel
where ID=200
or (ID=NULL and
country_id='NL' and
name LIKE '%bart%')
Use this when the name is NULL:
select * from personnel
where ID=200
or (ID=NULL and
country_id='NL')
Hello I have Filter option in my program.
When i use an single option like when i filter only by STATUS=SCHEDULED i get the correct list as shown bellow
But when i give multiple condition then the SQl query returns more additional rows irrelevant to the date. like bellow
i am trying to filter the order with STATUS=SCHEDULED and CUSTOMER ID=87.
I took reference from here1
here2
And bellow is my SQL query
SELECT
*
FROM
workforce_customerorder
WHERE
ORDER_ID LIKE '$sOrder'
UNION
SELECT
*
FROM
workforce_customerorder
WHERE
CUSTOMER_ID LIKE '%$sCustomerID%'
UNION
SELECT
*
FROM
workforce_customerorder
WHERE
AGENT_NUMBER LIKE '%$sAgentNumber%'
UNION
SELECT
*
FROM
workforce_customerorder
WHERE
STATUS LIKE
'$sStatus'
UNION
SELECT
*
FROM
workforce_customerorder
WHERE
GST_NUMBER LIKE '$sGST'
UNION
SELECT
*
FROM
workforce_customerorder
WHERE
DATE(ORDER_DATE) BETWEEN '$sOrderDateFrom' AND '$sOrderDateTo'
I need the best SQl query. Thanks in advance
Ok After I tried using AND this what i got
You should have just one query, with a WHERE clause that performs the filtering on only the columns that are set
SELECT *
FROM workforce_customerorder
WHERE
(ORDER_ID = COALESCE('$sOrder', ORDER_ID)) AND
(CUSTOMER_ID LIKE '%$sCustomerID%' OR '$sCustomerID' IS NULL) AND
(AGENT_NUMBER LIKE '%$sAgentNumber%' OR '$sAgentNumber' IS NULL) AND
(STATUS = COALESCE('$sStatus', STATUS)) AND
(GST_NUMBER = COALESCE('$sGST', GST_NUMBER)) AND
(DATE(ORDER_DATE) BETWEEN COALESCE('$sOrderDateFrom', ORDER_DATE) AND COALESCE('$sOrderDateTo', ORDER_DATE))
Note that LIKE without a wildcard is equivalent to =. I find it clearer to specify which are exact matches and which are sub-matches with different syntaxes.
Don't use a UNION. You should be using AND in a single WHERE clause.
This behavior you are witnessing is due to use of UNION, Omit UNION and use AND like:
SELECT * FROM workforce_customerorder
WHERE
ORDER_ID LIKE '$sOrder'
AND
CUSTOMER_ID LIKE '%$sCustomerID%'
AND
AGENT_NUMBER LIKE '%$sAgentNumber%'
....
When using UNION although one of the subsets may be empty you get the results from all other non empty subsets
If you are doing a UNION that means give me anything in either of these sets, or in both. Try using just AND if you want two+ conditions to be true
Using UNION, you merge your columns. Because you use the same data table in your unions, you have the same column multiple times. You should use AND instead in the WHEREstatement. Below is the corrected query.
SELECT *
FROM workforce_customerorder
WHERE
ORDER_ID LIKE '$sOrder'
AND CUSTOMER_ID LIKE '%$sCustomerID%'
AND AGENT_NUMBER LIKE '%$sAgentNumber%'
AND STATUS LIKE '$sStatus'
AND GST_NUMBER LIKE '$sGST'
AND DATE(ORDER_DATE) BETWEEN '$sOrderDateFrom' AND '$sOrderDateTo'
ok finally i found the solution
$query_order_id = ($sOrder != "") ? " AND (ORDER_ID LIKE '$sOrder') " : "";
$query_customer_id = ($sCustomerID != "") ? " AND (CUSTOMER_ID LIKE '%".$sCustomerID."' OR '".$sCustomerID."' IS NULL) " : "";
$query_sAgentNumber = ($sAgentNumber != "") ? " AND (AGENT_NUMBER LIKE '%".$sAgentNumber."%' OR '".$sAgentNumber."' IS NULL) " : "";
$query_sStatus = ($sStatus != "") ? " AND (STATUS LIKE '%".$sStatus."%' OR '".$sStatus."' IS NULL) " : "";
$query_sGST = ($sGST != "") ? " AND (GST_NUMBER LIKE '%".$sGST."%' OR '".$sGST."' IS NULL) " : "";
$query_sOrderDateFrom = ($sOrderDateFrom != "") ? " AND (DATE(ORDER_DATE) BETWEEN COALESCE('$sOrderDateFrom', ORDER_DATE) AND COALESCE('$sOrderDateTo', ORDER_DATE)) " : "";
//sql query here
$sql = "SELECT * FROM workforce_customerorder where ORDER_ID IS NOT NULL".$query_order_id.$query_customer_id.$query_sAgentNumber.$query_sStatus.$query_sGST.$query_sOrderDateFrom;
}
It is same as what #Caleth showed but the one which matched exactly is by applying ternary operator before passing the variable to SQL query. Thank you every one you answers...
i have long store procedure in whcih i want Add filter on this Column
select (PaymentAmount-PaymentPosted) as Unapplied, BalancDue from ERAMaster
where Unapplied = 22
but i run the query they give me error invalid column name. any one tell me the write way to Add filter on Unapplied Column
You can do it like below :
select * from
(
select (PaymentAmount-PaymentPosted) as Unapplied, BalancDue from ERAMaster
) as T where Unapplied = 22
Or like this :
select (PaymentAmount-PaymentPosted) as Unapplied, BalancDue from ERAMaster
where PaymentAmount-PaymentPosted = 22
It should be like this:
select (PaymentAmount-PaymentPosted) as Unapplied, BalancDue from ERAMaster
where (PaymentAmount-PaymentPosted) = 22
'Unapplied' is not a column name it is only alias.
I have this SQL
SELECT devudp1.deviceoid,devudp1.valueType
FROM DeviceUdpValues devUDP1
WHERE devudp1.udpname='TestUDP'
and <<either bdvalue or string value based on user selected value datatype>>
Here in the Query based on the devudp1.valueType I want to append below attribute
If the valueType is 3 then I want to append my above select clause with devudp1.bdvalue ='10', else it should be appended by devudp1.bdvalue = 'Hello'
So the above query when valueType is 3 will look like
SELECT devudp1.deviceoid,devudp1.valueType
FROM DeviceUdpValues devUDP1
WHERE devudp1.udpname='TestUDP'
AND devudp1.bdvalue = '10'
else it will look like
SELECT devudp1.deviceoid,devudp1.valueType
FROM DeviceUdpValues devUDP1
WHERE devudp1.udpname='TestUDP'
AND devudp1.stringValue = 'Hello'
Can anyone suggest me how to put this logic in place
Try this:
SELECT devudp1.deviceoid,devudp1.valueType
FROM DeviceUdpValues devUDP1
WHERE devudp1.udpname='TestUDP'
AND (
(<USER-SELECTED-VALUE> = 3 AND devudp1.bdvalue ='10') OR
(<USER-SELECTED-VALUE> <> 3 AND devudp1.stringvalue ='Hello')
)