Getting part of query defined by a parameterized function - sql

Hello all :) I'm try to do something like this in Oracle 10g:
SELECT
CAR_ID,
CAR_DATE,
get_some_other_info(CAR_TYPE)
FROM CARS
Where get_some_other_info(CAR_ID) would return several columns:
| CAR_ID | CAR_DATE | CAR_COLOR | CAR_CO2
| 001 | 01/01/2013 | BLUE | 100
| 002 | 02/01/2013 | RED | 120
| 003 | 03/01/2013 | BLUE | 100
I need to use a function for implementation reasons. I feel that I could use Table functions, but I cannot wrap my head around how to use them for my case.
Best regards,

You can also use dynamic SQL to accomplish that. Concatenate the result of your function with a variable containing your SQL then run it with execute immediate.
Google it and you'll find a lot of examples.

Related

How to update multiple rows of data using linq?

I am new to linq query and EFCore, I am needed to design an API which updates multiple rows of data. This is an example of my current database table in postgresql:
| id | value |
| ---- | ----- |
| 1 | 123 |
| 2 | 456 |
I want to write an update API using linq query to allow user to update using JSON into:
| id | value |
| ---- | ----- |
| 1 | abc |
| 2 | zxc |
What is the best approach to code this API?
My advice is you try to use the out-of-the-box functionality. So do the changes you want to do, and then when you are happy do the SaveChanges() that does everything.
And then ask about specific challenges you experience.

How can I combine Postgresgl's ArrayField ANY option with LIKE

I'm trying to filter a queryset on the first characters of an element in an ArrayField in postgresql.
Data
--------------------
| id | registration_date | sbi_codes |
| 1 | 2007-11-13 | {9002, 1002, 85621} |
| 2 | 2010-10-11 | {1002, 9022, 9033 |
| 3 | 2019-02-02 | {9001, 8921} |
| 4 | 2012-02-02 | {120} |
I've tried the following (which obviously don't work), but I think clearly indicates what I'm trying to achieve.
select count(*)
from administrations_administration
where '90' = left(any(sbi_codes),2)
or
select count(*)
from administrations_administration
where '90%' like any(sbi_codes
So the sbi_codes can be for example 9002 or 9045, And I'm trying to filter all the records that contain an element that starts with 90.
expected result
____
| count | sbi_codes |
| 3 | 90 |
Thanks!
The thing on the left hand side of LIKE is the string, in which % is just a %. The thing on the right hand side is the pattern, in which % is a wildcard. Using ANY does't change these semantics, the pattern still goes the right.
To solve this, you could create your own operator which is like LIKE, but has its arguments reversed.

Pivot Data in a BigQuery Standard SQL View Definition

I'm not sure whether this is possible with some of the new BigQuery scripting capabilities, UDFs, array/string functions (or anything else!), however I simply can't figure it out.
I'm trying to write the SQL for a view in BigQuery which dynamically defines columns based on query results, similar to a pivot table in a spreadsheet/BI tool (or melt in pandas). I can do this externally in Python or hard-code it using case statements, but I'm sure that a SQL solution to this would be incredibly useful to a huge number of people.
Essentially I'm trying to write a query which would transform a table like this:
year | name | number
-----------------------
1963 | Michael | 9246
1961 | Michael | 9055
1958 | Michael | 9203
1957 | Michael | 9116
1953 | Robert | 9061
1952 | Robert | 9205
1951 | Robert | 9054
1948 | Robert | 9015
1947 | Robert | 10025
1947 | John | 9634
1946 | Robert | 9295
----------------------
SQL to generate initial example table:
SELECT year, name, number
FROM `bigquery-public-data.usa_names.usa_1910_2013`
WHERE number > 9000
ORDER BY year DESC
Into a table with the following structure:
year | John | Michael | Robert
---------------------------------
1946 | | 9,295 |
1947 | 9,634 | | 10,025
1948 | | 9,015 |
...
This then needs to be connected to downstream tools, without requiring maintenance when the data changes. I know that this is not always a great idea and that tidy form data is more universally useful, but there are still some scenarios where this behaviour is desirable.
I have seen a few solutions on here, but they all seem to involve string generation and then manually pasting the query... I can do this via the BigQuery API but am desperate to find a dynamic solution using nothing but SQL so I don't have to maintain an external function.
Thanks in advance for any pointers!

Combine duplicates and create one comprehensive list

I was wondering what the easiest way would be to get the data I needed.
I am not sure if it is even possible to add columns like that or not. If so I am not sure where the easiest place to accomplish this would be.
Excel Formula, SQL, or VBA code?
I have a huge list of names and what I want to do is take all of the data for a name and get all the unique data from the table then put it in a list basically.
Any help would be appreciated.
DATA
Name | FirstID | SecondID | ThirdID | FourthID | FifthID |
-------------------------------------------------------------
Joe Smith | AB.5 | AC.1 | AH.9 | ZZ.4 | BB.9 |
Joe Smith | DD.7 | AC.1 | AD.3 | XD.5 | BB.9 |
Joe Smith | RV.4 | AC.1 | AD.3 | ZZ.4 | BB.9 |
Joe Smith | AB.5 | AC.1 | AD.3 | ZZ.4 | BB.9 |
Desired Results
Name
-------------------------------------------------------------
Joe Smith | AB.5 | AC.1 | AH.9 | ZZ.4 | BB.9 | DD.7 | AD.3 | XD.5 | RV.4 |
This should be a fairly easy task, so Ill let you know what youd need to do to get this results
1) Learn to open and go through a recordset using vba. Plenty of articles on it and its super easy.
2) Your SQL for this would be SELECT * FROM tbl WHERE Name ='thename'
3) Learn how to store values in an array, or a new tables
4) learn how to check your array or table for preexisting values from the current step in your recordset, and if not there add it(the value in the current spot in the recordset that is).
Happy coding and good luck! Come back when you have tried something and have code to show

Oracle view grouping elements [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Oracle: Combine multiple results in a subquery into a single comma-separated value
Hi there,
this is my problem...
I have a table:
+------+------+------+
| CODE | NAME | TYPE |
+------+------+------+
| 1 | AAA | x |
+------+------+------+
| 2 | BBB | x |
+------+------+------+
| 3 | CCC | y |
+------+------+------+
| 4 | DDD | y |
+------+------+------+
I wanna make a view in ORACLE .... I wanna that the result is:
+---------+------+
| NAME | TYPE |
+---------+------+
| AAA;BBB | x |
+---------+------+
| CCC;DDD | y |
+---------+------+
Can I grouping AAA and BBB because they have same TYPE in a VIEW that in a NAME will be "AAA;BBB" ... so grouping various names divided with ;
Can anyone help me?
Regards,
Tommaso
Tim Hall has a page that covers the various string aggregation techniques available in Oracle depending on the Oracle version, what packages are installed in the database, and whether you can create new procedures to support this or whether you want it done in pure SQL.
If you are using 11.2, the simplest option would be to use the built-in LISTAGG analytic funciton
SELECT listagg(name, ';') within group (order by code), type
FROM your_table
GROUP BY type
If you are using an earlier version, my preference would be to use the custom aggregate function (Tim's string_agg).