sql

SQL queries with date types

I am wanting to do some queries on a sales table and a purchases table, things like showing the total costs, or total sales of a particular item etc. Both of these tables also have a date field, which stores dates in sortable format(just the default I suppose?) I am wondering how I would do things with this date field as part of my que...

Easier way to select MAX() based on conditions?

Hi, Given the following table format... id | date | value ___|____________|______ 11 | 2010-01-01 | 50 11 | 2010-01-02 | 100 12 | 2010-01-01 | 150 12 | 2010-01-02 | 200 ... I need to select the id that corresponds to the maximum value on a day that I specify. The only way I've figured out how to do this so far is using a sub-qu...

How to Optimize Queries in a Database - The Basics

It seems that all questions regarding this topic are very specific, and while I value specific examples, I'm interested in the basics of SQL optimization. I am very comfortable working in SQL, and have a background in hardware/low level software. What I want is the tools both tangible software, and a method to look at the mysql databas...

How to stop Databasemail from executing

Hi I was playing around with a stored procedure and I think I made an endless while loop(I think I forgot to close the cursor) now it is sending me emails and I don't know how to stop it. How can I kill it? edit this is what I see when I do what remus told me to do Sorry I don't know how to output the results nicer. 1,0,2010-07-06 1...

Update with function for value only seems to evaluate the function once

I'm trying to change all the NULLs in an INT column to an incrementing value using a statement like this: UPDATE [TableName] SET [ColumnName] = dbo.FunctionName() WHERE [ColumnName] IS NULL where the function looks like this: CREATE FUNCTION FunctionName() RETURNS INT AS BEGIN RETURN (select MAX(ColumnName) + 1 from TableName) EN...

SQL wildcard matching excluding a specific pattern.

Hi, sorry about the question I am a newbie to sql. i am attempting to create a search query for our database and I was wondering how would you filter certain words from your query for example: Here is the sample data (the name column): Jean, Jain, Joan, Jorn, Juan, John, Juin Lets say that we are searching for the names that start with...

join two mysql queries based on parameters

hi there i have two queries, querying the same table, but based on different parameters, i then need to mush these two result sets together based on certian parameters //get initial text Q1 SELECT campaign_id AS campaign_id, from_number AS mobile, received_msg AS join_txt, date_received AS join_txt_date FROM received_txts WHERE act...

SQL Server Row by Row operation

I am trying to do some operation in column 'Increment' based on previous row record in column 'Value' e.g. row_num| Period | Measure | Decay 1 | Jan 08 | 10 | 2 | Feb 08 | 18 | 3 | Mar 08 | 7 | 4 | Apr 08 | 67 | i would like to update column 'Decay' based on a formula row_num| Period | M...

Displaying a MYSQL display set for a year with monthly totals

I have two tables, sales and costs, each with a costs fields, and a date field of type date. The rest of the fields are not relevant for what I want to do. I want to take the totals of each month, and display them together. I am unsure if most of this should be done in SQL, if it must be one or several queries, of if I must manipulate...

ruby on rails does update_attributes protect against sql injection?

Does update_attributes protect against sql injection? Example: if @user.update_attributes(params[:user]) # updated end I know find(), and {} and [] do in find :conditions, but didn't see any info on this method. ...

rails is Model.new safe from sql injection?

Does ModelName.new protect against sql injection? Example: @user = User.new(params[:user]) @user.save I've read the rails security docs and didn't see anything about inserts via Model.new. Thanks! ...

Restricting an SQL query to certain fields in a table not used by the query

I have a products table, with the fields productname, category and cost, of type varchar, varchar and double. I then have a sales table, with the fields productname, cost, and saledate, of type varchar, double and date. Lastly, I have a purchases table with the fields purchase, cost and purchasedate, of type varchar, double and date. ...

rails: does the build method protect against sql injection

Does build protect against sql injection? Example: @post = @user.posts.build(params[:post]) @post.save Didn't see build in the rails security docs. Thanks! ...

grouping common results in sql

I have a products table, with the fields product, category and cost, of type varchar, varchar and decimal. I then have a sales table, with the fields client, productname, quantity, cost, and saledate, of type varchar, varchar, int, decimal and date. I want to show all of the products sold for a month, say the current month. However, I...

Oracle exporting SQL of the Database structure

I want to create an sql script that can recreate a DB that I already have. I want to recreate the DB without data inside. So is there anyway with sqlplus of exporting a DB of a user? ...

sql sequences count

hello everyone i have table a follows userid answer 1 true 1 true 1 false 1 true 1 true 1 true 2 true 1 true i want to get the latest count of true sequence per user so i will get userid count 1 4 2 1 please help ...

display sql custom text from table column result

let say i do select score from table1. this will return result score ---- 0 20 40 how to use case, to change the output to if 0->strongly not agree 20->agree 40->very agreed ...

How do I get a SQL function to return a list to be used by the IN statement in a WHERE clause?

Hey guys I have a complex SQL Query, that needs to be filtered further. Part of the WHERE clause looks like this: Where P.PeriodID in (36, 37) I need to get it to look more like this: Where P.PeriodID in dbo.GetPeriodsInRange(@startDate, @endDate) The function must return a list of PeriodIDs to be used by the IN statement. I reall...

Returning multiple columns and grouping by month

I have three tables products, sales and costs. The sales and products table each have a costs field of type decimal and a date field of type date. Additionally, the sales table has a productname field, which is matched to a productname field in the products tables. The products table also has a category field. I want to select all sa...

CASE vs. DECODE

Referring to a previous question, i was wondering if its always possible to replace DECODE by CASE and which one is better for performance? ...