I am trying to find whether a set of numbers are contained in another set of numbers.
ID NumberSet Result
-- --------- ------
1 1457 5741
2 4187 7148
3 6324 1345
So for this dataset I would return ID 1 & 2. All the numbers from the NumberSet must be contained within the Result.
Any suggestions?
This actually isn't that hard. Just look for the reverse . . . is there a case where a number from NumberSet is not in Result?
For the first row, you could manually create a like expression for finding a result that has a character other than "1457":
where Result like '%[^1457]%'
What you want is:
where Result not like '%[^1457]%'
Now, let's generalize:
where Result not like '%[^'+NumberSet+']%'
It seems not easy but you can look into this blog http://wikiprogrammer.wordpress.com/2011/10/17/find-out-anagram-using-sql/
Related
I'm looking to select the primary key of a row and I've only got a column that contains info (in a substring) that I need to select the row.
E.g. MyTable
ID | Label
------------
11 | 1593:#:#:RE: test
12 | 1239#:#:#some more random text
13 | 12415#:#:#some more random text about the weather
14 | 369#:#:#some more random text about the StackOverflow
The label column has always a delimiter of :#:#:
So really I guess, I'd need to be able to split this row by the delimiter, grab the first part of the label column (i.e. the number I'm looking) to get the id I wanted.
So, If I wanted row with ID of 14, then I'd be:
Select ID from MyTable
where *something* = '369'
Any ideas on how to construct something ..or how best to go about this:)
I'm completely stumped and haven't been able to find how to do this.
Thanks,
How about:
WHERE label LIKE '369#%'?
No reason to get fancy.
Although.. if you are going to do this search often, then maybe pre-split that value out to another column as part of your ETL process and index it.
I use SAP-HANA database. I have a simple 2 column table whose columns are number, name, noodles, fish . The rows are these:
number name noodles fish
1 tom x
1 tom x
1 jack
2 jack x
I would like to group the rows by the id, and concatenate the names into a field, and thus obtain this:
number name noodles fish
1 tom x x
2 jack x
Can you please tell me how we can perform this operation in sap-hana? Thanks in advance.
Well, you did not really concatenate the names, but instead kept the same ones (if you would have concatenated the names as well, you would get something like jackjack in your result). I guess your x's indicate some sort of ABAP-style flags.
In any case, you would do this with grouping. This is a completely non-HANA thing (you can use the same basic SQL for any DB). You can group against several columns. All other columns that you want to select must be used in an aggregated expression (e.g. a SUM, MAX, COUNT, etc.).
To get the output from your question, I wrote the following code:
SELECT "ID", "NAME", MAX("FISH"), MAX("NOODLES")
FROM #TEST GROUP BY "ID", "NAME";
And got the same output as you. I used the MAX function based on the following assumption: you would want to get X if there is any X in the "concatenated" (aggregated) rows in that column. You get nothing / space if all the "concatenated" rows have space in them.
I have a table. In that table there is a column named CIS. This CIS column contains a unique number in each row. I want to get 50 rows from a specific row captured via CIS number.
For example lets say i have the following table
CIS MODEL
--- -----
123 1
212 2
213 3
325 4
452 3
. .
. .
. .
841 4
And i just have a CIS number nothing else more. Let say my CIS number is 212. I want to get next 50 rows from the row with the CIS number 212. How can i do that?
Does this do what you want?
select top 50 t.*
from table t
where cis > 212
order by cis;
It assumes that "next" means the next rows ordered by the CIS number.
Based on your question and comment, it sounds like you want to get the next 50 rows based on the order those rows were inserted in the table. As others have suggested, SQL Server does not have a method for retrieving by insert order. A SELECT query with no ORDER BY does not retrieve data in any particular order, even though it might seem to. If you are interested in why that is, you may want to review the post at https://stackoverflow.com/a/10064571/4656137
Since you want to know what is next and there is no concept of insert order, SQL needs to know what order to evaluate the rows to provide what you are looking for. One option might be if you have an auto-incrementing key column on that table, you could try to order by that (using Gordan's example).
Note: I would have simply commented on Gordon's response, but I don't have enough reputation to comment on others answers yet.
Hopefully this helps some.
TestTable
inputsCOLUMN
3-300-150-150-R
3-200-100-100-A
5-500-00-500-A
output
3_open 3_spent 3_closing 3_type 5_open 5_spent 5_closing 5_type
-------- --------- ----------- -------- -------- --------- ----------- --------
300 150 150 R 500 00 500 A
200 100 100 A
Above is the input table called TestTable. It has two columns that contains rows of data(strings)
And there is a desired output table of which the column names are based on the input string.
the column name is the first number on the string + another string name, like CONCAT(split(inputsCOLUMN,'\\-')[0],'-','type')
so that output is the desired output. and the below query is not working as desired because of that part when i am trying to concatenate an alias i think is not allowed. so help me if there is a way i can find that desired output.
SELECT split(inputsCOLUMN,'\\-')[1] as CONCAT(split(inputsCOLUMN,'\\-')[0],'-','open'),
split(inputsCOLUMN,'\\-')[2] as CONCAT(split(inputsCOLUMN,'\\-')[0],'-','spent'),
split(inputsCOLUMN,'\\-')[3] as CONCAT(split(inputsCOLUMN,'\\-')[0],'-','closing'),
split(inputsCOLUMN,'\\-')[4] as CONCAT(split(inputsCOLUMN,'\\-')[0],'-','type')
Hive cannot have a dynamic number of columns, and it cannot have dynamic column names. It must be able to determine the entire schema (column count, types, and names) at query planning time, without looking at any data.
It's also not clear to me how exactly you're matching up input records into a single row. For example, how do you know which "3" record corresponds to which "5" record.
If you knew that, for example, there would always be a "3" record and a "5" record and you could commit to those being the only column names, and if you had a consistent way of matching up records to "flatten" this data, then it is possible, but difficult. I've done almost this exact operation before, and it involved a custom UDTF and a custom UDAF, and some code to auto-generate the actual query, which ended up being hundreds of lines long in some cases. I would re-evaluate why you want to do this in the first place and see if you can come up with another approach.
I apologize if this question was asked before I just couldn't correctly formalize it. I have a code column in a table and want to query it but remove some elements with some particular code. Say I want to take elements with code starting from 4 but not include the elements with code whose 6-th number is 9 (1121290).
The code column contains string of numbers with max-length of 8 char. and I want to take almost everything that starts with 4 except elements that start with 411, 427 and 428
Yes you can give query like this:--
I have column element with 6 digit codes.
I am fetching element whose 1st digit is 4 or 6th is 9.
we have to use % and _ for fetching..
SELECT element FROM "table" WHERE element LIKE '%4____9%';
try this it will work.
Everything that starts with 4 except elements that start with 411, 427 and 428 :-
SELECT element FROM "table" WHERE element LIKE '%4__';
1st digit is 4 and 6th is not 9: I tested this one it is working fine try this :-
SELECT element
FROM "table"
WHERE element NOT LIKE '%_____9' and element LIKE '%4_____'
It might be easiest to simply spell out each condition in the where clause:
WHERE code like '4____9%' AND code NOT LIKE '411%' AND code NOT LIKE '427%' AND code NOT LIKE '428%'
The extra conditions won't hurt the query's efficiency much; it's going to have to scan every single row starting with 4 anyway.
Assuming that your "CODE" field is varchar and you are using SQL Server,
you can use the below Query
select * from yourTable
where code like '4%'
AND CHARINDEX('9',code)<>6
Here is a more compact version.
`SELECT * FROM table_name WHERE code IN ('4%') and code NOT IN ('411%','427%', '428%','_____9%');`