Microsoft's SQL Server is a relational database management system (RDBMS) that runs as a server providing multi-user access to a number of databases. It originated from the Sybase SQL Server codebase, which is why both products use the extension of SQL called Transact-SQL (T-SQL).
0
votes
0answers
10 views
Replace CASE WHEN statement with something more sensible
While checking my SPs today I found that I have,
...
6
votes
0answers
18 views
Forgotten zombie killers
Zombies are questions with no upvoted answers.
I came up with this SEDE query to find answers with zero votes on questions that have no accepted answer and no answers with greater than zero score.
In ...
8
votes
1answer
50 views
Naruto, where are you?
During Winter Bash 2014, the Naruto hat was awarded to users who gave an answer that got accepted but hasn't received votes for 12 hours.
It was a weird thing, really:
why would someone ever accept a ...
2
votes
2answers
37 views
Haversine formula in SQL
This is an implementation of the Haversine formula in Microsoft Transact SQL.
How can I simplify the function?
...
2
votes
0answers
6 views
Create View having trouble with JOINS [migrated]
I have 5 regions that each contain 1 store and 1 warehouse. I have a table that contains the regions and corresponding stores and warehouses. I also have a table that lists the inventories of both ...
4
votes
2answers
22 views
Selecting internationalization preferences
I have a query below using CTE. I am wondering if there are some approach other than this. So I am collecting some advise to refactor it. Or does the query look good even though I am using this ...
2
votes
2answers
107 views
Timeouts when calculating stock ratings using LINQ
I'm getting a lot of timeouts with the following code and I'm running this method around 10,000 times so I need to figure out how I can optimize this so the timeouts will stop. I am getting timeout ...
1
vote
1answer
17 views
Inserting into Group_roles table for all rows in Group table
I have five groups in Group table as Group1, Group2, ...
3
votes
1answer
57 views
Is the usage of SQL Cursor appropriate here?
I'm looking for following kind of feedback:
Firstly, is the use of a cursor an overkill here?
Is there a simpler way to do what I'm doing in SQL?
Could the script go terribly wrong?
I have limited ...
7
votes
2answers
129 views
What are my highest activity streaks?
I have written the following query to figure out activity streaks on a per-user basis. I find it... Ugly... And would love to improve it!
Limitations
Those are explained as commented text at the ...
17
votes
6answers
619 views
Winterbash 2014 “Red Shirt” Estimation
Some fellow code reviewers (hi @Janos!) have been inquiring about a SEDE query to allow to check progress of the Red Shirt "hat" progression.
Try it here!
Background
Red Shirt
cast 5 ...
8
votes
6answers
342 views
SQL Server: Select first value if exists, otherwise select default value
Given an instance of SQL Server, imagine there's a table named Configuration, which has three columns: ID, ...
7
votes
1answer
63 views
Fascinating, ma'am
Finding a famous question with high score I haven't voted on yet was proving to be a bit difficult for me. So I came up with this simple SEDE query to find good candidates:
...
13
votes
2answers
75 views
Bling Accounting - Badge summaries
A question was raised in chat (hi @malachi) about how you can count the number of badges of a given metal (bronze, silver, gold). This can be hard to do because the medal details are not stored in the ...
8
votes
2answers
168 views
Checking for Duplicates, Except
I have a query that returns records that have duplicate names within a given DBSTATUS. I used to do this with a direct connection to the remote oracle database ...
10
votes
3answers
130 views
Tag badges per user SEDE query
I wanted to see where I lined up with other users on tag badges (I wanted to count just bronze but figured I would start with all of them to start out with).
Here is the query that I ended up with:
...
12
votes
2answers
82 views
Archaeologist Progression
One of the badges available on Stack Exchange is the Archaeologist badge. This is awarded for making 100 edits on posts that had been inactive (at the time of the edit) for 6 months.
There is no ...
5
votes
3answers
90 views
Selecting Employee details from a complicated schema
I have a "litte" problem with my stored procedure because I need some values selected at a point in time.
I need to do many select and group by's in my Left outer join which looks and feels like I am ...
3
votes
1answer
41 views
SQL stored procedure for fruit checks with several joins
I developed a SQL stored procedure which works lovely, but when under stress, it takes more than 2 minute to get back to the user. I am providing the stored procedure but would also provide the table ...
3
votes
2answers
74 views
SQL cursors and dynamic SQL
The idea is to have a heading for each date returned by the query. This could be any number of dates. I dynamically altered the #table which is ugly but there is no other way.
Ultimately I would ...
12
votes
3answers
136 views
Creating an object oriented model in VBA using COM and ADODB from 2 depended SQL tables
The story...
A bit of background info and how is the database designed...
Please notice you don't really have to rebuild the tables in SQL but I shared an SQL Fiddle just in case and screenshots1 of ...
3
votes
2answers
46 views
Running validation procedures on a table
I am working on a project that has to parse through a text file and perform validations on certain fields in the file. Some of the validations are basic(i.e. data types), and other's are a little more ...
2
votes
2answers
34 views
3
votes
1answer
259 views
File Exists Script Task for SQL Server SSIS
I have an SSIS package for SQL Server 2008. In this package, I need to verify that a file exists that matches a pattern of SomeFileName_*.txt where the wild card ...
3
votes
1answer
38 views
Better way to divide by the total count SQL
I have this query where I am working out the percentage of peoples occupation, I have a sub query that works out the total count of people codes and that result is what I used to divide by, I could ...
11
votes
4answers
313 views
LW, MTD, STD, YTD Sales… all in line and looking pretty
That's Last Week, Month-to-Date, Season-to-Date, and Year-to-Date sales.
I have a table-valued function for each one, that takes a date and returns all ...
11
votes
2answers
159 views
SQL-Server Merge Statement with .NET DataTable
Background:
I'm trying to synchronize order information between Oracle and SQL-Server databases and allow users to be able to track any changes between what has been replicated via an ASP.NET web ...
2
votes
2answers
38 views
Assign Specific Value If BitField Is True
My colleague created this SQL to handle certain user selections which are based on an on/off bit flag. The goal is to identify those individual bit flags with a specific integer which will be used in ...
3
votes
2answers
45 views
Extract last record inserted for each key identifier
I have a table with this schema
ID, int primary key
PathKey, string not null
InsertDate, datetime not null
Value, int not null
In the table there are a ...
3
votes
2answers
74 views
Finding the runtime of a task by name
Can this query be improved? I iterate over a couple of thousand times, and if I can improve it, it should decrease the run time. Otherwise I might look into using a batch instead of doing one at a ...
1
vote
2answers
109 views
Weekly fee calculation
I have the following query. Its execution took 15 min and it's too much slow. Is there a way to optimize it?
...
7
votes
3answers
249 views
Normalizing Invoices
I need to move data from one database to another, and since I don't have SSIS I'm doing this ETL with T-SQL scripts.
One of the source tables contains invoice details, and features a column that ...
3
votes
2answers
109 views
Role-based access control query
I use SQL Server, and though not pertinent here, Railo's CFML engine.
I'm not great at SQL Joins, but I worked through this one and got the result set that I want. This particular SQL will only ever ...
5
votes
1answer
49 views
Saving and updating records
I have made two SQL statements as I was unable to figure out how to solve my problem. One is called Product_GetID and the other ...
0
votes
2answers
234 views
6
votes
2answers
152 views
Follow up to Top Active Answerers on Stack Exchange site
This is a follow up to Top Active Answerers on a Stack Exchange site
Actual SEDE Query --> Top Active Answerers on site
Do I have a good understanding of the ...
3
votes
2answers
276 views
Top Active Answerers on a Stack Exchange site
I created this query back in January to find the top active answerers on Code Review and thought that it might be a good idea to get other opinions on my SQL coding using a database that we can all ...
3
votes
1answer
27 views
SQL update statement
I have a SQL update statement which does what it's supposed to. However, I have a feeling that the way I wrote it isn't the best. I am not highly experienced in SQL so any pointers or hints on how can ...
2
votes
3answers
247 views
Rewriting SQL IN statement with long list of parameters
These IDs seem like they should be a table as, there are several queries that use the same list of IDs. The following needs to be refactored.
...
7
votes
2answers
94 views
Query for your Answer Progress
I was inspired to write a query that produced a graph mapping the perceived quality of your Stack Exchange answers over time.... in other words, "Are your answers getting better, or worse?"
Because ...
3
votes
2answers
82 views
Basic T-SQL to compare two same length strings
I've come up with the following T-SQL to compare similar same length strings.
Example usage would be:
OCR returns a value which is expected to be in the database.
PATINDEX is used to check the ...
3
votes
1answer
80 views
Query for combining two SELECTs to get active users and 1 inactive user
What this query does:
Gets multiple users with the first select in the union; those are the active users.
But when a user is ...
2
votes
1answer
24 views
Optimizing sales report stored procedure
I need to optimize the following stored proc. Please let me know of any techniques or modifications that I can make to optimize this piece of code.
The procedure is for a report that needs to run ...
5
votes
2answers
108 views
DateTime internal representation to FILETIME
Here is a routine I put together to convert SQL Server's internal DateTime representation (BINARY(8)) to a ...
1
vote
1answer
34 views
Too many parentheses to format a percentage in SELECT
This query seems to have way too many parentheses.
...
3
votes
1answer
86 views
Monthly change query with SQL Server 2012
I'm new to SQL and working on a monthly change calculation. I want to calculate the change between this months balance and the previous months balance.
This query is working for me but does not ...
3
votes
2answers
100 views
Similar statements need duplication removed
How would you refactor this?
ExpensiveQuery must only run when necessary.
...
4
votes
1answer
83 views
Unduplicated counts over various dimensions
I'm working with some data on college students, which has a unique key of SSN, Campus, ...
3
votes
3answers
303 views
Get and display employee time clock data
I have been making a web page in PHP and JavaScript to take employees clock times and do some things with them so I can display them. Everything is currently working quite well, but the code looks ...
6
votes
5answers
1k views
Transaction handling for multiple SQL statements
In this code I update two tables called Payment and SalaryTrans. First I insert records (Salary payments) to ...