Structured Query Language is a language for interacting with relational databases. Read the tag wiki's guidelines for requesting SQL reviews: 1) Provide context, 2) Include the schema, 3) If asking about performance, include indexes and the output of EXPLAIN SELECT.

learn more… | top users | synonyms

0
votes
0answers
3 views

Stored procedure to query custom data tables as dynamic SQL

There's a lot going on here, but the background for why this is necessary is that there is a set schema, or 'core' set of tables that are prefixed with 'bu', and any core table can have a custom table ...
2
votes
1answer
13 views

Collating client ratings

The code is fetching records from a table called CSAT_SUMMARY_REPORTS[driver table] but due to the filters conditions applied which needs to be checked in another table called CSAT_REPORT_FIELDS. In ...
0
votes
1answer
18 views

Finding colors that have not been assigned to a given user

Goal: make this code as efficient as possible. Given a finite dataset (Colors - this isn't Crayola here :D) how do I know what colors are not assigned to a given user? Additional requirement - the ...
1
vote
1answer
33 views

Get unique results that have duplicate entries? (intersection?)

I have a table of patients that has a field for which hospitals they visited. I want to retrieve a list of patients that have visited Green Hospital and have also visited other hospitals. Is this ...
2
votes
0answers
37 views

Which version of custom mysql sql duplicate_key update query is cleaner?

The goal is to create an activity or update it's counter if it is already present. And to be super accurate the code avoids race conditions with a custom ...
1
vote
1answer
22 views

Parsing data from big json field and geocode services then store to PostgreSQL using psycopg2 instead django-orm

This code works, but my boss told me that it's very bad code, and don't want to deploy it. It is my first time working as a programmer and I have at least 3 months of experience. ...
1
vote
0answers
36 views

Find all the countries I can visit by just crossing direct borders

I was helping on this question. There you have a table border to indicate countryA and countryB have a common border. And want ...
-2
votes
1answer
44 views

Sample SQL Server code using pivot table

I'm using a dynamic pivot example. This SQL code has the purpose of perform transposing data from rows to columns, using a temporary table in SQL Server. The data is initially presented this way: ...
-1
votes
0answers
19 views

Multiple insert tables in pdo [closed]

I'm working on an project where i need to insert different values in more then one table. Inner join' is only available on a select prepare but i need it on a insert prepare pdo. ...
3
votes
1answer
42 views

MSSQL query to look for duplicate record

This query took 6 seconds to complete. How can I optimize it? Total records in table is 166803. ...
3
votes
2answers
77 views

Insert or update user data

I inherited an application that uses some stored procedures. Here is a sample of one of the stored procedures used to insert or modify user data. would like to get the group's opinion on the code. ...
3
votes
1answer
45 views

Stored procedure to classify policies by sales date

Currently this is the store procedure I am working on, it is working according to some testing that was done. I feel that it could be improved though, could use some optimization, feedback or ...
2
votes
1answer
21 views

Looking for meta data which matches certain criteria and was created after a certain date

I've got the below SQL statement which runs in MySQL on a WordPress installation. I'm looking for meta data that matches certain criteria and was created after a certain date. However, WordPress ...
3
votes
2answers
48 views

What are the top voted comments on closed questions, and who made the comments?

Following this linked question and the feedback it generated, I put together a new query which utilizes a JOIN. ...
4
votes
1answer
54 views

What percentage of down votes are users responsible for?

I put together this simple query to calculate the percentage of downvotes each user is responsible for and displaying them in order of downvote percentage. It's generating the desired results, but I ...
7
votes
1answer
30 views

SEDE Query to Find… Weird Questio​n/Protect Combinations

I was interested in finding some of the weirdest question/protection combinations, basically out of curiosity. I heuristically defined this as: Low view counts Not a moderator/SE employee From ...
7
votes
1answer
57 views

Deduplicating value pairs between 2 columns

This was asked in the SQL Helpline room: ...
0
votes
3answers
40 views

Counting pending bookings using a subselect

Can this query be improved? Is there a way to eliminate the duplicate function call? ...
4
votes
2answers
50 views

Top Python badged users from NYC

I forked this StackExchange Data Explorer (SEDE) query from another one, and made it a bit more complex. I'm looking for feedback on my style, as well as answers to other questions (see below). ...
2
votes
1answer
52 views

Parsing of XML in Python using minidom, and inserting data into a database

I have some XML that I am parsing in Python and I need to insert the data on the database. This is current implementation of my code that is working. Actual XML: ...
0
votes
1answer
35 views

Display the count of field in a view

I have a table that look like this ...
-2
votes
1answer
55 views

Adding tables and values to a database

I am having problems with my code because I am using a system called styleci. Can you please check it? How can I make it better and more secure? ...
3
votes
1answer
31 views

Query with many UNION ALL

I'm replicating a scenario on a sample set for better understanding. Database Table: Products Report required from that table, the query is working fine, and I'm able to get the required ...
2
votes
1answer
23 views

Reportable Sp_Who2

One of my issues with sp_who2 is that you need to be in the console to run it as it recalls spid twice and most reporting tools throw this as an error. My question is, would using the code below to ...
1
vote
1answer
67 views

Shredding XML into an SQL table

I have a few "given" things: An SQL Server 2012 table with certain structure Data provided as XML with certain structure My code has to shred that XML into a ...
7
votes
2answers
144 views

Replacement for sp_MSforeachdb

Having been informed that sp_MSforeachdb is undocumented and unsupported, I have been working on creating a replacement stored procedure based off the work done ...
5
votes
1answer
62 views

Inserting products scraped from the web into a database

I am trying to speed up the runtime of my program as the API can make calls. However after adding the inserts to a database 3 mins turns in 7 mins after 180 API calls, which can be one a second. How ...
5
votes
1answer
51 views

Tools for transferring data from an API to a database

I explain everything on my current code but this question is more general "HOW TO" work correctly with this in this case but even next time. I have 4 methods in my class, each does something but in ...
1
vote
0answers
27 views

Calculation of duration between two timestamps in different rows

In my sample data table below, all datatypes are varchar(255). ...
1
vote
2answers
68 views

Random uniqueID for each ID SQL

How can I improve this code? I wanted to know if there's a possibility to remove the for loop, and UPDATE a different ...
2
votes
1answer
58 views

Build SQL query criteria from form input

Very new to php. Using XAMPP, netbeans. I am using a <form> to send various input parameters as a GET to retrieve data from a very simple two table MySQL db. ...
0
votes
0answers
20 views

Stored procedure to compare stored procedure result sets

Originally posted as an answer to a SOF question, this is about a generic procedure that allows back-end developers (e.g. SQL developers) to compare the results sets obtained by running two procedures ...
1
vote
2answers
56 views

Get Word Doc from SQL table, save temp file, edit that file, save new doc in a different table

I have Frankensteined the crap out of this method and just wanted to make sure of two things. I'd like to make sure that this is the best route to take. If it isn't, I would welcome with open arms ...
1
vote
1answer
50 views

Adding a List to SQL database (Entity Database)

I got a database with members, each member has a list of sports they do. now I want to loop through a listbox and add every selected item to my database. This is my database : And this is my code ...
2
votes
1answer
39 views

SQL Inserting up to 10'000 records from a table to a secondary table

I'm relatively new to ASP.net and working with larger data sets as a fresh graduate, and I am currently maintaining/developing for an ASP webform and T-SQL system at the moment where we have a ...
3
votes
3answers
278 views

SQL Procedure that will calculate the Last Price paid for a certain Part

I am trying to write an SQL Procedure that will calculate the Last Price paid for a certain Part. ...
3
votes
1answer
43 views

Ruby on rails instance loops

I am trying to use a custom sql query to display different attributes for a product i.e. Size and Price. The query I have when running in console displays as it should ...
6
votes
2answers
106 views

Finding customer searches and graphing their items

One of the reports I am working on requires me to locate searches a customer makes, and graph them in certain manners. I have one query I'm particularly concerned about. This one takes the most ...
0
votes
1answer
33 views

DRYing two very similar SQL conditional inserts

I have two very similar SQL statements ...
4
votes
1answer
43 views

Highest andLowest in PLSQL

I have made a procedure to display the highest and the lowest popular Items for a particular time of a given date. The procedure works with no errors or exceptions ...
5
votes
1answer
45 views

Report for a restaurant showing hourly sales information for a given timespan

I have been tasked with creating a report for a high-volume restaurant that shows hourly sales information for a given timespan. When generating this report for a single day, it's very quick, but when ...
4
votes
1answer
34 views

Fetching all the project's items for a specific project id

In the following query, I am fetching all the projects items with specific project id. At the same time, I am using a join in Select to fetch the delivered quantity of items of this project only. The ...
6
votes
3answers
325 views

Sum of orders per customer ID

So, I have two tables, customers and orders and I want to get all orders associated with a customer. The tables look like this: ...
7
votes
2answers
47 views

Execute every Monday that is not the Monday following last Saturday in month

I think the following is good but wondering if it can be simplified or improved. My condition for code execution is the following: It is a Monday AND It is not the Monday following the last Saturday ...
0
votes
0answers
31 views

django performance - is sql view good enought?

As part of my present project I revived task to deliver group of on-line reports. As per reports complexity and best performance on my radar I decided to write sql view on Posgres and next attached ...
18
votes
1answer
52 views

Cross-posts from Stack Overflow to CR

What this code does it explained in detail in the top portion of the query. I have left out part of the list of language tags as I felt it was just noise. You can see the full collection here. You ...
12
votes
1answer
48 views

Mysql homeowner database

I'm working on a project right now. I don't have much experience in MySQL, or database design, so I was hoping someone could review what I have now. This database must be able to track multiple home ...
12
votes
2answers
83 views

Query to count interactions between users

The source data represents interaction between people. Some are internal, others are external. The internals are recorded in the Users table (represented as the ...
8
votes
3answers
135 views

XML splitting query very slow

I have a query to split a delimited string into to multiple columns. The query is extremely slow and takes more than 5 minutes to handle 80444 rows. How can I improve execution time? ...
4
votes
0answers
31 views

Coin flips and Dice rolls in a stored procedure

I saw a post in chat a few days ago, of what I thought was an interesting (albeit fairly simple) statistical challenge: By @skiwi: If I shoot once with 50% chance of target A and 50% of target B, ...