I have a table of patients that were admitted and discharged from the hospital. I am trying to find any patients that have an admission within 30 days of their previous discharge date. So for example, I have the table: Name Admission Date Discharge Date Patient A 1/1/2021 1/5/2021 Patient A 1/7/2021 1/10/2021…
How to update multiple rows at the same time with one query
In cleaning a set of data, I came across several entries where there are multiples of a street name such as: and In total I have 25 street names that are misrecorded as the street name followed by (). I want to update all of the street names so that they are rewritten to drop the () and all read
Getting null data for left join on same table twice
I have some tables like this: Employee: Teams: TeamMember: I am trying to get list of employees with info regarding who they are : Team Member or Team Leader or Individual. Current output: Expected output: Query: Only issue here is that I am getting TeamId and TeamName as blank for “Team Leader” d…
Ruby on Rails converting Timescale SQL rate code to Ruby
I’m trying to use the Rate function from timescale to generate graph data. Right now I have a database view that does this using the concepts from the SQL code below from TimescaleDocs: Is there a way to convert this directly to ruby code in a timeseries model to improve runtime? Answer It’s possi…
How do I get results of SQL data change statement with triggers updates applied?
I have a requirement where I need to get the immediate result of an update statement. I saw that I can do that by using the SQL data-change-statement modifiers. However, I’m not being able to get the final result after applying all associated triggers. For example, let’s say I have the following q…
Select specific columns from the stored procedure
I have a stored procedure in other database which is maintained by other team. Assume that it is currently returning 3 columns, and my system only needs those 3 columns but the other team can add few more columns for their own use which is causing my system to fail. Other database SP And below is my query, wh…
SQL lag function avoid getting first row as null
I have a sql query if my filter date is > 20210625 , I get value as 27 and previous value as null , is it possible to get the prev_value 25. If there is no filter I will get it. But filter is required to prevent reading the whole table. Also one more requirement. If previous value is null
get request to accepts params in postman, currently sends a unsupported media type error
I’ve built different get methods using body request to select an id (with raw json). However, as I learned that get methods doesn’t support body request, I would like to change so that I can use params in postman insted. However, when I do it now, I get an 415 Unsupported Media Type. Here’s …
Add OR in WHERE
I have SQL query: And I want to adjust it so it looks for p.post_status = ‘publish’ OR p.post_status = ‘t_active’. I tried: But it didn’t work. How do you achieve this? This is an entry level question – SQL is not my jazz. Answer You need to use brackets to make clear what …
Using HAVING COUNT(column name) NOT working as expected
I have the SQL statement, working in SQL Server 2016, below where I’m getting records from an INNER SELECT UNION ALL statement and in the OUTER SELECT I’m trying to extract those records that meet the HAVING COUNT code however, my code is bringing back no records. Here are the records from my INNE…