I have a database table with two foreign keys pointing to two different tables. The business logic requires an “either or” relationship; only one foreign key can be NOT NULL at any given time. Here are the four possible states the foreign keys can hold based on the business logic: NULL, NULL – okay number, NULL – okay NULL, number
Tag: database
Joining one table twice and counting records
I have 2 tables. One is master and thee other is a lookup. TblMstr and Cmaster Tblmstr Cmaster I need to get how many Registered Nurse for each question have Strongly Agree, Disagree and Agree. Similarly for Nurse Practitioners and Pharmacists. Answer Hmmm . . . You can join twice. Once to get the occupation and once to get the
UNION ALL not combining rows
I have this table called “valores_indices” where it imports stocks values every 1 hour, I need to get the values from each monitored stock at 8am and 6pm, those being respectively my “vlrAberto” and “vlrFechado” selects I don’t get why these are not merging, I’ve done it before, any help would be appreciated since I’m running out of ideas, I’ve
SQL – Update multiple tables, the same column across one query
I have a postgres database There are 3 tables, I’m trying to make a sql UPDATE statement that updates an active column in each table. Table1 has a field record that looks like this: Table2 Table3 is the same as table 2 I need a query that looks for the name “Digital Only” and it’s ID on table1. Update it’s
Relational Database Question: Parent that references child
Is it valid to have a parent table have a reference to a child table? For example, a table Parents has a child table Children. Children has a foreign key Parent_ID linking back to its parent. But Parent has another column (say favorite_child_id) that is a foreign key back to the Child table. Is this acceptable or is it bad
How can I count rows into more than one ‘then’ of a Postgres searched case expression? (reporting)
Background I have a Postgres 11 database running on RDS. I need to produce a report which shows counts of events over a certain period, grouping, renaming, and counting them based on description …
Find rows using in between values in MySQL
I found it really hard to come up with a descriptive problem title, so I’ll give a bit of context. We have a list of boats and a list of docks. Boats can be transferred from one dock to the other on a specific datetime, or it could be at no dock at all. I’m trying to find out which
How to create an Oracle Procedure which includes a Subquery for an SQL Insert Into clause?
I have a problem to declare an Database Procedure, which includes an SQL Insert Into Statement with a subquery. I use an Oracle database. I get an errormessage, that tells me the From keyword was not found where expected (ORA-00923). Could somebody explain me, how I create this syntax error in the following example ? Is there a problem with
how to INSERT new record with SELECT value in sql
How can i insert a value into table A using return value from table B. I know i can use INSERT INTO SELECT statement but this requires that data types in source and target tables match and the columns are in order but i have the below table like thus: Table A: Table B: Now i want to count all
Pivot data row to header using SQL in Postgresql
I get this result using SQL request: NBR_TYPE Count ———————– 0021 20 0022 19 0023 13 But i want result like this : 0021 …