sql

SQL Server - Order by case

I am trying to get the following query to display the results in alphabetical order by City except for the fact that I want "Berlin" to appear at the top of the list So the results would look something like Berlin Algeria Australia Fiji Greece ... Hope that makes sense, I currently have the following... SELECT CompanyName, City ...

How to write a sql query to figure out if a value falls within a range

tableA contains {id, fromPos not null, toPos} fromPos and toPos denotes a range of values for a particular row toPos is a nullable field and has the following values tableA (1, 5) // means any position greater than or equal to 5 tableA (2, 5, 10) // means any position between 5 and 10 (inclusive) tableA (3, 6) tableA (4, 7, 9) ...

Subdivide timestamps into 3 timeslots

I have following problem where I have to group different transactions into timeslots. Suppose you have a table with records which contain an entry datetimestamp. These records are created by users (operators) who work in different shifts Shift 1: 5 - 13h // Shift 2: 13 - 21h // Shift 3: 21 - 5h Now I want to have a flexible query whic...

Getting more details out of a SQL subquery

I have a SQL Query giving me a list of double records in my database. select periodid, itemid from periodscore group by periodid, itemid having count(*) > 1 This works as expected, but now I would like to retrieve additional fields of these records (such as date last updated etc). So I tried: select * from periodscore where period...

Packaging SQLite DB with my application.

I have created a SQLite database on my PC. I have imported it into my assets folder in the project directory. How do I access it from my Android application? ...

Split single row into multiple rows in SSIS

Hi. Given an input of the format (currently a flat file): Name, StartDate, EndDate with sample data like Peter, 2010-09-01, 2010-09-04 I was wondering which SSIS task I could use to split this single row into a row per day: Peter, 2010-09-01 Peter, 2010-09-02 Peter, 2010-09-03 Peter, 2010-09-04 I know I can probably do it with a s...

Call one Stored Procedure inside other Stored Procedure on different servers

How to execute one Stored Procedure inside other Stored Procedure? NOTE: Both Stored Procedure's reside in different servers ...

Ranking without ranking functions

Hi, I have written the following query so as to select the rank of each customer and show it with other information in the output. use northwind go select Employees.EmployeeID as ID, FirstName+' '+LastName as Name, DENSE_RANK() over (order by SUM(Orders.OrderID)) as [Rank] from employees inner join orders on Employees.EmployeeID...

SQL: Query a List within a List XML

I'm trying to query some XML in SQL Server but am having difficulties: Here is some sample XML: <BaseReport> <Parties> <Party> <SubjectType> <ListItem Name="SubjectType1Name" /> <ListItem Name="SubjectType2Name" /> </SubjectType> </Party> <Party> <SubjectType> <ListItem Name="SubjectType...

Oracle left outer join: howto limit requests in right table

I have a large statement: SELECT a.user_id, a.user_name, s.name, s.value, d.default FROM accounts a, settings s LEFT OUTER JOIN default d ON ( d.name = s.name ) WHERE s.user_id = a.user_id; The problem is that settings contains a large amount of entries and I need to pick the one with the highest ID. I can ...

SQL/JDBC : select query on variable tablenames

Hi all, I'm using Oracle DB and I would like to write a SQL query that I could then call with JDBC. I'm not very familiar with SQL so if someone can help me, that could be great ! Here is the problem. I have a table MY_TABLE wich contains a list of another tables, and I would like to keep only the nonempty tables and those that their na...

Select max date row from results

Using Pervasive SQL, I have a result set: Tp_No Name State Eff_Date Actual Billed 1006 ABC TN 2006-07-01 .1 .5 1006 ABC TN 2008-02-15 .27 .6 1006 ABC TN 2010-09-01 .37 .7 1022 Widget TN 2006-07-01 ....

building a hierarchy from 3 tables

I need to return a parent child relationship from 3 tables which are for bottom, mid and top level respectively. Easy stuff so far, and done already: SELECT -1 ParentID, ID + 100000 ID, txtName Value from tblLevel1 UNION SELECT Level1ID + 100000 ParentID, ID + 50000 ID, txtName Value from tblLevel2 UNION SELECT Level2ID + 50000 ParentI...

what are the different format options for importing a file in MSAccess

Hello, I am using MSAccess and am using this code [Text;HDR=''No'';FMT=''Delimited'';Database='+ExtractFilePath(Location)+'].'+ ExtractFileName(Location); What are the different options I can use for FMT? I have seen: Fixed Delimited TabDelimited ...

How to get my dropdown to default to first row name in table

Edit: more code Dim oControl As New Forms.Control() Using types As New DataSet With oDal .Parameters.Clear() .Execute(sql, types) End With Wi...

SQL: Get a random entry iff condition is false.

Using Firebird: I want to select a random entry in the table if the first SQL query returns 0 rows. Is there anyway to combine these two queries? SELECT * FROM table WHERE cond=1; SELECT FIRST 1 * FROM table ORDER BY rand(); Im using ExecuteNativeQuery on the java-side which takes basic SQL statements. Sadly, If-Else statements don'...

Efficient SQL test query that will work across all (or most) databases

Many database connection pooling libraries provide the ability to test their SQL connections for idleness. For example, the JDBC pooling library c3p0 has a property called preferredTestQuery, which gets executed on the connection at configured intervals. Many example queries I've seen are for MySQL and recommend using SELECT 1; as the v...

Running Counter in SQL 2008

Hi all, I'm trying to establish a temp table for a running inventory demand report in SQL 2008. I can get the results that I am looking for until I hit an invoice with multiple lines. Here is the code so far: --Gather Current Order Data DECLARE @b TABLE ([Planned Shipment Date] DATE, [Order] VARCHAR(15), [Line Number] VARCHAR(15), [L...

How to create an array from this result set (nested categories stored in databased with traversal model)?

Based on this question: http://stackoverflow.com/questions/1310649/getting-a-modified-preorder-tree-traversal-model-nested-set-into-a-ul The logic bellow is used to build an ordened list, but how to do the same with an array? I want to build a nested array. // bootstrap loop $result = ''; $currDepth = -1; // -1 to get the outer <ul...

Client want a field like PREFIX20100001

Client wants a field in the mysql DB to be composed of a prefix, the year, and a counter that resets each year. PREFIX 2010 0001 ... PREFIX 2010 0734, then PREFIX 2011 0001. Are there any mysql tricks to make that happen or do I keep track of the largest number used for each year. Any thoughts appreciated. ...