I have a select statement that obviously pulls some results from my database. Once I get those results, I would like to access them one at a time for another Select statement, is that possible? I am doing this in a user defined function.
Example:
SELECT differentKindsOfBread FROM breadDrawer
WHERE differentKindsOfBread IS NOT Null
Now I want to use my results to do another Select like:
SELECT differentKindsOfBread[1] FROM differentKindsOfBread
WHERE blah = blahblah
EDIT
Lets say differentKindsOfBread selected:
Rye
White
Wheat
half-Wheat
Now I want to do selects for each one like:
Select Rye (differntKindsOfBread[0]) ...
Select White (differntKindsOfBread[1]) ...
Thanks,
Related
So I have a task to create a report that displays some data, for which I have a working query. The next challenge is that every time I run the query, it should also print certain terms with the report. The terms are in the form of the table itself but I have restricted access to the database. I cannot update the table with new data.
What I want to achieve here is:
|-------------|
|Report data |
|-------------|
|Fixed Set of terms|
|------------------|
I am looking for a way to append some text at the end of the table. Not sure how can I do that, hoping for some help.
SELECT
x, y, z
FROM
foo
UNION ALL
SELECT
'SOMETHING', 1, 5
UNION ALL
SELECT
'ANYTHING', 2, 7
ORDER BY
x, y, z
The data types being unioned should match, don't try to union integers with strings, for example.
The ORDER BY is applied after the UNIONs.
UNION normally searches for and removes duplicates. Specifying UNION ALL avoids that costly overhead.
I am pulling data and when I pull in the text field my results for the "distinct ID" are sometimes being duplicated when there are multiple results for that ID. Is there a way to concatenate the results into a single column/row rather than having them duplicated?
It looks like there are ways in other SQL platforms but I have not been able to find something that works in HANA.
Example
Select
Distinct ID
From Table1
If I pull only Distinct ID I get the following:
ID
1
2
3
4
However when I pull the following:
Example
Select
Distinct ID,Text
From Table1
I get something like
ID
Text
1
Dog
2
Cat
2
Dog
3
Fish
4
Bird
4
Horse
I am trying to Concat the Text field when there is more than 1 row for each ID.
What I need the results to be (Having a "break" between results so that they are on separate lines would be even better but at least a "," would work):
ID
Text
1
Dog
2
Cat,Dog
3
Fish
4
Bird,Horse
I see Kiran has just referred to another valid answer in the comment, but in your example this would work.
SELECT ID, STRING_AGG(Text, ',')
FROM TABLE1
GROUP BY ID;
You can replace the ',' with other characters, maybe a '\n' for a line break
I would caution against the approach to concatenate rows in this way, unless you know your data well. There is no effective limit to the rows and length of the string that you will generate, but HANA will have a limit on string length, so consider that.
I want to run a query that fetches me all the names that have for say "ana" in it but i want the result like this
Anna
Brianna
not like
Brianna
Anna
which means the name starting with "an" should come first and then anything containing "an" in them
My SQL query is like this but this dpes not give me the desired results. I checked some other stuff but not quite sure how to use and does not give the desired results.
SELECT *
FROM TABLE_NAME
WHERE Name LIKE 'ana%'
AND Name LIKE '%ana%'
You need to select the records only once and then order them and according to your requirements i understand that you need to order something like this by using ORDER BY CASE
SELECT *
FROM TABLE_NAME
WHERE Name LIKE '%ana%'
ORDER BY CASE
WHEN Name LIKE 'an%' THEN 1
WHEN Name LIKE '%an%' THEN 2
ELSE 3
END
I have a table which contains geometry lines (ways).There are lines that have a unique geometry (not repeating) and lines which have the same geometry (2,3,4 and more). I want to list only unique ones. If there are, for example, 2 lines with the same geometry I want to drop them. I tried DISTINCT but it also shows the first result from duplicated lines. I only want to see the unique ones.
I tried window function but I get similar result (I get a counter on first line from the duplicating ones). Sorry for a newbie question but I'm learning :) Thanks!
Example:
way|
1 |
1 |
2 |
3 |
3 |
4 |
Result should be:
way|
2 |
4 |
That actually worked. Thanks a lot. I also have other tags in this table for every way (name, ref and few other tags) When I add them to the query I loose the segregation.
select count(way), way, name
from planet_osm_line
group by way, name
having count(way) = 1;
Without "name" in the query I get all unique values listed but I want to keep "name" for every line. With this example I stilll get all the lines in the table listed.
To expound on #Nithila answer:
select count(way), way
from your_table
group by way
having count(way) = 1;
You first calculate the rows you want, and then search for the rest of the fields. So the aggregation doesnt cause you problems.
WITH singleRow as (
select count(way), way
from planet_osm_line
group by way
having count(way) = 1
)
SELECT P.*
FROM planet_osm_line P
JOIN singleRow S
ON P.way = S.way
you can group by way and while taking the data out check the count=1.It will give non duplicating data.
#voyteck
As I understood your question you need to get only non duplicating records of way column and for each row you need to show the name is it
If so, you have to put all the column in select statement, but no need to group by all the columns.
select count(way), way, name
from planet_osm_line
group by way
having count(way) = 1;
I have a Postgres 9.0 query returning results in a way similar to this:
item;qty
AAAA;2
EEEE;3
What I would like is to transform that into:
AAAA
AAAA
EEEE
EEEE
EEEE
Is there any way I can do that on simple, i.e., without stored procedures, functions, etc?
There's a function 'generate_series' which can be used to generate a table of values. These can be used to repeat a column via joining:
select item
from data,generate_series(0,1000)
where generate_series<qty order by item;
Consider the following demo:
CREATE TEMP TABLE x(item text, qty int);
INSERT INTO x VALUES
('AAAA',2)
,('EEEE',3)
,('IIII',4);
SELECT regexp_split_to_table(rtrim(repeat(item||'~#~',qty),'~#~'),'~#~') AS item
FROM x;
Produces exactly the requested result.
In my tests it performs faster by an order of magnitude than the solution with generate_series().
Additional bonus: works with any number of qty.
Weakness: you need a delimiter-string not contained in any item.
SELECT
myTable.item
FROM
myTable
INNER JOIN
(SELECT 1 AS counter UNION ALL SELECT 2 UNION ALL SELECT 3) AS multiplier
ON multiplier.counter <= myTable.qty
Increase the number of UNIONS based on your Maximum value in qty
But I'd also follow #djacobson's advice : explain why you want to do this, as the may be a completely different approach altogether. Doing this feels, ummm, odd...