Skip to content

Tag: sql

Oracle Hierarchical Query at depth level

I have a requirement to build a table from a hierarchical table. Table structure as below: emp_hier table: emp_id supervisorId 100 null 1 100 2 1 3 2 New table: I have to write a select query on the emp_heir table and the selected data should look like this: sel_emp_id rel_emp_id relation depth_lvl 100 100 se…

Select Distinct Rows with complex criteria

I have a table generated from a CRM software and it has a lot of repited individuals in different rows but with different fields completed in each repetition, something like this: id birth_date sex postal_code customer smoker 001 NULL NULL 00067 Yes 1 001 NULL Male 00067 NULL 1 001 21/03/1994 NULL 00067 NULL …

Group By function

hope you all having a great day. I need some help with this query. Just to explain the situation, I have to send e-mails containing the data found in this select, but I have to group by customers (TBLOTE.CEMPRES) so it would build a single e-mail for each customer found. Example, if the query finds 3+ row for…

C# Is it safe to concatenate constant strings to form a SQL Query?

I need to change the table name dynamically based on specific conditions. Is it safe to build my sql query the following way or am I prone to SQL Injection? To generalize, I want to build a parameterized sql query string by concatenating constant strings. Answer While this should not present any security prob…

Select range in SQL

I have a sql database that have InvoiceNumber column Now i need to select range between two numbers, but i don’t know what happened. I’ve tried “Between” and “<= , >=” Statements but the results are same. Table My query For example: If i need range between “1300…

Calculate the difference of dates in one column

DB-Fiddle Expected result: I want to calculate the date_difference between the MAX and the MIN order_date per campaign. So far I was able to come up with this query: This solution would work if the dates would be in two separate columns. How do I have to modify the query to also make it work if they are in on…

SQL Joins not working correctly when no data present

I’ve got the following code, where I build a tag from an entity Each tag has an id, name, description and how many questions it has, how many it has in a day, and how many it has in a week. The problem is, I can’t seem to display tags that have no questions tied to them, and also if