sql

already an open DataReader associated with this Command - when I'm not using datareader??

I am getting an error that an open DataReader associated with this Command, when I'm not using datareader(though probably executereader() is the same thing) how would I close this if I don't have a datareader present? using (SqlConnection conn = new SqlConnection(ConnectionString)) { SqlCommand cmd = new SqlCommand("spSelectAllTypes",...

SQL efficiency - [=] vs [in] vs [like] vs [matches]

Just out of curiosity, I was wondering if there are any speed/efficiency differences in using [=] versus [in] versus [like] versus [matches] (for only 1 value) syntax for sql. select field from table where field = value; versus select field from table where field in (value); versus select field from table where field like value; ...

Mysql Update Records

I have to update one column from my user table . Current record in User Table **id , user_name** 1 , sachin rathore 2 , dilip CHOUHAN 3 , GariMA JAIN I want to update user_name column like this 1 , Sachin Rathore 2 , Dilip Chouhan 3 , Garima Jain User column should be in titlize form means first letter of each word should ...

Convert result within statement

Let's say I have the following simple query SELECT TOP 1 name FROM months which returns name = "march". Is it possible to convert this result? Instead of "march" I want name = "3". Is SQL capable of doing such things? I'm using a MSSQL database. [update] Corrected the query. While writing this simple example I mixed it up with MySQL ...

How to select top N from a table

Now I am working on a project, which has to select top 25 records from a table according to one column, let's call it Num. The case is 1) the table is not sorted by Num; I know this part can be done by using "GROUP ORDER BY" 2) the number of the records in the table might be less than 25. For this question, is there any way to make ...

Measuring the complexity of SQL statements

The complexity of methods in most programming languages can be measured in cyclomatic complexity with static source code analyzers. Is there a similar metric for measuring the complexity of a SQL query? It is simple enough to measure the time it takes a query to return, but what if I just want to be able to quantify how complicated a qu...

oracle (or any relational) data model question. Parent with fixed number of Childeren??

This is a particular problem that I have come across many times, but I have never really found a simple solution to this (seemingly) simple problem. How to you ensure that a given parent has a fixed number of children? 1) Example. How do you make sure a given class has only , say, 50 students enrolled..? create table class( class_...

SQL: case-when statement with "exists"

I'd like to be able to set add a field that answers the question "For a value in this record, does that value meet some condition in another table?". I thought I'd try a case-when with an exists, but Teradata (my dbms) does not like it. Any recommendations? select foo, (case when exists (select x.foo from some...

Pivot a one row column T-SQL

I have a one row table returned from a query that looks something like this [Date1] [Date2] [Date3] [Date4] [Date5] [Date6] and I want all the Dates to stack up like this [Date1] [Date2] [Date3] [Date4] [Date5] [Date6] How would I go about doing this without a bunch of separate queries and union statements? I have tried playing a...

SqlDataReader return rows

If we select a large number of rows and use an SqlDataReader, will it return the rows as they come or will it wait until the operation is complete? This is with C#.net ...

Migrating content from legacy blogging platform to wordpress

i am working on a massive site for a client, but their current website (thousands of pages deep) is built on a legacy blogging platform that was built specifically for them in aspx. My job is to take all of the SQL tables filled with data(comments, categories, posts, titles, dates, etc) and convert them to a wordpress ready format. I wan...

Are these two queries the same - GROUP BY vs. DISTINCT?

These two queries seem to return the same results. Is that coincidental or are they really the same? 1. SELECT t.ItemNumber, (SELECT TOP 1 ItemDescription FROM Transactions WHERE ItemNumber = t.ItemNumber ORDER BY DateCreated DESC) AS ItemDescription FROM Transactions t GROUP BY t.ItemNumber 2. SELECT DISTINCT(t.ItemNumb...

Can I update two identical tables in with one query - mySQL

Can I update two identical tables with one query? TABLEA _____________________________ | id | value | |_____________|_____________| | 1 | a | | 2 | b | | 3 | c | | 4 | d | | 5 | e | |_____________|_____________| ...

How can I improve the performance of this query?

Recently I had this question, and everything worked properly until I sent it to my server at DreamHost. The query bellow seems to take too long to execute and I can't figure out why so many rows are processed at once. In my local server the same query was executed in 0.3 seconds. SELECT feed_entries . * FROM feed_entries WHERE id IN (...

how to get input into an Jet OLE DB Query

Background: I am using Excel 2003 I have 2 Excel files (Source, list), one is essentially my source data. The second is where I am using the excel "Import External data" function to get the data in teh second sheet. I am then using the modifiy query to allow me to use SQL to query my data and limit the data I am displaying. My SQL query...

MySQL: hourly average value

Hello, I have a table with a 'timestamp' column and a 'value' column where the values are roughly 3 seconds apart. I'm trying to return a table that has daily average values. So, something like this is what i'm looking for. | timestamp | average | | 2010-06-02 | 456.6 | | 2010-06-03 | 589.4 | | 2010-06-04 | 268.5 | etc... ...

MySQL JOIN with 3 tables and COUNT() not working

Hi, I'm having a problem with my MySQL statement. I need a query that counts the number of comments and the number of topics a user has created. My table structure is something like this: Table 'users' ------------- user_id user_name ... Table 'topics' -------------- topic_id topic_user_id ... Table 'topiccomments' -----------------...

Date serial in SQL ?

How can I convert a day [1-31] and a month [1-12] and a year (all int), to a date serial IN SQL (without converting to varchar)? ...

Under what conditions would SELECT by PRIMARY KEY be slow?

Chasing down some DB performance issues in a fairly typical EclipseLink/JPA application. I am seeing frequent queries that are taking 25-100ms. These are simple queries, just selecting all columns from a table where its primary key is equal to a value. They shouldn't be slow. I'm looking at the query time in the postgres log, using the...

SQL - When would an empty OVER clause be used?

I'm analyzing some code that utilizes empty OVER clauses in the contest of Count(). Example: SELECT ROW_NUMBER() OVER (ORDER BY Priority DESC) AS RowID, CAST((COUNT(*) OVER() / @pagesize) AS Int) AS TotalPages, I'm trying to understand why the empty OVER clause is being used here. There are other standard select ele...