Questions tagged [condition]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
0
votes
3answers
34 views

A flag (indicator) that shows for each row which conditions was used to select it

Suppose I have the table foo. I want to filter the rows of this table using different conditions (cond_1, cond_2, ..., cond_n - the order is important) combined in a disjunction, i.e.: SELECT * FROM ...
0
votes
1answer
31 views

Create a trigger to fill a field according to acondition in sql

I want to create a trigger to fill a field according to acondition as below description For example: I have a table for all insps that have attachments named: “inspectionAttachments” and I have ...
1
vote
2answers
21 views

Set column values based on condition statement

I want to add values to a new column based on: if board_stage is less than alight_stage then service_direction equals 1, to indicate Inbound; if not, then service_direction equals 0, indicating ...
2
votes
2answers
56 views

Where Clause for Results Appearing after Occurence

I'm wondering if it's possible to find records that only appear after the first occurrence of a where clause. I'm currently using PostgreSQL for this example but something not PostgreSQL specific ...
0
votes
1answer
17 views

return field name and number of rows which meet condition in one sentence

I have a table in postgres with 10 fields, and need to check for certain values in 3 of them, then return the field name which met the condition and the number of rows in each field. So it's something ...
2
votes
1answer
33 views

How to filter table 2 before joining to table 1 so that the result still includes all of table 1, even if no match in table 2?

The following returns all rows from table 1 where post_type = dlm_download. It groups like values from table 2 by the matching column in table 1 so that each row in table 1 has a maximum of one ...
0
votes
1answer
32 views

Prevent failure in conditional insert in mysql database

The infrastructure of our system looks like this. An AWS lambda function receives requests such as (accountId, .....). It creates an entry in the MySQL database using a newly generated UUID as caseId....
-3
votes
2answers
55 views

Multiple statement conditions using OR AND within the query

I have this survey table and I'm trying to gather the information of userid who answer Strongly agree to the question of I'm trying to watch my weight OR I love drinking Coffee Black AND must have ...
4
votes
1answer
118 views

Conditional selection in FROM clause

I have two tables: parent with columns identifier (pkey) and period; child with columns identifier (pkey), parent_identifier and period. There are additional columns, but I did not list them here as ...
0
votes
0answers
48 views

Condition table theory

Is there a good way to create a "table of conditions"? I have a huge table, where 4-5 columns are considered in a set of rules, and based on those columns default values must appear in other columns. ...
0
votes
1answer
284 views

Conditional Join in DB2

2 tables are given Table T_PRICE P_Price P_Type P_Route 15 red long 10 long 5 small Table T_ROUTE R_Type R_Route blue long red ...
-1
votes
3answers
281 views

Can a `Select` Subquery be conditionally returned? [closed]

Is it possible in Postgres to return a column(Resulting from a subquery) only when a condition matches? Query: SELECT (SELECT NOW()) AS FIRST, (SELECT NOW()) AS SECOND Outputs: | first | ...
2
votes
1answer
129 views

Does the “don't use errors for flow control” axiom apply to postgres?

In traditional programming there is an axiom that states, "do not use errors for flow control". A general example is to throw an error then catch the error instead of using an ordinary conditional ...
0
votes
2answers
23 views

Get slave status using sql query inside if-else condition

How can I stop the slave if it is already running using sql query statements only? So far I tried to do something like below but getting sql syntax error. I think it is due to the borders of result ...
0
votes
1answer
694 views

perform multiple conditional join on multiple tables

I have a situation where I need to get result based on conditional joins For example, SELECT CGI.SALES_BRAND_BSNSS_ID, CGI.SALES_BRAND_CD, NVL(MTRX. matrix_score_cd,'RV1') ...
3
votes
3answers
129 views

How to use IN clause in sql server so that it selects everything if nothing is provided for its list argument?

Assume I have a sql query like this SELECT * FROM TABLE where country in ('USA', 'UK') This query works as expected that is it selects all rows where country column is either 'USA' or 'UK'. Now ...
0
votes
2answers
31 views

Postgres - Only allow insert if TWO columns match with a separate table

I have three tables: a users table, a books table, and a chapters table. Each book has an id called identifier_id (for good business reasons that I cannot work around). This id is guaranteed to be ...
1
vote
1answer
192 views

In where clause, does MySQL continue checking conditions if first condition before AND falsy

I have a question about and AND inside a where clause of a query. Within the where clause, I have a subquery but it is after a few other, row value conditions, like validated = 1 AND (subquery). I ...
0
votes
1answer
174 views

IS NOT DISTINCT FROM vs row-wise equality with =

Looking at the below query, SELECT null IS NOT DISTINCT FROM null AS indf, null = null AS eq; indf | eq ------+---- t | From this we can see the result of the IS NOT DISTINCT FROM is true, ...
1
vote
1answer
1k views

MySql sort conditional based on column values

I have a table with products where there's a column for price and stock. I want the products to be display sorted by the following rules: All products with price = 0 go to the bottom; Then the ...
1
vote
0answers
19 views

How can I auto_increment main and sub category conditionnaly?

So I have a table of items as so |mainCtgry |subCtgry| --------------------- | 1 | 1 | --------------------- | 1 | 2 | --------------------- | 1 | 3 | -------------...
0
votes
1answer
1k views

Conditional where for postgres stored procedure

I want to create a conditional where clause using a PostgreSQL stored procedure. This is my original stored procedure : select distinct t.serialnumber as sn, t.productid as tid, ...
2
votes
2answers
191 views

In what direction SQL Server WHERE clause conditions are checked?

I have this query: SELECT RCodeID as 'ListItemID', RCode as 'ListItemName' FROM RCode WHERE RCode not like '%,%' AND ISNUMERIC(RCode) = 1 AND CAST(RCode as int) >= CAST(@...
3
votes
0answers
4k views

How to apply a condition on “count over partition”?

I'm using Oracle 11g. Assuming multiple consecutive entries (sorted by datetime) with the same place_id represent a single visit. With this table: datetime_id | user_id | place_id and I want to ...
1
vote
1answer
7k views

If else statement on join query

i am working with a Postgresql database having two tables: professionals[professional_id,user_id,account_id] users[user_id email] accounts[account_id email] professionals [professional_id,...
0
votes
1answer
81 views

MySQL select all records unless user must be present in favorites

I am having trouble to find the best solution for the following problem; Description: I want to select all records from an items table, unless it is for favorites only, in that case the user must be ...
7
votes
2answers
10k views

Conditional string concatenation in PostgreSQL

I have a table parcels which currently contains the columns owner_addr1, owner_addr2, owner_addr3. Sometimes, one or both of the latter two fields is empty. I want to combine them into a single new ...
5
votes
2answers
7k views

How to use a conditional ORDER BY in MySQL?

I have the table courses where we have some fields and also the field course(string), favorited(bool), favoritedDate(dateTime), dateUpload. The teacher uploads the course and this date is stored in ...
-1
votes
1answer
36 views

If true then insert event [closed]

I have an insert statement that inserts events into a table. I need it to NOT insert if these events contain specific words or even specific username executing the event. Thought this would be easy ...
0
votes
1answer
930 views

SSAS Calculated Member Conditional Count

I have a table representing Accidents and a column with Accident Severity ranging from 'Slight' to 'Fatal'. I want to create a measure counting Fatal Accidents depending on other columns on the ...
3
votes
1answer
11k views

How do I select arrays that are not empty?

Why is this so tricky, what is token set to that it isn't equal to null nor an empty string? SELECT lexemes FROM ts_debug('This is a title') WHERE alias = 'asciiword'; lexemes --------- {} {} {}...
2
votes
1answer
506 views

Matching multiple rows in a MySQL database

A custom built eCommerce store has the following two tables: orders | order_id | user_id | amount | shipping_info | date_time | |----------|---------|--------|---------------|--------------...
1
vote
1answer
100 views

Is it possible to switch queries based on an input in mySQL?

Is there a way to switch queries based on values? Let's say I have the below query: select pdate as 'Payment Date', ROUND(@sum_amount := @sum_amount + `amount`) AS 'Total Amount' from ( select @...
16
votes
2answers
5k views

NOT (a=1 AND b=1) vs (a<>1 AND b<>1)

In the WHERE clause of a SQL query I would expect these two conditions to have the same behavior: NOT (a=1 AND b=1) vs a<>1 AND b<>1 The first condition behaves as expected, and while ...
2
votes
2answers
137 views

If a record does not exist set a value

I am struggling with my statement in PostgreSQL. I have 3 tables: tbl - flags (labeled as a) a.email tbl - accounts (labeled as b) b.id b.email tbl - foo (labeled as c) c.account_id c.total My sql ...
2
votes
2answers
788 views

Updating whole table once, criteria : column_name and each value

I have a table with 606 rows. The first column is simply ID (starting at 1 to 606), the next column names are name1, other1, name2, other2, etc. until name100, other100. I'm trying to create a query ...
0
votes
2answers
4k views

How to combine “SELECT” and “REPLACE INTO” when combining two MySQL tables into another?

I've got data with measurements split between two different tables, but the data doesn't need to be present in both: mysql> SELECT * FROM t1; mysql> SELECT * FROM t2; +----------------...
1
vote
0answers
96 views

Tracking Changes

I have an SSRS report with hierarchies for the company and their contact info. As these hierarchies change overtime, is there a way to track what has changed since the last run(run on a weekly basis)? ...
2
votes
1answer
926 views

Conditional relationships? One-to-many relationship using a pivot table for only a type of entity

I have the following tables: users: - id - name - user_type_id (references id on user_types) neighborhoods: - id - name homes: - id - house_number - neighborhood_id (references id ...
11
votes
4answers
4k views

Invert a Boolean expression which can return UNKNOWN

Example I have a table ID myField ------------ 1 someValue 2 NULL 3 someOtherValue and a T-SQL Boolean expression which can evaluate to TRUE, FALSE or (due to SQL's ternary logic) UNKNOWN: ...
0
votes
2answers
117 views

SQL With Multiple Condition and Column

I have 3 tables: Employee +----+--------------+--------------------+----------------+ | Id | EmployeeName | EmployeePermission | Position | +----+--------------+--------------------+------------...
3
votes
2answers
15k views

Multiple WHERE conditions on same column =val1 but doesnt=val2 or val3

I am trying to filter a table which has NON-UNIQUE transaction id's, joined to a products table on 1 common column. The filter needs to be as follows: equal to :group1 AND NOT equal to :group2 AND ...
0
votes
1answer
47 views

Update row contents on some condition

For example, let's say I'm building a database for an auction system, in which various users can partake in both selling and bidding on different items, I'd want for an "Auction" instance to also ...
2
votes
1answer
123 views

Understanding performance of highly unique WHERE clauses

I've been struggling to understand how to deal with a specific type of performance issue that shows up very often in this scenario: when you are wanting to apply multiple filters in a query, but you ...
0
votes
2answers
312 views

mysql table replication on just some records?

I have a big database which contain a table with about 26 million records. Now i need to make some reports on it from a special period of time. So how to make a replication from that table from a ...
7
votes
3answers
1k views

Convert date in date list condition to list of date ranges

I want to look for all the records that occur on specific dates. SELECT * FROM table1 WHERE date(column) in ($date1, $date2, ...); However, as many of you, know this kind of comparison doesn't get ...
0
votes
1answer
76 views

Calculations involving thresholds between two tables

I have a table comment and a table price like below. The key columns in both tables are ticker_id, price_datetime and price_open. The last column threshold in table comment (which currently NULL) is ...
6
votes
1answer
7k views

Change constraint on column based on value of another

Is it possible to change a constraint on a column in postgres based on the value of another column? E.g. (pseudocode): CREATE TABLE transactions( id SERIAL PRIMARY KEY, type TXN_TYPE NOT NULL,...
1
vote
1answer
5k views

Select two aggregate functions on the same column using different criteria - performance

Dear stack exchange users, first question here. The database in question is a service database where a user hires a service. Now I would like to show the person that was hired an overview of how many ...
0
votes
1answer
12k views

Use if exists in postgresql [closed]

I know about the exists feature, but that does not help me with the given situation. What I want is: Check if some row exists, and if exists then check for an another row if row exists. Here's ...