I have two related entities: Station Connector When I insert data, I fill up both entites and it seems kinda okay, but when I’m trying to receive stations with particular types of connectors it duplicates rows. For example, I have an object And if I want to filter stations only by one connector type I r…
Joining two tables but pulling in values only if multiple columns match
I have the query below that assigns a station volume based on the volume processed that week. Station sizes are based by looking at 4 data points throughout the year. This is because station fluctuate in volume throughout the year for example, they may have small volume in one quarter, but large in another. T…
How to print NULL when table has no rows in SQL Server?
I have a table with a named column but no rows. I wanted to write a select similar to: I wanted to get a result like: But instead I get just the Col1 name and no results. How can I get a table with NULL as the result? Answer You can use aggregation: An aggregation query with no group by
Combining data from two columns in the same table into one and getting a count using SQL query
Type_1 Type_2 a c b b c a I have two columns in the same table similar to above where each row has two types, I am trying to get a count for each type (i.e how many a, b ,c etc) from both columns and a resulting table that has column headers of Type (a,b,c etc) and the COUNT
Divide the column by the sum grouped by another column
I have a table like this and I would like to divide the frequency by the sum of frequency grouped by the source. Therefore, I am looking for a table like Is there any way to do this in a single sql query? Answer You can use SUM() window function. If you want a numerical result: Depending on your database,
ora-00900 invalid sql statement execute immediate
I am trying to solve a task with a dynamic SQL, but facing an issue ora-00900 invalid sql statement. However, it works in the anonymous block treating the statement to be executed as a string. So where is the issue in the first case? It looks like with escape quotes, but can’t catch this. Answer As the error …
Expand Col values based on names/email SQL
I want to explode my table based on their ‘KEY’s and assort their names and emails separately into individual cols. In Python such scenarios are dealt easily(like we use explode in pandas). since I’ve just started with SQL I’m finding it difficult to get my feet wet. My googling didn&#…
Do PostgreSQL server side cursors store the results to disk?
Do PostgreSQL (server-side) cursors store/materialise the entire results set to disk? Context: running some queries with a large number of results and getting disk space errors from the server. Answer Not specifically. But various operations (large sorts, large multi-batch hash joins, set-returning functions,…
SQL Like Operator not working as expected in PHP
I am trying to fetch all the data from the database with a certain content tags value. If I run the code below, it only returns the row with id 1, even though the word tag is part of the tags value in both rows. I also added %or% operator but it is fetching the exact row. When I search
Get the count of records from another table
I have a view that looks like this: which is defined using this SQL statement: Now I want to count the records for another table (TABLE 2) based on the C_CODE, P_CODE and PH_CODE and display it as a column TOTAL in my view. How can I do this? Answer one way is to use subquery: