sql

one-to-many query selecting all parents and single top child for each parent

There are two SQL tables: Parents: +--+---------+ |id| text | +--+---------+ | 1| Blah | | 2| Blah2 | | 3| Blah3 | +--+---------+ Childs +--+------+-------+ |id|parent|feature| +--+------+-------+ | 1| 1 | 123 | | 2| 1 | 35 | | 3| 2 | 15 | +--+------+-------+ I want to select with single query every row fro...

(SQL) Grouping by End of week

I have a report which needs to total up the number of registrations per week, over the last ten weeks and display it as "Week Ending October 10th, 2009" 500" "Week Ending OCtober 3rd, 2009" 400" "Week Ending September 26, 2009" 1000" etc... Here is my query: SELECT Count(*) as [Total] ,Week = DateAdd(day, -1 * da...

PLSQL Collections - to create a collection of records or not?

Hi, I am new to Oracle PL/SQL and am having some difficulty conceptualizing collections with regard to records. I have the following problem: I want to compare various fields of a record from the client table to various fields of a record from the person table. e.g., LName, FName, Soc. (unfortunately, there is no identifier to easily l...

How does MySQL store timestamps internally?

How does MySQL store timestamps internally? Is it an int, or a string or something else? ...

Oracle SQL LOOP (between 2 dates) and Counting

I'm stuck with a SQL Query. I have this table: [Reserve] ID NUMBER START_DATE, DATE END_DATE, DATE .......... (more cols) I need to look if the next count is more than 0 for any of the dates between START_DATE and END_DATE (including both). If the count is more than 0, the query must stop inmediately and return "There is no loca...

Generate SQL statements with python

I need to generate a list of insert statements (for postgresql) from html files, is there a library available for python to help me properly escape and quote the names/values? in PHP i use PDO to do the escaping and quoting, is there any equivalent library for python? Edit: I need to generate a file with sql statements for execution lat...

Doublet select/delete in mysql database

Hi everybody ! Here is my question : I have a database (mysql) like this : id0 id1 id2 And i would like to delete the lines who have the couple (id1,id2) in common to keep only one. Example : 1/1/1 2/1/2 3/1/2 <= Delete 4/2/1 5/2/3 6/2/3 <= Delete I hope this is clear enough for you to help me :) Thanks ...

do spaces in sql tables fubar queries?

I'm having an issue I'm generating sudoku puzzles and the solution to the puzzle are being stored in this format: xxxxxxxxx xxxxxxxxx xxxxxxxxx xxxxxxxxx xxxxxxxxx xxxxxxxxx xxxxxxxxx xxxxxxxxx xxxxxxxxx 8 spaces between the 9 sets, I cannot seem to query the row, it returns a blank row.. Is this because there is spaces? If I do a sel...

MS Access Limit

What's the equivalent of mysql Limit in ms access. TOP is not sufficient since I'm going to use it for pagination. Thanks ...

How to filter by association count?

Let's say I have models that look like this: class Foo < ActiveRecord::Base has_many :bars, :through => :cakes has_many :cakes end class Bar < ActiveRecord::Base has_many :foos, :through => :cakes has_many :cakes end class Cake < ActiveRecord::Base belongs_to :foo belongs_to :bar end How would I get all foo...

SQLite: Combine SELECT and DELETE in one statemet

Hi, I need to run two statements like so: Select amount from db where ID=5 DELETE from db where ID=5 Currently I prepair a run two different statements. I wonder if there is a way to combine it in on statement. Basically all I need to do is to get and amount column from the row before is is deleted. thanks ...

Select 1 row per order, but include order_line table?

I have two tables order [ order_id ] order_line [ order_id, product_id, cat_id ] I want to select every order & associated order_line, but I want row per order, and i only want to select cat_id from order_line so i want a result like this.. results: [0] order_id, cat_id1 , cat_id2, cat_id3 [1] order_id, cat_id Possible? ...

Help with a SQL Query

Hi All, I am wanting to perform a Threshold query, whereby one would take the Value of a field from Today, and compare it to the value from yesterday, this query would look something like this, but obviously, it is wrong, and I can't find how to do it: select TableB.Name, TableA.Charge, ( select Charge from Tabl...

connect to a DB inside awk script

Hi In a shell script we can connect to a Database using sqlplus on unix. can i perform the same thing inside an awk script? i need to access the output of a select query inside an awk script.is that possible? ...

Hibernate DetachedCriteria in FROM clause

I have 2 tables: orders: id items: id, orderId, total, flag I would like to make following query using Hibernate Criteria (DetachedCriteria): SELECT o.id, SUM(i1.total), SUM(i2.total) FROM orders o LEFT JOIN ( SELECT i.orderId ...

Reducing values in one table until reserves depleted in another - recursion?

Hello everyone, I have two tables - let's call them dbo.ValuesToReduce and dbo.Reserve The data in the first table (dbo.ValuesToReduce) is: ValuesToReduceId | PartnerId | Value ------------------------------------- 1 | 1 | 53.15 2 | 2 | 601.98 3 | 1 | 91.05 4 ...

Building a comma separated list?

hi there I'm tryin to use SQL to build a comma separated list of cat_id's the code is: declare @output varchar(max) set @output = null; select @output = COALESCE(@output + ', ', '') + convert(varchar(max),cat_id) edit: changed '' to null, STILL same. but the output im getting is like so: , 66 , 23 the leading comma sh...

Grouping items into first occurances

I totally can't get my head around writing a correct query for my problem this morning so here's hoping that someone out there can help me out. I have a database table called Sessions which basically looks like this Sessions: SessionID SessionStarted IPAddress ..other meta data.. I have a requirement where I am to show how ma...

Using a 'case when' in a Doctrine select statement

I have a select query I'd like to perform with Doctrine: $resultset = Doctrine_Query::create() ->select("t.code, t.description, case when t.id_outcome = 1 then 1 else 0 end as in_progress") ->from('LuOutcome t') ->orderBy('t.rank') ->fetchArray(); And it barfs on the 'case'. The documentation does not mention that it'...

Execute sql file on a SQL Server using C#

I and trying to create a method to run a .sql file on an SQL Server database. The code i have is: SqlConnection dbCon = new SqlConnection(connstr); FileInfo file = new FileInfo(Server.MapPath("~/Installer/JobTraksDB.sql")); StreamReader fileRead = file.OpenText(); string script = fileRead.ReadToEnd(); fileRead.Close(); SqlCommand comm...