tags:

views:

33

answers:

1

i have a array of strings i want to write a query such that

  1. first it will count the Id from a particular table say products on the basis of first product names that is in the array

  2. if the value is more than one then select the top one id from the list randomly

  3. otherwise if the count comes one or zero then perform the same query with next value from the string array.

can any body suggest a suitable query for this condition

A: 

I assume that the array data are in TMP_ARRAY.

You have two sets products and array, You want to retrieve from products only those entires that are in arrays. For this You should use inner join with proper on clause, next you want to set those results in groups GROUP BY by column name, as you want to operate only on those results that single group size is bigger then one, the clause having needs to be added at the end you want to pick from them 'top' id. And here we have a problem what You mean by 'top'? In data base the data don't have any specific order. I assume that is about the largest, other case in this situation mean only one result, so for both cases we select MAX(ID).

In result I came up with something like this

SELECT MAX(ID) FROM PRODUCTS p INNER JOIN TMP_ARRAY t ON t.NAME = p.NAME GROUP BY p.NAME HAVING COUNT(ID) > 1;

But I'm not sure that i have had understand everything correct.

Vash