Skip to content

Tag: sql-server

Searching with a lateral join extremely slow

I’m currently implementing a search function in one of our grids and when doing so, my query goes from running in 1 second or less to running in about 16 seconds (I’ve added suggested indices, etc). The data returned from this query will be the parent’s id, parent’s name, most recent c…

Select the most popular month

This is my sample data. I want to select the most popular month for borrowing. This is what I’ve tried so far: Answer In SQL Server, you can use select top (1): If there are ties, this returns an arbitrary matching row. If you want all of them, use TOP (1) WITH TIES.

SQL server: Question about query group and auto id

i have a table which has 2 columns like this, picture is input and output: Explain input: 2 column is 2 person who relation ship together. Exam: A relation with B, C,D,H Output: i want to merge 2 column , with Column group ID auto and column RelationShip ID group auto: i tried query: row_number() OVER (ORDER …

Combine ORDER BY and GROUP BY and a counter

I use this SQL query to get customer order lines from an imported CSV file and get this result Now I would like to produce 2 different results, where one, in addition to order by ordernr also group them by their customerid and another where I get a counter (the cnt column) with how many times a customer/custo…

Divide Zero error when calculating Sales % in SQL

I keep getting the following error message: Msg 8134, Level 16, State 1, Line 1 Divide by zero error encountered. I’ve seen some posts about using IFNULL which I have tried. For example: But this returns the following error: ‘IFNULL’ is not a recognized built-in function name. What I’m…