Skip to content

Tag: sql

Results of an UNION in two columns

I am querying names from two tables: And I get the result I want in a single column. Now I want to know if it is possbile to get the results in two different columns like: “names_from_data1”, “names_from_data2” Or another way to identify where each name comes from: data_1 or data_2 Ans…

How to format a number in thousands in SQL

I’m trying to format a number like 238350.50 into “238” with SQL. I round to the nearest thousend using ROUND(number_x, -3) and it works fine but I can’t get rid of the hundreds digits. A few more examples of how the formatting should work: 499.99 becomes 0 500 becomes 1 1250 becomes 1…

postgresql total column sum

SELECT I have a SELECT like this. I need to get something like the one shown above. That is, I need to get the total of one column. I’ve tried a lot of things already, but nothing works out for me. Answer If I understand correctly you want a result where extra row with aggregated value is appended after…

MySQL Multiple Conditions in Grouped Data

I am trying to see how many users have clicked on an ad campaign and signed up afterwards. My Logging table has the columns IP, UserID, and Data (this has the $_GET info like utm_source = newsletter). A user may click a link in newsletter and then later sign up on my website (which is indicated when they have…

Using wildcard in bigquery project name

I am trying to extract some information from all tables in the SALES dataset. I want to run it for multiple clients. Each client has a big query project. Bellow query returns what I want for our London client. How I can use a wildcard to do the same query for the other 10 clients and union all in one

How to compare multiple rows

I have a table with data like following, want to return those group_id with unique data. Both group_id 3 and 4 have two component 123 and 456, so they are “duplicated”, we just need to return the smaller group_id, that’s 3. Also group_id 5 doesn’t have a duplication, it can be returned…

LEFT JOIN by closer value condition

I have this query In the second LEFT JOIN, I would like to change the second condition pob.year = proc.anno_eleccion so that it does not only search for the exact year when joining. Instead, I would like to get the closer year stored in my pob table. For example, the first year stored in pob is 2003, so I wan…