In this above query where condition should be executed only when operationType column is present in the table. If operationType column is not present where condition is no needed. Answer Try This:
Tag: sql
References invalid table in create table script with foreign keys
I’m trying to create a table in SQL Server with two foreign keys. But the problem is it’s not recognizing one of the reference tables. Here is table creation script. SQL Server is throwing the error on [dbo].[Group]. Here is the error message. I have created both of the tables [dbo].[Group] and [d…
IF when zero result from another Stored Procedure
I have a Stored Procedure which contains an Insert statement on it. From the above Stored Procedure, I want to have a condition from result of another Stored Procedure. With the following action: So, if cBranch is zero. Then execute Sp1 else do nothing. Need advice please. Really appreciated. Answer Try This:
How to fetch the the second row
I have a query below which is used to make a view. This query pulls the latest record of the report_id. One report_id can have multiple unique report_info_id This results in data something like Lets take the example of last record where report_id= 130 Now it is required that in first query I want such data if…
MySQL – select distinct value from two column
I have a table with the following structure: How could I make a select statement on this table, which will return some rows of this table, where in each row, a specific id appears only one, indifferent on which column it is specified? For the above result set, I would like a query that would return: To give a…
Update columns based on calculation
My table looks like this: I have a list of id’s connected to datestamps. I can manage to calculate the difference between their latest and first entry as follows: However, I am unsure how I can update my table to reflect these calculations. What I want is the following: Answer In MySQL, you can self-joi…
Change column value if date is older than 1 hour in mysql/mariadb?
Say I have below SQL table: Basically I want MySQL to check the Date column every 3 minutes and if Date value is older than 1 hour, I want to change Status to Offline. Can we achieve such thing in SQL? Alternatively if there is easier solution in Django python, I am open to use that as well. Answer I
Create a table in a trigger and compare it
i have 4 tables and i want to create a trigger that it declares a table and store it all in that table so it can be compared latter. In here are the tables that im using. But now i have a trigger that after insert on orders it inserts on transactions. My objective now its to create a trigger
How can I get the names of authors of books with the shortest titles from this database?
Let’s suppose I have a database with the following tables And I want to get the names of authors of books with the shortest titles. I think I could take isbn numbers of books with the shortest titles, get the author_id and from this get the name. I would be glad for any help with this problem. Answer Wh…
How can I achieve a Cartesian Product for all distinct entries of two fields within the provided sample data without using a Cross Join (MySQL 5.6)? [closed]
Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 2 years ago. Improve this question Here’s a sample data set: Specifically, I want to get the Cartesian …