query

RESTful URL design - how to query using OR between parameters

How would you design a RESTful query to support OR operand between parameters. Let's say my resource has two fields field1 & field2. How would you design the URL to enable the following query: "Get myresources where field1=x OR field2=y" Designing queries in REST is pretty straight forward, but I have only seen queries that supports A...

Access DB query - need help updating certain records

I have an access DB which we use to track tickets. Each ticket may have multiple occurrences because of different programming changes associated with that ticket. Each record also has a program_type field which is SVR or VB. Example: 123456 - SVR - SomeCode 123456 - VB - SomeVBCode I've added a column to the database called VB_Flag, ...

How do I perform a GROUP BY on an aliased column in MS-SQL Server?

I'm trying to perform a group by action on an aliased column (example below) but can't determine the proper syntax. SELECT LastName + ', ' + FirstName AS 'FullName' FROM customers GROUP BY 'FullName' What is the correct syntax? EDIT Extending the question further (I had not expected the answers I had received) wou...

How do I join the most recent row in one table to another table?

I have data that looks like this: entities id name 1 Apple 2 Orange 3 Banana Periodically, a process will run and give a score to each entity. The process generates the data and adds it to a scores table like so: scores id entity_id score date_added 1 1 10 1/2/09 2 2 ...

Stored procedure Delete query problem

I have stored procedure: ALTER PROCEDURE [dbo].[k_ShoppingCart_DELETE] @cartGUID nvarchar AS DELETE FROM [dbo].[k_ShoppingCart] WHERE CartGUID = @cartGUID When I execute this sp; exec dbo.k_ShoppingCart_DELETE '32390b5b-a35a-4e32-8393-67d5629192f0' result: 0 row (s) affected Actually when I try this query: Delete FROM k_S...

Ruby on rails count and group!

I have a database with the following schema: t.string "mail" t.integer "country" t.boolean "validated" t.datetime "created_at" t.datetime "updated_at" And I want to find the top 5 countries in the database, so i go ahead and type @top5 = Mail.find(:all,:group => 'country',:conditions => [ "validated = ?" , "t" ...

Using 'distinct' in a MySQL query

I have the following query. "SELECT p.author_name, p.author_id, DISTINCT p.topic_id, t.title FROM `ibf_posts` p, `ibf_topics` t WHERE p.topic_id = t.tid ORDER BY pid DESC LIMIT 8" When I run it, I get the following mysql Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL s...

Efficient Paging (Limit) Query in SQLServer 2000?

What would be the most efficient way to do a paging query in SQLServer 2000? Where a "paging query" would be the equivalent of using the LIMIT statement in MySQL. EDIT: Could a stored procedure be more efficient than any set based query in that case? ...

Please help me with a MySQL SQL subquery

I have two tables named foo and bar, hypothetically speaking. foo has columns foo_id, foo_fluff bar has columns bar_id, foo_id, timestamp I need a query which will retrieve return one row for any foo_id that the table bar contains, with the latest timestamp. So, if bar has three rows, two of which have a foo_id of 1, and 1 of which ha...

mysql query to get the count of each element in a column

Hi, i have a table called category in which i have main category ids and names and each main category has sub category ids and names.i also have a product table with a category id column which either has a numeric sub category id or a 2 letter main category id eg:EL for electronics..my problem is how to get top categories ie., number of...

How to find the one hour period with the most datapoints?

I have a database table with hundreds of thousands of forum posts, and I would like to find out what hour-long period contains the most number of posts. I could crawl forward one minute at a time, keeping an array of timestamps and keeping track of what hour had the most in it, but I feel like there is a much better way to do this. I wi...

Finding unmatched records with SQL

I'm trying write a query to find records which don't have a matching record in another table. For example, I have a two tables whose structures looks something like this: Table1 State | Product | Distributor | other fields CA | P1 | A | xxxx OR | P1 | A | xxxx OR | P1 | B ...

MySQL query to return only duplicate entries with counts

I have a legacy MySQL table called lnk_lists_addresses with columns list_id and address_id. I'd like to write a query that reports all the cases where the same list_id-address_id combination appears more than once in the table with a count. I tried this... SELECT count(*), list_id, address_id FROM lnk_lists_addresses GROUP BY list_id, ...

Get a list of dates between two dates

Using standard mysql functions is there a way to write a query that will return a list of days between two dates. eg given 2009-01-01 and 2009-01-13 it would return a one column table with the values: 2009-01-01 2009-01-02 2009-01-03 2009-01-04 2009-01-05 2009-01-06 2009-01-07 2009-01-08 2009-01-09 2009-01-10 2009-01-11 2009-01-12...

How do I insert into a table if a value does not exist, but only for certain criteria? (MS SQL Server)

Hi, Im very new to SQL but need to write a query to do the following. Using MS SQL Server 2005. Profile DefinitioninProfile Definition ------ ------------------- ---------- ProfileID DefinitionID DefinitionID ProfileType ProfileID ...

Convert query to variables for multi-language

I have built a couple of multilingual sites which output labels from an XML file - the XML file used is determined by a cookie (ENG, NL, ESP etc) and looks like this: <resources> <coming>Coming soon</coming> </resources> I've been using a script which, within a function, takes each XML child and makes a variable from it's value: ...

Question about a query

$sql = "SELECT count(u_id) AS num_replies FROM `replies` WHERE `u_id`='".$uid."'"; $res = mysql_query($sql) or die(myqsl_error()); Will that return the number of replies a user with id $uid has made? If not, can anyone suggest something that will? Thx for the help. ...

MySQL "SET NAMES" near the top of the slow query log

On a recently launched site, I noticed that, well over the actual heavy queries on the site, the most costly request, out of several million queries, is actually SET NAMES, which clocks over 2.3 seconds on average, while various multi-join union queries are well below 2 seconds. In the end, this places it near the top of the slow query l...

SQL query to select unreferenced rows

I'm having a brain-dead moment... I have two tables described by: CREATE TABLE table_a ( id INTEGER PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255) NOT NULL UNIQUE (name)) CREATE TABLE table_b ( id INTEGER PRIMARY KEY AUTO_INCREMENT, a_key INTEGER ...

Why would a sql query have "where 1 = 1"

I was going through a few queries I am maintaining, and a programmer had put in the queries "where 1=1" to me that always seems to evaluate to true. Are there benefits to this? Duplicate: Why would someone use WHERE 1=1 AND in a SQL clause? That question isn't an answer to this question. Where-clause: select * from table where 1...