sql

How to get a "top X with the rest" in SQL ?

Say I have a table with a 'name' and 'quantity' columns, and I want to get the first X products which are the most present, and another line, which is the sum of all the other products. We can take this sample SQL script as an example: CREATE TEMPORARY TABLE product ( name TEXT, quantity INT ); INSERT INTO product (name, quant...

SQL replace all instances of word in a field

Any way to do this? Replace function only replaces first instance. Thanks. ...

SQL - Make this statement faster i.e. less duration, reads

Given the following tables: Orders (OrderID, OrderStatus, OrderNumber) OrderItems(OrderItemID, OrderID, ItemID, OrderItemStatus) Orders: 2537 records Order Items: 1319 records I have created indexes on Orders(OrderStatus) OrderItems(OrderID) OrderItems(OrderItemStatus) I have the following SQL statement (generated by LinqToSql) ...

How to read/write or copy/paste text file locally using Stored procedure?

Hi My Stored Procedure is using Bulk Insert Task. As per requirement of Bulk Insert, the text file should be on same server where database is. Now file is residing on another machine. But database server can access file using shared network drive. Now question is How my stored procedure can read or copy the file from network drive an...

DataSet to SQL mapping framework?

Hi, I'm developing a system that needs to accept sql queries dynamically. I'm looking for something where someone can define a select query in a file external to the system -- the system would execute the query and return the results as a DataSet. Then the system would read a mapping file (also defined external to the system) that woul...

How can I create a user in SQL Server Express database I added to my project?

How can I create a SQL user in a SQL Server Express database that I added to my project? I need to create a user to use in a connection string that doesn't use Integrated Security. ...

hibernate : howto order by custom (user-defined) function in PostgreSQL 8.4

We have a Family Hibernate Entity. This entity has(among others) 4 booleans properties. When retrieving the Families from the Postgres 8.4 DB, it is required that the List of families be ordered by sum of the boolean properties. There are 2 other fields in the order by criterion. Eg select fam.a, fam.b, fam.c, fam.d, fam.e from family...

[SYBASE] SQL query for "concatenate on join"

NOTE: I'm still looking for an answer that I can accept. Hi, I'm using a Sybase ASE database. I have two tables that look like: Table Shops: --------------------- | ShopName | ShopID | --------------------- | Sweetie | 1 | | Candie | 2 | | Sugarie | 3 | --------------------- Table Sweets: --------------------...

SQL Statement on date and time while the data is being stored as string

HI, I have stored data & time as varchar type and data looks like this "9/16/2010 2:59:10 PM". Now I want to retrieve data from between two dates such as From 01/01/2010 to 31/10/2010, When I run this SQL Statement: SELECT username, timein, timeout FROM user_log WHERE (timein BETWEEN '01/01/2010' AND '30/11/2010') It r...

Deploying an application containing SQL Server database. Which authentication method?

I'm just a beginner in SQL Server database development and was wondering which authentication method to use. My database needs to have the simplest protection there is. If I choose Windows authentication, what username and password do I pass when connecting to the database? What will happen when another user installs the application on ...

Sql single index or multiple index

Let's say I have the Products table. On my UI, I allow the user to search by name, description, code The user can only search on criteria. Should I create an index for each criteria : name, description, code or create ONE single index for all 3? What will make you choose one versus the other? ...

How can you write this query?

I have two tables. The structure of tables is as follows. TRAILERS_INVENTORY TRAILER_TYPE VARCHAR2(100) TRAILER_LENGTH INT TRAILER_WIDTH INT YEAR_OF_MANUFACTURE INT NEW_INVENTORY NEW_INVENTORY_TYPE_ID INT, TRAILER_TYPE VARCHAR2(100), TRAILER_LENGTH INT, TRAILER_WIDTH INT, YEAR_OF_MANUFACTURE INT I want to list all of the trailer...

How would I write this SQL query?

I have the following tables: PERSON_T DISEASE_T DRUG_T ========= ========== ======== PERSON_ID DISEASE_ID DRUG_ID GENDER PERSON_ID PERSON_ID NAME DISEASE_START_DATE DRUG_START_DATE DISEA...

Is a variable declaration in grails the same as a belongsTo relationship?

Hello all, I am trying to set up a few domain classes. I will explain it in english, and I am wondering how the domain would be set up in grails. Capitalized words are my domains An Employee has an Education. An Employee has many Employer (past and present). An Employee had one or many Project for each Employer. Project have a Role, Cl...

How do I stop AutoMapper from making two identical SELECT statements with NHibernate?

I have an entity called Incident and a DTO called IncidentDTO. For now, IncidentDTO simply looks like this: public class IncidentDTO : Incident { // empty until I can get AutoMapper working correctly } I'm trying to pull a list of all the Incidents in the database and convert them to DTOs using this code: Mapper.CreateMap<Inciden...

How to format a SQL query into a more human-readable format?

I'm looking for a program that can take in an SQL string, (my environment is MySQL, but I could look at similar tools for other RDBMSs) parse it and render it again in a format that is more human-readable. I've seen some online ones, but I'm hoping for one that I can integrate into some internal logging tools. For example, taking: SELE...

Javascript Object -> SQL, in Javascript

I have a simple client-side Sqlite database (via. Google Gears) I want to use for persistence of the contents of a javascript object's properties (not methods). I don't really care about normalization since it has lots of potentially disparate fields (primitive types, arrays, objects) that would thwart normalization anyway. What would ...

What makes this database's cascading deletes cyclical?

Vendor (PK) - Id Name Stand (PK) - Id VendorId - fk - cascaded delete if Vendor is deleted) Name Item (PK) - Id VendorId - (fk - cascaded delete if Vendor is deleted) Name Price UnavailableItem (PK) - ItemId - (fk - cascaded delete if Item is deleted) (PK) - StandId - (fk - cascaded delete if Stand is deleted) The database above repr...

Return 1 result per left join

Currently I am performing a left join on two tables. The first table has an id and a persons name, the second table has an id, the id of a person from table 1, and then a timestamp (of the last flight they had). People Flights id | name id | person_id | time ------------ -----...

Update statement fails because I have a column named order

I am trying to run an update statement as follows... UPDATE tblDeductionSystem SET [ORDER] = [0RDER] + 6 WHERE [ORDER] >= 7 AND ScoringCriteriaTypeID = @CheerDeductionScoreSheetID Sql Server if giving me invalid column name ORDER. I thought if I delimited the reserved word with the square brackets this would work. ...