query

SQL query Parent-child distinct

I have a pair of SQL server tables. P contains id and name. PR contains id, interestrate, tiernumber, fromdate, todate and P.id. PR may contain many rows listed per p.id / tier. (tiers are a list of rates a product may have in any given date period.) eg: Product 1 tier 1 starts 1/1/2008 to 1/1/2009 and has 6 rates shown 1 row per rate...

How do I search an ActiveX/COM object for a method?

I have an ActiveX/COM DLL. It contains many methods and properties. I would like to be able to ask it if it has a particular symbol, as per the following snippet: If HasMethod( "StdLib.DLL", "ReadFileE" ) Then ... End If Is there a way to do this from, say, VBScript or JScript? If not, where do I go to get the information I need? ...

Does NHibernate Criteria API support projections on collection properties?

I need to replicate the following working HQL query using criteria API. session.CreateQuery( "select c " + "from Parent p " + "inner join p.Children c " + "where p.Id = 9 " + "and c.Id = 33") .SetMaxResults(3) .List(); The query selects all the children that satisfy a certain criteria that belong to parents...

How do I make multiple database queries more efficient in Perl?

Hi all, I have a queries that reside in multiple methods each (query) of which can contain multiple paramaters. I am trying to reduce file size and linecount to make it more maintainable. Below is such an occurence: $sql_update = qq { UPDATE database.table SET column = 'UPDATE!' WHERE id = ? ...

How to use the System.LINQ.Dynamic assembly w/ IEnumerable(Of T)

I'm trying to take the final solution from Phil Haack here and sort using his killer LINQ query but instead of using data context like he is, I want to query against IEnumerable(Of T) ... not having any luck. Public Function DynamicGridData(ByVal sidx As String, ByVal sord As String, ByVal page As Integer, ByVal rows As Integer) As Acti...

How to do this join query in Django

In Django, I have two models: class Product(models.Model): name = models.CharField(max_length = 50) categories = models.ManyToManyField(Category) class ProductRank(models.Model): product = models.ForeignKey(Product) rank = models.IntegerField(default = 0) I put the rank into a separate table because every view of a pa...

How to using select with multi table in SubSonic query ?

Hi, I am new with SubSonic and have a problem with query. This is my query string string sql = "SELECT *" + " FROM tbl_exrates, tbl_currency" + " WHERE date = " + d; " AND tbl_exrates.currency = tbl_currency.cid" + " AND (cash > 0 OR transfer > 0 OR sell > 0)"; How to convert it to...

More efficient of the two queries?

I have a table with columns user_id, email, default. Default stores 'Y' or 'N' depending if the email is the users default email. Each user can have only one default email. When a user is doing an update or insert on the table, in my SP I check if the user has passed isDefault as 'Y'. If so, I need to update all the entries for that us...

Querying for Consecutive Rows with Certain Characteristics

I've got a table with the following columns: id int(10) user int(10) winner int(10) profit double created datetime The winner column can be either 0 or 1. I'd like to create a query that returns the maximum number of consecutive winners as ordered by the created datetime column along with the first and last created date as well as th...

SQL Help With Query - Group By and Aggreate Functions

Hi guys, I have a query I need some help with, have been checking out a fair few tutorials but nohting I found covers this issue. I have three joined tables, Products,ProductImagesLookUp and Images. A product can have any number of Images and the Order of the Images for a Product are stored in ProductImagesLookUp. I need to return...

Storing and Querying GPS Coordinates Effectively

I want to create a large database of GPS coordinates that can be queried by saying "Return all coordinates that are within 'n' metres of [this coordinate]". I need it to be as efficient as possible so looping through all the coordinates in the database and calculating whether a coordinate is within 'n' metres wouldn't be a desired solut...

Displaying zero-valued data in an SQL Query?

I have the following two tables (simplified for this question): CREATE TABLE team ( teamID CHAR(6) NOT NULL PRIMARY KEY); CREATE TABLE member ( memberID CHAR(7) NOT NULL PRIMARY KEY, teamID CHAR(6) NOT NULL REFERENCES team(teamID) ); I also have the following query, which is to list the number of members in each team: SELECT tea...

Has anyone built a dynamic query engine using table defined columns?

I have a table called 'Fields' that contains application fields. In this table I store metadata for each fields (TableName, ColumnName, JoinType (inner/outer). I have something working but it's not as clean as I'd like. Has anyone tackled this before? I'm looking for some ideas around better practices. ...

How do you select latest entry per column1 and per column2?

I'm fairly new to mysql and need a query I just can't figure out. Given a table like so: emp cat date amt cum 44 e1 2009-01-01 1 1 44 e2 2009-01-02 2 2 44 e1 2009-01-03 3 4 44 e1 2009-01-07 5 9 44 e7 2009-01-04 5 5 44 e2 2009-01-04 3 5 44 e7 2009-01-05 1 6 55 e...

SQL ORDER BY the 'IN' value list

I have a simple SQL query (in PostgreSQL 8.3) that grabs a bunch of comments. I've composed a bunch of ids before-hand and that gets fed into the WHERE IN clause like so... SELECT * FROM "comments" WHERE ("comments"."id" IN (1,3,2,4)) This returns the comments in a natural order which in my case is the ids like 1,2,3,4. What I'm wan...

Combining multiple SQL Queries

I want to make a query to list cats that took longer than average cats to sell? I have five tables: Animal, Sale, AnimalOrderItem, AnimalOrder, and SaleAnimal Animal table: AnimalID, Name, Category (cat, dog, fish) SaleAnimal table: SaleID, AnimalID, SalePrice Sale table: SaleID, date, employeeID, CustomerID Ani...

Assistance on a SQL Query - The multi-part identifier could not be bound

I'm trying to do a sub-select and Inner Join in the same query, with little success. The query, as it stands is below, and returning the error The multi-part identifier "Company.LocalityId" could not be bound. UPDATE Company SET Company.BusinessRegionId = b.Id FROM ( SELECT Id FROM BusinessRegio...

MySQL on duplicate key update

If I have query like this, how can I refer to values I have already given in update statement, so that I don't need to insert same data to query again? Example I would like to update col1 value with 'xxx', but now I need to enter 'xxx' again in duplicate statement. Is there anyway to refer those values in duplicate statement? INSERT INT...

Finding the 'Most Improved' in linq

The following code is what i used to identify the Top 10 earning items var top = (from m in db.Stats where m.Item.AccountID == AccountID && m.DateTime >= month && m.DateTime < month.AddMonths(1) group m by m.Item into g orderby g.Sum(p => p.Earnings) descending select g.K...

Can SQL Sub-query return two/more values but still compare against one of them?

I have this query: SELECT Items.Name, tblBooks.AuthorLastName, tblBooks.AuthorFirstName FROM Items WHERE Items.ProductCode IN ( SELECT TOP 10 Recommended.ProductCode FROM Recommended INNER JOIN Stock ON Recomended.ProductCode = Stock.ProductCode AND Stock.StatusCode = 1 WHERE (Recommended.Type = 'TOPICAL') ORDER BY CHECKSUM(NEWID()));...