2022 Developer Survey is open! Take survey.

Hot answers tagged

3 votes
Accepted

How to Join two tables with SUM and GROUP BY in SQL server

This can be accomplished simply with a JOIN and GROUP_BY clause like so: SELECT p.designation, SUM(ISNULL(sp.quantity, 0)) AS [total quantity] FROM product AS p LEFT JOIN sub_product AS sp ...
user avatar
  • 24.3k
2 votes

Microsoft SQL server management studio : insert label in SQL query results

I am unaware of a way to add custom labels to result pane. You can add a column to the query to help identify SELECT 'label1',* FROM Customer.[dbo].TableName WHERE ...
user avatar
  • 2,765
2 votes
Accepted

Postgres "unnesting" multiple json arrays simultaneouesly with different depths

Unnesting multiple set-returning functions in parallel behaved in mysterious ways (borderline broken) in Postgres 9.6. That got fixed with Postgres 10. See: What is the expected behaviour for ...
user avatar
2 votes
Accepted

SQL Server - Finding Parent Source from T-SQL Snippet

parental advisory You can use the dm_exec_procedure_stats view, and if you're on SQL Server 2016 or better, the dm_exec_function_stats view as well, to track down scalar UDFs. Both of those views have ...
user avatar
  • 33.4k
1 vote
Accepted

How to get the highest (max) cost, lowest (min) cost, and the date that each occurred?

To obtain results per part, you obviously need to group by part somewhere, and I don't see that in your attempt. Something like this might work for you: select g.part, min_cost, (select max(p1....
user avatar
  • 21.2k
1 vote
Accepted

SQL query to return rows whose column has a common value

Try using an EXCEPT… SELECT InstNum FROM Table WHERE Profile = 'C' EXCEPT SELECT InstNum FROM Table WHERE Profile <> ‘C' GROUP BY InstNum;
user avatar
1 vote

Row_number to not increment if values on three columns are the same from the row before

Based on your description, what you want should be dense_rank() over (partition by orderid order by DocumentNo_, CurrentPackageTrackingNo)
user avatar
  • 473
1 vote
Accepted

MySQL - Getting the percentage of an occurrence

To get the percentage you need to divide the sum of the value, with the total and multiply by 100 to get a percentage. resolution='Cancelled' is an expression returning 0 for false, or 1 for true. So: ...
user avatar
  • 6,380
1 vote

SQL Server - Finding Parent Source from T-SQL Snippet

dm_exec_sql_text supplies a objectid column which is NULL for ad-hoc batches. You can just left-join it with sys.objects WITH cte AS ( SELECT COUNT(*) [count], s.query_hash, min(...
user avatar
  • 5,214
1 vote
Accepted

Forcing a sequential scan over the index scan

set enable_indexscan TO off; set enable_bitmapscan TO off; solves
user avatar
  • 95

Only top scored, non community-wiki answers of a minimum length are eligible