Skip to content

Filter on Rows that are Within a Certain Timeframe

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…

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…

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

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 …