Skip to content

Tag: sql-server

SQL Generate per day records between dates

I have a table that where each record has start and end dates. I need to return a record for each day between start and end date fields (including the start and end dates). Using MS SQL Server. Example: Current data Data required: Looking for recommendations. Thanks. Answer You can use recursive cte :

SQL Count depending on certain conditions

I have two tables. One have userid and email (users table). The other have payments information (payments table) from the userid in users. I want to count the PaidMonths taking in consideration the following rules: If ValuePaid < 10 PaidMonths should be = 0.23 (even if in the column the value seen is any o…

Having and Where in combination with Group By

I have learned to use HAVING when I use GROUP BY instead of the WHERE clause and never encountered any problems with it. Today I saw this on a w3schools.com a SQL Learning Page: Why this should work? When I should use this? Answer The difference between WHERE and HAVING is central to when you should employ on…

SQL – select based on value existence in another column

I want to create a SQL query to identify wells which do not have A3 events. I am using SQL server. I have tried multiple ways like checking count, comparing with event A3 but still unable to get what I want. From below example desired result would be W3 and W4 Site Event W1 A1 W1 A2 W1 A3 W2

SQL Merge Into with multiple statements

I’m trying to keep a historic table of another one. When updating the original I would like to insert rows into the historic one. I’m using Sql Merge: Can I make multiple statements in the same “when” condition?, as such: Answer Normally you can INSERT only into one table. The syntax d…