Questions tagged [condition]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
1 vote
2 answers
22 views

Applying conditional statement in Oracle DB

In the Oracle DB there are three tables, see below. Table: 'People' Table: 'Dogs' Table: 'Cats' Here is a Fiddle with data to test : http://sqlfiddle.com/#!4/d7cb4 Depending on the result of a ...
user avatar
  • 177
1 vote
1 answer
44 views

Splitting up a few big tables into multiple ones and avoiding empty tables

I am in the process of restructurizing and unifying a large database in PostgreSQL. One part of this is to split up some large tables into smaller ones according to their value in 'key'. The function ...
user avatar
0 votes
0 answers
20 views

Do most real world join conditions involve no more than 2 columns?

In the database application I have worked on, all join conditions are binary, like select * from A join B join C on A.id=B.id and B.orderno = C.orderno Each condition, separated by "and", ...
user avatar
0 votes
1 answer
32 views

Stuck at inserting records into MariaDB conditionally

I know about insert ignore. That's of no use, because MariaDB does not consider NULL to be part of the uniqude index. Therefore I have to use if ... else. But this code does not run: select * from ...
user avatar
-1 votes
2 answers
50 views

How to conditionally pick a row based on status value?

I want to select all available and bought packs. An user can open two pages to buy one pack, this results in two rows in the payment table. One gets status 'Pending' and the one that is bought gets ...
user avatar
0 votes
0 answers
35 views

using lead() to modify next row data

So I have some data like this: And what I would like to do is modify the WorkDate using the following logic if lead(EndTime) = StartTime then lead(WorkDate) = WorkDate So for example R3 WorkDate ...
user avatar
  • 1
0 votes
1 answer
40 views

Fetch all rows and leave column value empty if no equality match

I have a query where I want to fetch all rows where the value in column x is between 400-600. Including in this I want a matching column value from antoher table. But I want all 200 rows between 400 ...
user avatar
  • 103
0 votes
3 answers
85 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 ...
user avatar
0 votes
1 answer
271 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 ...
user avatar
1 vote
2 answers
147 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 ...
user avatar
2 votes
2 answers
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 ...
user avatar
0 votes
1 answer
22 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 ...
user avatar
  • 131
2 votes
1 answer
342 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 ...
user avatar
0 votes
1 answer
51 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....
user avatar
  • 101
-3 votes
2 answers
64 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 ...
user avatar
4 votes
1 answer
127 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 ...
user avatar
  • 281
0 votes
0 answers
86 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. ...
user avatar
  • 111
0 votes
1 answer
488 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 ...
user avatar
-2 votes
3 answers
629 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 | ...
user avatar
  • 107
3 votes
1 answer
262 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 ...
user avatar
  • 219
0 votes
2 answers
36 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 ...
user avatar
  • 125
0 votes
1 answer
1k 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') ...
user avatar
  • 129
3 votes
3 answers
202 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 ...
user avatar
0 votes
2 answers
93 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 ...
user avatar
  • 103
1 vote
1 answer
339 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 ...
user avatar
  • 133
0 votes
1 answer
394 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, ...
user avatar
  • 56.2k
1 vote
1 answer
2k 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 ...
user avatar
  • 113
1 vote
0 answers
23 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 | -------------...
user avatar
  • 113
0 votes
1 answer
2k 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, ...
user avatar
2 votes
2 answers
335 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(@...
user avatar
3 votes
0 answers
5k 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 ...
user avatar
1 vote
1 answer
10k 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,...
user avatar
  • 37
0 votes
1 answer
112 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 ...
user avatar
9 votes
2 answers
13k 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 ...
user avatar
  • 317
6 votes
2 answers
10k 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 ...
user avatar
  • 317
-1 votes
1 answer
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 ...
user avatar
0 votes
1 answer
1k 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 ...
user avatar
  • 99
4 votes
1 answer
16k 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 --------- {} {} {}...
user avatar
  • 56.2k
2 votes
1 answer
550 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 | |----------|---------|--------|---------------|--------------...
user avatar
1 vote
1 answer
153 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 @...
user avatar
  • 3,546
16 votes
2 answers
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 ...
user avatar
  • 177
2 votes
2 answers
245 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 ...
user avatar
2 votes
2 answers
997 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 ...
user avatar
0 votes
2 answers
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; +----------------...
user avatar
1 vote
0 answers
104 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)? ...
user avatar
2 votes
1 answer
994 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 ...
user avatar
  • 165
11 votes
4 answers
5k 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: ...
user avatar
  • 3,135
0 votes
2 answers
133 views

SQL With Multiple Condition and Column

I have 3 tables: Employee +----+--------------+--------------------+----------------+ | Id | EmployeeName | EmployeePermission | Position | +----+--------------+--------------------+------------...
user avatar
  • 15
3 votes
2 answers
18k 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 ...
user avatar
0 votes
1 answer
51 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 ...
user avatar