Skip to main content
Search type Search syntax
Tags [tag]
Exact "words here"
Author user:1234
user:me (yours)
Score score:3 (3+)
score:0 (none)
Answers answers:3 (3+)
answers:0 (none)
isaccepted:yes
hasaccepted:no
inquestion:1234
Views views:250
Code code:"if (foo != bar)"
Sections title:apples
body:"apples oranges"
URL url:"*.example.com"
Saves in:saves
Status closed:yes
duplicate:no
migrated:no
wiki:no
Types is:question
is:answer
Exclude -[tag]
-apples
For more details on advanced search visit our help page
Results tagged with
Search options answers only not deleted user 1192

Writing queries to request or change information stored in a database or other information management system. Questions should include table and index definitions, sample data, expected output, and a tag for the specific system (e.g. MySQL, PostgreSQL, Oracle, SQL Server, MongoDB).

3 votes

Understanding the flaw in the query

It looks fine to me. It correctly avoids listing any employees from department five, even if there are multiple employees with the same highest salary in that department.
Paul White's user avatar
  • 90.2k
3 votes

Can Somebody help me understand this query

This makes the result of the subquery dependent on the 'current row' from the outer query. … Essentially, the query is asking for rows where the first_name attribute matches any row (from the same table) where the last_name attribute is different. …
Paul White's user avatar
  • 90.2k
0 votes
Accepted

SQL SERVER SELECT COLUMN JSON ARRAY

Given: DECLARE @T table ( j nvarchar(max) NOT NULL ); INSERT @T (j) VALUES ( N' { "_id":{ "$oid":"61ccdc232d1e0a844149faba" }, "report":{ …
Paul White's user avatar
  • 90.2k
7 votes
Accepted

Compare two tables amount with respect to priortization of FundType

I cannot think of a reasonable way to meet the requirement with a T-SQL non-cursor solution, so the following is a SQLCLR procedure that returns a result set. The solution performs a single scan of e …
Paul White's user avatar
  • 90.2k
1 vote

Select distinct values in column with condition

Solution 1 The idea is to find distinct groups for each product, then the highest priority match calculated using a CASE expression: SELECT P.ProductId, P.ItemId, G.[Group], P.Color, …
Paul White's user avatar
  • 90.2k
2 votes
Accepted

Varbinary update attempt

As I read the question, you want to replace the leftmost occurrence of three zero bytes with 0xFFFFFF: DECLARE @ToFind binary(3) = 0x000000, @ReplaceWith binary(3) = 0xFFFFFF; UPDATE YT SET …
Paul White's user avatar
  • 90.2k
6 votes
Accepted

Select only rows where all values in a column match a value for an id

Yet another alternative: # All non-NULL seq_status values per seq_id contain 'A' SELECT s.seq_id FROM sequence AS s GROUP BY s.seq_id HAVING SUM(s.seq_status = 'A') = COUNT(*); That works because com …
61 votes
Accepted

Unexpected scans during delete operation using WHERE IN

"I'm more wondering why the query optimizer would ever use the plan it currently does." … The aggregate is not due to the redundant DISTINCT in the query text: it is an optimization that can be introduced by two exploration rules, LASJNtoLASJNonDist and LASJOnLclDist. …
Paul White's user avatar
  • 90.2k
4 votes

How to find Linear interpolation without using pivot and unpivot in SQL Server 2008

Sample data CREATE TABLE #Data ( id integer NOT NULL PRIMARY KEY, x integer NOT NULL, val float NOT NULL ); INSERT #Data (id, x, val) VALUES (1, 1, 8.18), (2, 3, 8.35), ( …
Paul White's user avatar
  • 90.2k
3 votes

Exclude Duplicates With Certain Value

Another approach would be to: Partition the rows by IDNum Number the rows within each partition (rows with the same IDNum) so that rows with '0' FirstName and LastName come last Delete rows numbered …
Paul White's user avatar
  • 90.2k
5 votes
Accepted

Select all overlapping ranges from starting range

A straightforward recursive solution splits the task in two parts: Find the earliest start date by following the chain of overlapping ranges Find the latest end date by following the chain of overla …
Paul White's user avatar
  • 90.2k
7 votes
Accepted

Find maximum number of concurrent logged in distinct users

There are standard solutions available to solve this problem by Itzik Ben-Gan and others. There is a question over the precise meaning of "maximum number of concurrent logged in distinct users". If th …
Paul White's user avatar
  • 90.2k
2 votes

Select repeating number x times and then increment

One solution using simple integer arithmetic on a table of numbers. This example uses an efficient way to generate a sequence on the fly, in practice you would probably have a permanent Numbers table. …
Paul White's user avatar
  • 90.2k
4 votes
Accepted

Separate a column into its components based on another table

CREATE UNIQUE CLUSTERED INDEX cuq ON dbo.ACCOUNT1 (ATYPCODE); CREATE UNIQUE CLUSTERED INDEX cuq ON dbo.ACCOUNT2 (AGLTLVL, AGLTCODE); Then organize the data into a useful hierarchy using a recursive query … 1 ║ 4000 ║ ║ 2 ║ 2 ║ 18 ║ 2 ║ 4000 18 ║ ║ 2 ║ 3 ║ 2020 ║ 3 ║ 4000 182020 ║ ╚═════╩════╩══════════╩═════════╩══════════════════╝ Then the final query
Paul White's user avatar
  • 90.2k
7 votes

SQL Server - Multiple running totals

Asynchronous implies that the running totals do not need to be completely accurate at all times, or your data change patterns are such that a one-off running total build will be valid and accurate unt …
Paul White's user avatar
  • 90.2k

15 30 50 per page