Relative Content

Tag Archive for sql-servert-sql

Optimizing SQL Performance: Merging Budget and Actuals Tables After same CTE transformations and dimension joins

I have created a SQL query where I merge columns from a budget table and columns from a actuals table together using the same dimensions to join on customer, date and product. They need to undergo the same steps. If a product has been sold at least one time in that year by a customer than the yearmonth from 1 to 12 should be shown regardless if it has been sold or not in that particular yearmonth. This is the same for the budget.

How to get difference from the first row but aggreagtiong from second row till the end

So, I have two columns where on first row (inspection) there are some violations that are spotted and noted … can be 15+ different violations. Then on every subsequent inspection there can be some violations that are completed (those that are resolved can NOT show again as unresolved). Number can vary and can even be that inspection didn’t see any of remaining violations resolved so for that one it would be an empty row but still has to be counted. What I need as a result are unresolved violations after each inspection …

LAG previous year wrong when grouped

I have created a SQL query which will show the current sales and sales from previous year with the LAG window function, this per customer, product, year and month. A product is not always sold each year and I know that LAG does not care about missing years, therefore the CASE statement, based on this stackoverflow post: LAG function alternative. I need the results for the missing year in between

SQL server – LAG previous year wrong when grouped

I have created a SQL query which will show the current sales and sales from previous year with the LAG window function, this per customer, product, year and month. A product is not always sold each year and I know that LAG does not care about missing years, therefore the CASE statement, based on this stackoverflow post: LAG function alternative. I need the results for the missing year in between