Skip to content

Union ALL with different number of columns

I have an Employee table with these columns : EmployeeId Fullname Phone Department Team Function Manager and EmployeeHistory containing the history with different columns, but there are some in common : EmployeeId Fullname Email Geolocation Department Team Function Manager How can I union them? Answer Replace…

SQLite3 Table Only Holds 1 Row

So, I have 2 tables, that I’ve added per the documentation, but something weird happens whenever I try to add a row to them. The data is written into the .db file, but whenever I query it, it only returns the first row. I can still update, and read these rows normally, but any data I write to them, just

Using PIVOT in conjunction with a total summation in SQL

I have some data with a general structure: I want to output a single row like: I can see that the way to approach this is with a PIVOT, but how would I do this whilst also calculating the total? Thank Answer You can use conditional aggregation: Note: If you have values other than ‘X’, ‘Y&#82…

Creating Dynamic SQL Query to join multiple tables?

I have a dataset in SQl which includes huge numbers of tables (more than 2000). Within these tables, I have a main table called ‘main_table’, that includes some parameters and one of them is the key named ‘id’. I have also a table called ‘Tables_name’, which consists of the…

Optimize Merge query inside n^2 loop

I’ve got a merge query that needs to be executed one time for each combination of day and sessionType inside the request ArrayList. I am using nativeQuery to execute it. Is there a way to plain the source data (two ArrayLists, one with a date range instead a single day, and the other with all the sessio…

Dynamic SQL stored procedure and datetime

I have a simple query that I want to convert to dynamic SQL. I have 2 input parameters: a table and a datetime. And the output is the rowcount for the table and this specific datetime. I tried different solutions. I tried the query with execute sp_executesql, I tied to add the the ”’ before and af…