I have table with followings columns.
Emp name,emp id,emp ph no
X,1,99
X,2,10
Y,2,30
Output:
x,1,(99,10)
Based on emp name and order by emp Id form the phone array
Query:
select emp-name,array_agg(emp_phno order by emp_id) from emp
Error:
function array_agg called with an invalid number or type of parameters
What is the problem, and how can I fix it?
First you need to define an array type to hold the results of your aggregation. This would be something like
CREATE TYPE emp_phno_arr AS VARCHAR(10) ARRAY[5];
In the above SQL note the size of the VARCHAR. I set it to 10 because that's the length of a North American phone number. You might want it to be a different length. Also not the size of the array. I set it to 5. This means the aggregation will allow for at most 5 phone numbers per person. If there are more than 5 number the aggregation will fail. But you can change the size of the array as needed.
Next you will want to do the aggregation.
SELECT emp-name, ARRAY_AGG(emp_phno, NEW emp_phno_arr()) FROM emp GROUP BY emp-name;
Note the second argument to the ARRAY_AGG function. It calls the constructor for the new array type you created in the first SQL statement.
Related
I want to detect column data types of any SELECT query in SQLite.
In the C API, there is const char *sqlite3_column_decltype(sqlite3_stmt*,int) for this purpose. But that only works for columns in a real table. Expressions, such as LOWER('ABC'), or columns from queries like PRAGMA foreign_key_list("mytable"), always return null here.
I know there is also typeof(col), but I don't have control over the fired SQL, so I need a way to extract the data type out of the prepared statement.
You're looking for sqlite3_column_type():
The sqlite3_column_type() routine returns the datatype code for the initial data type of the result column. The returned value is one of SQLITE_INTEGER, SQLITE_FLOAT, SQLITE_TEXT, SQLITE_BLOB, or SQLITE_NULL. The return value of sqlite3_column_type() can be used to decide which of the first six interface should be used to extract the column value.
And remember that in sqlite, type is for the most part associated with value, not column - different rows can have different types stored in the same column.
Given we have a simple table like
CREATE TABLE A(
amount INTEGER
);
What is the difference between queries
INSERT INTO A VALUES(4);
and
INSERT INTO A VALUES('12');
As seen in schema, amount is an INTEGER column. The first query operates with just that - an integer, but the second one operates with a string '12'. Yet both queries work just fine, the table gets values 4 and 12, and can select or, say, sum them up correctly as two valid Integers:
SELECT sum(amount) AS "Total" FROM A;
correctly yields 16.
So is there a difference between inserting an integer as (4) and inserting it as ('12') into the INTEGER-type column?
SQLite tries to convert your String into an Integer before inserting the value into your table as described in the manual.
The type affinity of a column is the recommended type for data stored in that column. The important idea here is that the type is recommended, not required. Any column can still store any type of data. It is just that some columns, given the choice, will prefer to use one storage class over another.
Is it possible to convert rowset variables to scalar value for eg.
#maxKnownId =
SELECT MAX(Id) AS maxID
FROM #PrevDayLog;
DECLARE #max int = #maxKnownId;
There is no implicit conversion of a single-cell rowset to a scalar value in U-SQL (yet).
What are you interested in using the value for?
Most of the time you can write your U-SQL expression in a way that you do not need the scalar variable. E.g., if you want to use the value in a condition in another query, you could just use the single value rowset in a join with the other query (and with the right statistics, I am pretty sure that the optimizer would turn it into a broadcast join).
If you feel you cannot easily write the expression without the rowset to a scalar, please let us know via http://aka.ms/adlfeedback by providing your scenario.
Thanks for input, below is the business cases -
We have catalog data coming from source for which we need to generate unique ids. With ROW_NUMBER() OVER() AS Id method we can generate unique id. But while merging new records it changes ids of existing records also and causes issues with relational data
Below is simple solutions
//get max id from existing catalog
#maxId =
SELECT (int)MAX(Id) AS lastId
FROM #ExistingCat;
//because #maxId is not scalar, we will do CROSS JOIN so that maxId is repeated for every record.
//ROW_NUMBER() always starts from 1, we can generate next Id with maxId+ROW_NUMBER()
#newRecordsWithId =
SELECT (int)lastId + (int)ROW_NUMBER() OVER() AS Id,
CatalogItemName
FROM #newRecords CROSS JOIN #maxId;
I have a relatively simple select query which asks for rows by an column value (this is not controlled by me). I pass in a variable argument of id values to be returned. Here's an example:
select * from team where id in (2, 1, 3)
I'm noticing that as the database changes its order over time, my results are changing order as well. Is there a way to make SQLite guarantee results in the same order as the arguments?
If you could have so many IDs that the query becomes unwieldy, use a temporary table to store them:
CREATE TEMPORARY TABLE SearchIDs (
ID,
OrderNr INTEGER PRIMARY KEY
);
(The OrderNr column is autoincrementing so that it automatically gets proper values when you insert values.)
To do the search, you have to fill this table:
INSERT INTO SearchIDs(ID) VALUES (2), (1), (3) ... ;
SELECT Team.*
FROM Team
JOIN SearchIDs USING (ID)
ORDER BY SearchIDs.OrderNr;
DELETE FROM SearchIDs;
Try this!
select * from team order by
case when 2 then 0
when 1 then 1
when 3 then 2
end
I face the following exception when i try to get data from table with the following structure:
ERROR:-528 MEssage: [Informix .NET provider][Informix]Maximum output
rowsize (32767) exceeded.
CREATE TABLE dr66req
(
req_ser SERIAL PRIMARY KEY,
req_desc LVarChar(32739),
);
Ref:
The total number of bytes that this statement selects exceeds the
maximum that can be passed between the database server and the program.
Try following-
1) Make sure that the columns selected are the ones that you intended.
2) Check that you have not named some very wide character column by
mistake, neglected to specify a substring, or specified too long a
substring. If the selection is what you require, rewrite this SELECT
statement into two or more statements, each of which selects only some
of the fields.
3) If it is a join of several tables, you might best select
all desired data INTO TEMP; then select individual columns of the
temporary table.
4)If this is a fetch via a cursor in a program, you
might revise the program as follows.
First, change the cursor to select only the ROWID of the desired row.
Second, augment the FETCH statement with a series of SELECT statements, each of which selects one or a few columns WHERE ROWID = the saved row ID.