I need to filter down some results before joining them. I have three models: ModelA, ModelB, and ModelC. ModelA and ModelB both have a relationship to ModelC and they both have an organization_id, …
If statement with select in Postgresql
I’ve having trouble to do a if with select, e.g: In SQL Server I can do this: But in postgresql I don’t have any idea how handle this. I’ve tring some ways to do this, but unfortunately I have not success. Answer The syntax for an IF in Postgres (or rather PL/pgSQL) is IF … THEN ……
Use select filed result on LEFTJOIN string
I’m trying to use id_program result from the availability table inside a string in LEFTJOIN, is it possible? I tried using this {post.id_program} but its not working. EDIT: http://sqlfiddle.com/#!9/aeb4a7/3 Answer I see two ways.. Using CONCAT(): With REPLACE():
How to copy data from one table to another when foreign key is only defined in the destination table?
Table_A schema: Table_B schema: Column UserId is defined as Foreign Key in Table_A. I need to copy data from Table_B to Table_A and I used: But I get: The INSERT statement conflicted with the FOREIGN KEY constraint “FK_UserId” The conflict occurred in database “Table_A”, table “d…
SQL – find all combinations
From a dataset like this, I need to get all possible combinations of cases in the same room so that no case overlaps another. The expected result is I am able to get single line results of what case can be combined with which other case like: And I’ve tried some recursions but I am at a loss nowhere nea…
What is the correct way of inserting values in to tables and link tables in oracle?
I am currently trying to build a database but have become stuck after creating the tables. I have tried inserting values into a table but it gives me the following error: Error starting at line : 73 in command – Error at Command Line : 73 Column : 66 Error report – SQL Error: ORA-00984: column not…
How do i perform a conditional update in SQL with data from multiple tables?
Trying to run an update on a column and i’m getting a syntax error on the FROM line connecting the tables Answer The WHERE clause belongs at the end of the update join statement: See SQL update query using joins for a good canonical answer to your question.
Counting users based on an event count
I have an events table where I currently have a count of users grouped by the date. I am wanting to make two different counts based on the number of times a user had a specific event (we’ll call it event_a) The first count will count the user if event_a appeared exactly 1 time for the user, the 2nd coun…
I can not make a composite relationship between 2 tables in SQL Server
I have the next schema where I want to relation the tables COMPANIES_COUNTRIES and TIME_TRACKING_REQUEST_TYPE by CountryId and CompanyId. DB Schema The scripts for both tables are the next: I tried to make a composite relationship with next script but didn’t work: I get this error (sorry, it is in Spani…
PostgreSQL multiple join
I would like to make multiple joins in one query, I have four tables for that with the following structure: Table 1 fields, t1: did (int), finished (datetime), userid (int) Table 2 fields, t2: userid …