Tables: What I am expecting to query is this: However this is what I get: With this query: I wonder, in what way the query above could be modified to return the expected results? What would be a good approach to achieve this? Answer You are joining along two dimensions, so you are getting a Cartesian products…
Tag: sql
SQL for identifying % of orders placed within 20 minutes of each other
Have a dataset like below and would like to know various ways to solve the question of : what % of orders were within 20 minutes of each other? CustomerId Order_# Order_Date 123 000112 12/25/2011 10:30 123 000113 12/25/2011 10:35 123 000114 12/25/2011 10:45 123 000115 12/25/2011 10:55 456 000113 12/25/2011 10…
SQL query Postgres 12
I’m doing an inner join on a table like this: The condition f.vac_1 = mv.id might not been satisfied in the case where a person have not been vaccinated. In such case, I don’t want to ignore the row, but instead of displaying the vaccine name (which is the purpose of the inner join) to display an …
What is wrong with my stored procedure SQL query on PostgreSQL?
I tried many times with or without the semicolon but still shows me the error message. ERROR: syntax error at or near “INSERT” LINE 13: INSERT INTO Advertisement(ad_id, business_id, requested_on… ^ SQL state: 42601 Character: 712 I am sure the table constraint and sequence are all correct bu…
SQL select query retrieval blank return
I have to retrieve distinct entities from a column, all of which start with a vowel. The query looks like this : It’s not giving compilation errors, but it’s not returning anything. Any ideas? Answer You can use regular expressions: LIKE wildcard patterns do not support character classes, except i…
How to use two where conditions in SQL?
Following is the query I have written and I need to where conditions. Admin_Level_3_palika is not null Year = ‘2021’ However, the following query is still giving me null values for Admin_Level_3_palika Please help me with how to work with this. Following is an example of my dataset, Epid_ID being …
SQL subqueries PostgreSQL 12
I’m having a table similar to this: first last date pos john doe 18-03-2021 harris potter 10-06-2021 john doe 10-05-2021 harris potter 14-06-2021 jessica potter 14-06-2021 kermit foster The use case is as follow: The pos column correspond to a positive covid test The date column correspond to the vaccin…
PostgreSQL – Find number of installs each day given a table of installs and uninstalls
I have a table for machine installations in this way: I need a query that returns the number of installed machines per day. Like this: I know that given a date, say ‘2020-01-03’, I can get the number of installed machines as follows: However, I do not know how to query in such a way I can get the …
Passing parameters for a constraint function
I have the following table that joins driver and truck tables in order to assign trucks to drivers. I need to constraint entries that belong to the same driver on the same day, and DO NOT include the same truck code. It was suggested to create a boolean function to pass along dateval, truckval and driverval, …
How can you group data by multiple fields with separate totals in SQL or Power query
I have a set of data that I want to count by multiple characteristics. I am starting with something likes this (True/False for catagories) week PP SEN 1 T F 1 T T 1 F F 2 T F 2 T T 2 F F 2 F F 3 T F 3 F F I want to end up with