As an example below, I am trying to figure out how I can use INTO (I have out parameters defined in a procedure I have made) when I am doing a SELECT statement that involves AS:
SELECT name, COUNT(addresses) AS TotalAddresses INTO outVar1, SUM(salary + tax) AS test INTO outVar2
FROM ...
Unfortunately, the compiler does not like this and well I have tried searching online, but no luck.
Use one into clause:
SELECT COUNT(addresses) AS TotalAddresses, SUM(salary + tax) AS test
INTO outVar1, outVar2
FROM ...
The results are going into variables. There is no need to select NAME if it is not going to a variable.
Related
I'm currently making a call to an SQL database that counts all entries where the cell starts with NOI, but ends with anything else.
I thought using the below would work, but it doesn't seem to, anyone have any ideas? I know the % sign is the wildcard for foxpro, I don't know if this is the same in SQL
SELECT COUNT * FROM DIARY WHERE PTNOTE = 'NOI%'
You have to use LIKE if you want to use the wildcard characters:
SELECT COUNT(*) FROM DIARY WHERE PTNOTE LIKE 'NOI%'
(also added the parantheses around *)
You are missing parentheses:
SELECT COUNT(*)
FROM DIARY
WHERE PTNOTE = 'NOI%';
It is not the case even in Foxpro. You should use parentheses and "like":
SELECT COUNT(*) FROM DIARY WHERE PTNOTE like 'NOI%'
I have a Pentaho CDE project in development and i wanted to display a chart wich depends on several parameters (like month, year, precise date, country, etc). But when i want to "add" another parameter to my query, it doesn't work anymore... So i'm sure i'm doing something wrong but what ? Please take a look for the parameter month for example :
Select_months_query : (this is for my checkbox)
SELECT
"All" AS MONTH(TransactionDate)
UNION
SELECT DISTINCT MONTH(TransactionDate) FROM order ORDER BY MONTH(TransactionDate);
Select_barchart_query : (this is for my chart, don't mind the other tables)
SELECT pginit.Family, SUM(order.AmountEUR) AS SALES
FROM pginit INNER JOIN statg ON pginit.PG = statg.PGInit INNER JOIN order ON statg.StatGroup = order.StatGroup
WHERE (MONTH(order.TransactionDate) IN (${month}) OR "All" IN (${month}) OR ${month} IS NULL) AND
/*/* Apply the same pattern for another parameter (like year for example) *\*\
GROUP BY pginit.Family
ORDER BY SALES;
(Here, ${month} is a parameter in CDE)
Any ideas on how to do it ?
I read something there that said to use CASE clauses... But how ?
http://forums.pentaho.com/showthread.php?136969-Parametrized-SQL-clause-in-CDE&highlight=dynamic
Thank you for your help !
Try simplifying that query until it runs and returns something and work from there.
Here are some things I would look into as possible causes:
I think you need single quotes around ${parameter} expressions if they're strings;
"All" should probably be 'All' (single quotes instead of double quotes);
Avoid multi-line comments. I don't think you can have multi-line comments in CDE SQL queries, although -- for single line comments usually works.
Be careful with multi-valued parameters; they are passed as arrays, which CDA will convert into comma separated lists. Try with a single valued parameter, using = instead of IN.
I have simple query like
SELECT * FROM temp t WHERE t.id IN (:IDs)
When executed, it prompts me (Oracle SQL Developer) for entering value if IDs variable.
When I enter for example 169, everything runs smoothly, but when I try to enter multiple IDs, like 169,170,171, I get error Invalid Number even while putting it into ''.
I'm used to working with MS SQL and MySQL, so this is little confusing to me.
Anyone any suggestions.
The problem is the varying-IN list. In SQL Developer, when you are prompted to enter the value for the bind variable, you are simple passing it as 169,170,171 which it is not considering as a set of values.
What you could do is, have multiple binds -
SELECT * FROM temp t WHERE t.id IN (:ID1, :ID2)
When prompted, enter value for each bind.
UPDATE Alright, if the above solution looks ugly, then I would prefer the below solution -
WITH DATA AS
(SELECT to_number(trim(regexp_substr(:ids, '[^,]+', 1, LEVEL))) ids
FROM dual
CONNECT BY instr(:ids, ',', 1, LEVEL - 1) > 0
)
SELECT * FROM temp t WHERE it.d IN
(SELECT ids FROM data
)
/
If you put them into ", you get error. Oracle doesn't accept ". You should use just numbers without ".
i.e: (169,170,171,...)
You can define a substitution variable as an array like so:
define IDS = (169,170,171);
and then use it like so:
SELECT * FROM temp t WHERE t.id IN &IDS;
I'm using Alpha Anywhere to take a SQL query that my company uses to create a grid.
The query is as follows:
SELECT t.name,cat.description,i.item_num,i.type_of_unit,i.brand,i.pack,i.description2
FROM cim i, tname t, cim cat
WHERE 'P'||i.price_book_code=t.nameid and i.price_book_group=cat.item_num and i.category!=95 and
i.buyer_num!=8 and cat.warehouse_num=0 and i.broken_case != 'Y' and i.item_num not in (select
item_num from proprietary_items)
ORDER BY t.name,cat.description,i.description2, type_of_unit;
In the Where clause, 'P' is concatenated to i.price_book_code to equal t.nameid because all those values have a P at the beginning.
This query works fine in sql-developer, however alpha anywhere will not run it. It claims invalid token at the 'P' level. Apparently this type of concatenation is not compatible to portable SQL. Is there any other way I can concatenate and compare?
Thank you,
Howard
The CONCAT function should work. Here's an example of using it in the WHERE clause:
SELECT *
FROM dual
WHERE CONCAT('Foo','Bar') = 'FooBar';
I need a good expression in order to select correctly parts of a field.
For example, the field can be of the type: "google_organic" or "google_campaign_HereGoesMyCode" . The part I am interested in is "organic" or "campaign" without any other addition.
So far I select with this:
substring(Referer, charIndex('_',Referer)+1, len(Referer))
But in the case of "campaign" I select the whole thing... I don't know how to manage the existence or non-existence of the second underscore...
thank you
One way is to basically create a lastIndex type search using the below SQL and use the result as the length:
len(Referer) – (charindex('_', reverse(Referer))-1)
You can then rewrite your query as follows, although you need the result of the first charIndex so this is fairly intense:
substring(Referer, charIndex('_',Referer)+1, (len(Referer) – (charindex('_', reverse(Referer))-1) - (charIndex('_',Referer)+1))-1 )
I realize that this will now only work if you have 2 underscores. But you can filter which query to run based off a CASE/WHEN statement.