Questions tagged [query-refactor]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
0 votes
0 answers
74 views

Sanity Check - Result set different after breaking up case expression in where clause into different selects w/union all

I'm back again with more dumb questions. This time I am dealing with refactoring a single select with nested case expressions in the where clause. I'm trying to optimize this, and in attempting to do ...
scarr030's user avatar
  • 115
2 votes
1 answer
333 views

About Apex SQL Refactor and SSMS keyboard shortcuts being broken

Hi DBA Stack Exchange! Today I tried Apex SQL Refactor and right from the get go, once installed and integrated into my T-SQL dev tool of Choice (SSMS 18.3.1) I noticed that some Keyboard shortcuts ...
Martin Surasky's user avatar
1 vote
1 answer
59 views

Refactoring database after migrating table

I'm creating a new staging database, into which I intend to migrate several tables from the old database. These tables are referenced in functions and stored procedures. If I were doing things ...
N4v's user avatar
  • 135
1 vote
1 answer
219 views

Refactoring a very large database table and having join over millions of records

I have a fairly big postgresql jobs table of (more than 60 attributes) and one critical query contains joins with this table of million records. and I cannot reduce its size(paritioning by range which ...
Radio Active's user avatar
4 votes
1 answer
889 views

PostgreSQL Query Refactoring

I have a simple table called events It has a date, start time, end time and status I have multiple events for the same date, start and end. To know if someone can make an appointment, I have to ...
Alex Takitani's user avatar
13 votes
3 answers
4k views

Avoiding multiple `or` expressions

I have the following oracle SQL and its works and all but it's quite ugly with all of the ors. Is there a more concise way of doing this? SELECT * FROM foobar WHERE (SUBJECT ='STAT' and TERM ='111')...
Kyle Decot's user avatar
1 vote
1 answer
663 views

Refactoring sql for tables with similar columns but different table names

I have the task of re-factoring a SQL script that performs a lot of unions from different tables. These tables have similar information, so the script queries the same columns as in each table, except ...
info_seekeR's user avatar
1 vote
1 answer
3k views

Find Duplicate Customers

Okay... I have a table that has customers: -- Individual Table * ID (Internal Unique ID) * IndividualID (External Unique Individual Identifier) * Last Name * First Name * Birth Date * SSN * ... The ...
WernerCD's user avatar
  • 1,215
2 votes
1 answer
81 views

Query difference with where condition

I need to find the ids of all rows in a table A without a matching row on table B. Following the answer on this question I'm using a left join like this: select A.id from A left join B on B.id_A = A....
Pedro Werneck's user avatar
0 votes
2 answers
368 views

How can I optimize this query and support multiple SKUs?

My current query only can select one SKU at a time. I can leave salesite_id constant. If there is a way to also have varying salesite_ids that would be good too, but not necessary. Also any ...
Michael's user avatar
7 votes
2 answers
5k views

Fetch the newest row grouped by a column in MySQL

My problem seems like it should have a much simpler solution than what I have come up with. Starting with this data set: log_table +--------+-----------+------------------+---------+ | log_id | ...
Asgrim's user avatar
  • 73
7 votes
3 answers
22k views

Is there a more concise way to convert a UTC datetime into a local date only?

I am trying to write a query that groups records based on the local date part only of a UTC datetime field. For example, if my table contains 10/19/2012 2:00:00, then it should get grouped as 10/18/...
Rachel's user avatar
  • 8,457
8 votes
2 answers
491 views

Is there way to join every row of TableA to a row of the smaller TableB by repeating TableB however many times are needed?

Sorry for the confusing title, I wasn't sure what to write there. I have a table of a few hundred records. I need to assign each record of this table to a much smaller dynamic table of users, and the ...
Rachel's user avatar
  • 8,457
11 votes
3 answers
763 views

Are these two queries logically equivalent?

Are these two queries logically equivalent? DECLARE @DateTime DATETIME = GETDATE() Query 1 SELECT * FROM MyTable WHERE Datediff(DAY, LogInsertTime, @DateTime) > 7 Query 2 SELECT * FROM ...
Alf47's user avatar
  • 961
6 votes
3 answers
44k views

Alternative query to this (avoid DISTINCT)

Note: I work with MSSQL 2008, but I guess it's valid for many others DB engines I have this table "Users": UserID User CountryID 1 user 1 1 2 user 2 2 3 user 3 3 4 user 4 ...
Allende's user avatar
  • 247
1 vote
1 answer
1k views

This refactoring from cursor on a dblink'ed Oracle table OK?

The data is being compared from the local version of the table to one in the contracts linked server. I'm not certain what the goal of this code is in the first place, but I'm trying to get a handle ...
Cade Roux's user avatar
  • 6,602
4 votes
4 answers
5k views

Optimize Query with Derived Table

I have the following query select ps.id, ps.title , IFNULL(s.likes,0) as num_likes , IFNULL(s.comments,0) as num_comments , IFNULL(s.ratings,0) as num_ratings , IFNULL(s.views,0) as num_views , ...
Anuj Gakhar's user avatar
1 vote
1 answer
2k views

Is my case statement broken by the use of outer apply?

I'm struggling with a case statement not working. Simple enough I need to supply a value of 0 where the question marks are. I have several other case statements which handle separate columns, but this ...
BZN_DBer's user avatar
  • 180
6 votes
3 answers
1k views

How do you approach refactoring an ugly procedure/query?

I've inherited a database which contains several procedures which are 1000-1500 lines long, with complex nested sub-selects going up to 7 or 8 levels deep in places. I desperately need to refactor ...
goric's user avatar
  • 1,626