Skip to content

Tag: sql

Using case statement or IF inside a WHERE clause

I have several stored procedures that are almost identical but have some different AND parts inside a WHERE clause. Based on a variable deptname, I want to add additional AND/OR conditions to my already existing WHERE clause. So kind of like IF/CASE WHEN on the part that is different. Think about it as string…

query mysql to find total value

please help me. imagine the table as bellow and I want to query and show result like this I dont know how to write query to show result like that. I used (date_join) as dates, count(id) and group by date(date_join) and the result not show like that. Answer In MySQL 8.0, you can solve this with aggregation and…

Problems with reserved field name ‘user’ in SAS Proc SQL

How do I select the user column from my table and not select the surprise ‘feature’ SAS added to their SQL syntax: Result: Bonus, the field name in the result table (if you create one) gets a temporary name… ie. something like _TEM001. Answer Use the name literal syntax, ‘<name>&…

How to validate dates are in the format of YYYYMMDD

I have a date column having thousands of rows in the format of YYYYMMDD. I have to validate that all the rows are in the correct date format. If there is any row off from the desired format or in any means (say null) the row should be returned in the result. I have the following sample data: Since NAN

Get the row where the sum of a value matches a condition

I have a table with the columns: date (timestamp) num (integer) Looks like this in CSV: I want different queries to figure out the following: A: The earliest date that the sum of num is >= 1 B: The earliest date that the sum of num is >= 2 In the sample data the output would be A: 2018-02-07 and