I have two tables. Lets say tab1 and tab2. tab1 has data like this: ID Field Name 00001 col1 00001 col63 00002 col3 00002 col47 00003 col22 00003 col40 Each ID has always 2 Field Name values. And tab2 has columns col1,col2,col3,col4….col70 I want to select columns dynamically from tab2. Like for 00001 i…
Tag: sql
Select some datetime from base
I have a 2 table in my base. The first table is reservation table. The start_ts and end_ts are time when start and end the reservation of desk (desk_id): [Reservation_table] The second is motion, which come from sensors motion. [Motion_table] This 2 tables are connecting that the sensors are start save to the…
how to sum two columns from different tables MySQL
I need to sum the data from two different columns located in different tables and grouped by session_id….. I need to sum the column of spent_points + price_points grouped by session_id this is the result i hope to get I have tried with this query but I have only managed to group the data but I have not …
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…
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