Questions tagged [query-refactor]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
1
vote
1answer
34 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 ...
1
vote
1answer
77 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 ...
4
votes
1answer
262 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 ...
13
votes
3answers
2k 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')...
1
vote
1answer
505 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 ...
1
vote
1answer
2k 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 ...
2
votes
1answer
78 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....
0
votes
2answers
241 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 ...
7
votes
2answers
3k 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 | ...
7
votes
2answers
20k 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/...
8
votes
2answers
463 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 ...
10
votes
3answers
649 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 ...
3
votes
3answers
23k 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 ...
1
vote
1answer
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 ...
4
votes
4answers
4k 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 , ...
1
vote
1answer
837 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 ...
6
votes
3answers
541 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 ...