How can i query a table using kdb
I created a table using the code following
q)name:`Iain`Nathan`Ryan`Ross
q)number:98 42 126 98
q)table:([] name; number)
This creates a table:
name number
Iain 98
Nathan 42
Ryan 126
Ross 98
How can a query this table to return results of number which is equal to "98"
Or name which is equal to Iain
This is what I had been using
You can do this using a Q-SQL statement
select from table where number=98
The documentation for these this form of querying can be found at https://code.kx.com/q/ref/qsql/
Related
I have 2 billion of records in table in SQL developer and wanted to export the records in csv file but while exporting data I want to sort one column in ascending order. Is there any efficient or quick way to do this?
for ex:
Suppose the table name is TEMP and i want to sort the A_KEY column in ascending order and then export it
/* TEMP
P_ID ADDRESS A_KEY
1 242 Street 4
2 242 Street 5
3 242 Street 3
4 242 Long St 1
Expected Result in csv file:
P_ID, ADDRESS, A_KEY
4, 242 Long St,1
3, 242 Street,3
1, 242 Street, 4
2, 242 Long St,5
I have tried using below query :
insert into temp2select * from TEMP order by A_KEY ASC;
and then export the table from sqldeveloper but is there any efficient or quick way to direct export records without query?
Losing time on creating a new table (TEMP2) won't help because you are using ORDER BY clause during INSERT, but that means nothing. It is ORDER BY in SELECT statement that matters.
Therefore, run
select * from temp order by a_key;
and export result returned by that query.
2 billion rows? It'll take time. What will you do with such a CSV file? That's difficult to work with.
If you're trying to move data into another Oracle database, then consider using Data Pump export and import utilities which are designed for such a purpose.
We are using select query to fetch data by joining tables in Databricks SQL. With the obtained dataset, we also need to create a header record(which contains static information) and a trailer record (containing details which are dependent on the join output).
Example is given as follows -
Let's assume that there are two Databricks SQL tables "class" and "student", both are joined by a common column " student_id" we are using the following query to obtain marks of all students in each class -
Select
a.student_id
, a.student_name
, a.student_age
, b.class
, b.roll_no
, b.marks
from student as a
inner join class as b
On a.student_id = b.student_id
From the join dataset I need to create the following final output -
Header 202205 some_static_text
Student01 Tom 23 01 01 50
Student02 Dick 21 01 02 40
Student03 Harry 22 01 03 30
Trailer some_text 120
where the last field in trailer reacord(120) is the sum of the last field(b.marks) in the SQL join output.
Is it possible to achieve the entire final output with a single SQL query and without using any other tool/script?
To consider here - our team has only SELECT permission on the databricks tables.
Any help is appreciated.
Thanks.
I have a table called Students with a bunch of names and associated Id's. Eg
Id | Name
123 john
122 james
248 jake
I wanted to write a write a select query that would allow me to search for a particular Id.
I've tried
select Id from Students where Id ~ '248'
But it doesn't seem to work. I'm getting really confused by the official PostgreSQL documentation. Is there a certain way to write this query?
Thanks.
I have a table name test
first last
raj kumar
raj patel
there is hundreds of row in this table.
I have another table called class having all the names that are present in the test table and its marks for each subject.
class_id first_name last_name subject marks
1 raj kumar physics 70
1 raj kumar chemistry 70
1 raj patel physics 80
1 raj kumar math 90
1 raj kumar computer 80
1 raj patel chemistry 90
1 raj patel math 100
2 raj kumar physics 70
2 raj kumar chemistry 70
2 raj patel physics 80
2 raj kumar math 90
2 raj kumar computer 80
2 raj patel chemistry 90
2 raj patel math 100
Now i want to know the total marks of each student present in the test table in below format :.
class_id raj.kumar raj.patel
1 310 270
2 n m
So there will be n number of columns in the resulting tbale
for this particular case we can write
select class_id
sum(case when first_name = 'raj' and last_name = 'Kumar' then marks else 0) as "raj.kumar",
sum(case when first_name = 'raj' and last_name = 'patel' then marks else 0)as "raj.patel"
from class
group by class_id
Since number of students is not fixed which is present in the test table. so i don't want to hardcode the first and last name in my query.
I want something like this which could be able to calculate marks for all the students which are present on test table.
select class_id,
for(i = 0; i < test.size; i++)
sum(case when first_name = i.first and last_name = i.last then marks else 0 ) as i.first|| '.' ||i.last,
from class
group class_id
How to write query or stored procedure for this
thanks in advance.
I am using postgresql.
For all students?
What is wrong with:
select class_id, first_name, last_name, sum(marks)
from class
group by class_id, first_name, last_name;
So you want a crosstab. Here the main problem is that the planner needs to know how many columns and what their types are before starting. So the bad news is there is no direct way to do this.
So you have a few options:
look at the tablefunc extension and dynamically generate your query around the crosstab() function
Use xml or json types and return objects or documents rather than records.
Use my query above and a client-side tool like excel to do the crosstab for you.
(edit) You can dynamically generate SQL in a stored procedure, and return a refcursor, but that brings you back to the problems just moving your "client" code generation into a function.
EDIT To be clear your problem is a planner limitation. You cannot have expand a column list into a variable number of columns. So you have to generate the SQL from outside the db. You cannot handle a dynamic number of columns directly in SQL.
Now, I would highly recommend using the crosstab() function from the tablefunc extension because this simplifies conceptually the result quite a bit. If you have to do this manually (please don't) you can write a query generator in pl/perl or pl/pgsql by selecting from the other table, and then use pl/pgsql's EXECUTE into a cursor. You can then return a refcursor. This is not ideal because the client then has to FETCH from the returned refcursor.
So your first point is to decide on where you are going to generate the SQL code that will run. Will this be in a database client? Or in dynamic SQL (with a requirement that the client FETCH rows separately from running the function)?
I have a table (Table1) which has a coloumn by name XMLColoumn a part of the xml is like
<StudentSubjects>
<ValueName>Maths</ValueName>
<ValueName>Science</ValueName>
<ValueName>History</ValueName>
<ValueName>Calculus</ValueName>
</StudentSubjects>
My table is (Table1) something like
StudentNo XMLColoumn(textfile)
112 (above XML)
1445 (same structure as above XML)
I am trying to get output as
StudentNo Subjects
112 Maths
112 Science
112 History
112 Calculus
my findings till now was
SELECT
convert(XML,CAST(XMLCOLOUMN AS nvarchar(max)).value('(//StudentSubjects/ValueName/text())[1]','nvarchar(max)'),StudentNo from Table1
Which returns me only the first row ie maths..how can i get all the <ValueName>?
Please let me know i tried a lot to find...but couldnt.Please help!
You need to use nodes() to shred the XML to rows.
select StudentNo,
N.value('.', 'nvarchar(max)') as Subjects
from Table1
cross apply XMLColumn.nodes('/StudentSubjects/ValueName') as X(N)
SE-Data