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 …
Tag: sql
How to optimize a MySQL query which is taking 10 sec to fetch results
I have a MySQL database. I have a table in it which has around 200000 rows. I am querying through this table to fetch the latest data.Query select * from `db`.`Data` where floor = “floor_value”…
Sql Query / Calculate rank for values based on date and id
For each date and id we have to give the rank to values in decreasing order. Below is the Input Table. Output should look like below: Answer You can use RANK(), partitioning on date and id and ordering by value descending: Output: Demo on SQLFiddle This query will run on all the DBMS you have tagged your ques…
how to select first and last row in 1 query after Filtering and then carry out calculation between the values of two values in one query
I’m using T-SQL 2014 Suppose I have a stock price chart as follow I want to write efficient code for a stored function to display the Open price at the start, Close price at the end, and the difference between Close and Open. Is it possible to do that in one query? The query seems easy but it turned out
MySQL sum() from more than 1 table
Ok, so every sum() returns the desired value, I already checked it but I’m having problems when trying to sum everything together and then get the total value. Basically I have to do this: initial Fee(which is a set value defined on the”customer” table) + sum of activity1 cost + sum of activ…
Delete from child table with two parents without cascade delete in sql server
I need your guidance. I create a script to delete from 3 tables separately. meaning when I created the script is to delete from table3 (child) then delete from 2 (child), then delete from table1(parent) I am having problem with Table3 since table2 has foreign key both for table2 and table1 so I cannot delete …
Counting rows in different tables and grouping by a certain criteria
I have 3 tables: Projects, Components, Suppliers. Each one has an id column, city column and some other columns. What I am trying to do is count how many projects, components and suppliers there are for each city. What I have tried is: After running this query, I am getting incorrect values, almost all of the…
SQL query fetching data from multiple tables and displaying in html table
I am working on a system that fetches data from three tables. My query takes input from a form stored in variables named $departure and $destination. However the query is not working when i use these variables but if i enter result in sql query then it fetches data from sql. I echoed $departure and $destinati…
Need to parse a date range location in a field to two SQLStatements
Have a custom field that contains a date range in the following format: What I need to do is separate this one line into two different fields, the first selection/range and the second range, so if you take the screenshot I need it to be separated to 3/16/20 for one field and 3/22/20 for the other field. Curre…
IsNullOrEmpty with an empty value still go into the loop
I’m currently trying to compare in C# a value I get from my database, and i use “IsNullOrEmpty” to check if the value is, well, null or empty. When the value in the database is “null”, no problem. …