sql

Redundancy in doing sum()

table1 -> id, time_stamp, value This table consists of 10 id's. Each id would be having a value for each hour in a day. So for 1 day, there would be 240 records in this table. table2 -> id Table2 consists of a dynamically changing subset of id's present in table1. At a particular instance, the intention is to get sum(value) from tab...

SQL query to get lowest 2 values of a counted query selection (using db2)?

Hi, Imagine I already have a query that returns the following: Col1 | Col2 ------------ A | 2 B | 3 C | 3 D | 4 E | 8 ... Say I used something like this: select Col1, count ( * ) as Col2 \ from ... where ... order by Col2 \ group by Col1 \ So now, all I want to select are (Col1, Col2) such that it r...

Anyone know a program to automatically dump a bunch of test (dummy) data into a sql database?

Possible Duplicate: Tools for Generating Mock Data? I am running SQL Server 2005 and I want to dump some dummy data into a large table with about 50 columns (I did not design it :P) - anyone know a tool to do this automatically? I want the rows to have all sorts of different data, and I would rather not write a script if ther...

How to group by having the same id?

Hello, I want the customerid who bought product X and Y and Z, from the following schema: Sales(customerid, productName, rid); I could do the intersection: select customerid from sales where productName='X' INTERSECT select customerid from sales where productName='X' INTERSTECT select customerid from sales where productName='Z' ...

Expand my knowledge in DB programming

Hi there, my name is Tal, Im working on a PHP Application the should have lots and lots of records, what DB should I use, and are there guides on the web that explains how to build efficient dbs and tables? Tnx! ...

Flatten date range memberships retaining only the highest priority membership (TRANSACT-SQL)

Problem statement: A table contains an item_id, a category_id and a date range (begin_date and end_date). No item may be in more than one category on any given date (in general; during daily rebuilding it can be in an invalid state temporarily). By default, all items are added (and re-added if removed) to a category (derived from outsi...

SOAPUI & Groovy Scripts, executing multiple SQL statements in one go

Hi, I've got some soapUI tests, that use groovy scripts to first insert some data into a table Previously, I've been using the following snippet of code to do this : def conn = context.dbConnEtopup conn.execute( "INSERT INTO A(ID, NAME) VALUES (1, "Johnny")" ) This works fine, however I have many test scripts that now d...

SQL to get distinct statistics

Hi, Suppose I have data in table X: id assign team ---------------------- 1 hunkim A 1 ygg A 2 hun B 2 gw B 2 david B 3 haha A I want to know how many assigns for each id. I can get using: select id, count(distinct assign) from X group by id orde...

Implementing a 'many-to-many' database

Greetings, stack*overflow* In my database, I already have one table, 'contacts' that contains records of individual people. I also have several other tables in my database which represent "skill sets" that contain records denoting a particular skill. 1) Am I correct in plotting this as a "many-to-many" relationship? (each contact can h...

Most optimal order (of joins) for left join

I have 3 tables Table1 (with 1020690 records), Table2(with 289425 records), Table 3(with 83692 records).I have something like this SELECT * FROM Table1 T1 /* OK fine select * is bad when not all columns are needed, this is just an example*/ LEFT JOIN Table2 T2 ON T1.id=T2.id LEFT JOIN Table3 T3 ON T1.id=T3.id and a query like this S...

SQL query for selecting the firsts in a series by column

I'm having some trouble coming up with a query for what I am trying to do. I've got a table we'll call 'Movements' with the following columns: RecID(Key), Element(f-key), Time(datetime), Room(int) The table is holding a history of Movements for the Elements. One record contains the element the record is for, the time of the recorded l...

Invalid SQL Query

I have the next query that in my opinion is a valid one, but I keep getting error telling me that there is a proble on "WHERE em.p4 = ue.p3" - Unknown column 'ue.p3' in 'where clause'. This is the query: SELECT DISTINCT ue.p3 FROM table1 AS ue INNER JOIN table2 AS e ON ue.p3 = e.p3 WHERE EXISTS( SELE...

PHP sql with foreach loop variable problem

This is really getting frustrating. I have a text file that I'm reading for a list of part numbers that goes into an array. I'm using the following foreach function to search a database for matching numbers. $file = file('parts_array.txt'); foreach ($file as $newPart) { $sql = "SELECT products_sku FROM products WHERE products_sku='...

how to implement multivalued attribute in oracle ?

I want to store more than one Email IDs in the Email id column of a table, as a multivalued attribute. How can I do this in oracle? ...

oracle replace comma with period and period with comma

How to change comma with period and period with comma e.g. 1,50,000.25 to 1.50.000,25 in oracle ...

Selecting all but one field?

instead of SELECT * FROM mytable, i would like to select all fields EXCEPT one (namely, the 'serialized' field, which stores a serialized object). this is because i think that losing that field will speed up my query by a lot. however, i have so many fields and am quite the lazy guy. is there a way to say... `SELECT ALL_ROWS_EXCEPT(seri...

Merge computed data from two tables back into one of them

I have the following situation (as a reduced example). Two tables, Measures1 and Measures2, each of which store an ID, a Weight in grams, and optionally a Volume in fluid onces. (In reality, Measures1 has a good deal of other data that is irrelevant here) Contents of Measures1: +----+----------+--------+ | ID | Weight | Volume | +---...

sql combine two subqueries

I have two tables. Table A has an id column. Table B has an Aid column and a type column. Example data: A: id -- 1 2 B: Aid | type ----+----- 1 | 1 1 | 1 1 | 3 1 | 1 1 | 4 1 | 5 1 | 4 2 | 2 2 | 4 2 | 3 I want to get all the IDs from table A where there is a c...

How would I duplicate the Rank function in a Sql Server Compact Edition SELECT statement?

It doesn't look like SQL Server Compact Edition supports the RANK() function. (See Functions (SQL Server Compact Edition) at http://msdn.microsoft.com/en-us/library/ms174077(SQL.90).aspx). How would I duplicate the RANK() function in a SQL Server Compact Edition SELECT statement. (Please use Northwind.sdf for any sample select stateme...

how to return some default value in oracle cursor when nothing is found

I have following statement in one of the oracle SP: OPEN CUR_NUM FOR SELECT RTRIM(LTRIM(num)) as num FROM USER WHERE USER_ID = v_user_id; When the above does not return anything the SP result just has no column name in it. But I'd like the cursor to still have num column with some default value like NOTHING when no data is fo...