I have a table with below schema which should take string input and give me output:
create table tblA (sName varchar(20), id int);
select * from tblA;
insert into tblA (sName, id) values ('Bay', 2), ('Kay', 3), ('May', 4);
select distinct sName
from tblA
where id in ("3,4");
O/P:
Kay
I need:
Kay
May
There is an application that sends String and MySQL needs to read that string parse and give an output. The application cannot send integer value. In the application input is only id. Whatever I enter, it will be converted into String and will be passed to the query. My input will always be comma seperated e.g. (4,5,6). Each 4, 5, 6 is id. But application sends (4,5,6) as a single string.
Try
select sName
from tblA
where id in (3, 4)
I.e remove the qutation marks from the argument to in
Edit following OP's comment
select sName
from tblA
where id in ('3', '4')
i.e. Single quotes and quote each possible value of id.
Related
I have a Matrix Report built in SSRS, I have it grouped on the ID for the rows and grouped on the FirstName & LastName for the columns to create the needed report, however the only thing that I need to get to work is the to indent the second and third rows to start at the first column instead of after the last column of the first row as showing, how can I get that to work please?
Thanks in advance!
Your problem is that you have created a column group based on FirstName + LastName. This will create a new column for each instance of this (so 1 column for each record as they are all unique).
What you need to do is assign a column number for each name with each ID.
Here I have reproduced your sample data and then assigned a value to ColN for each unique name within each ID.
DECLARE #t TABLE (ID INT, FirstName varchar(20), LastName varchar(20))
INSERT INTO #t VALUES
(25, 'Abby', 'Mathews'),
(25, 'Jennifer', 'Edwards'),
(26, 'Peter', 'Williams'),
(27, 'Johns', 'Jacobs'),
(27, 'Mark', 'Scott')
SELECT
ID, FirstName, LastName
, ColN = ROW_NUMBER() OVER(PARTITION BY ID ORDER BY FirstName, LastName)
FROM #t
This gives us the following output.
You can now use ColN in you column group instead of the expression you now have.
This will give you the desired output.
Requirement is to generate JSON from clob data type column.
environment version Oracle 12.2
I have a table with fields id (number data type) and details (clob type) like below
ID - details
100 - 134332:10.0, 1481422:1.976, 1483734:1.688, 2835036:1.371
101 - 134331:0.742, 319892:0.734, 1558987:0.7, 2132090:0.697
eg output:
{
"pId":100,
"cid":[
{
"cId":134332,
"wt":"10.0"
},
{
"cId":1481422,
"wt":"1.976"
},
{
"cId":1483734,
"wt":"1.688"
},
{
"cId":2835036,
"wt":"1.371"
}
]
}
please help with oracle SQL query to generate output.
Below I set up a table with a few input rows for testing; then I show one way you can solve your problem, and the output from that query. I didn't try to write the most efficient (fastest) query; rather, I hope this will show you how this can be done. Then if speed is a problem you can work on that. (In that case, it would be best to reconsider the inputs first, which break First Normal Form.)
I added a couple of input rows for testing, to see how null is handled. You can decide if that is the desired handling. (It is possible that no null are possible in your data - in which case you should have said so when you asked the question.)
Setting up the test table:
create table input_tbl (id number primary key, details clob);
insert into input_tbl (id, details) values
(100, to_clob('134332:10.0, 1481422:1.976, 1483734:1.688, 2835036:1.371'));
insert into input_tbl (id, details) values
(101, '134331:0.742, 319892:0.734, 1558987:0.7, 2132090:0.697');
insert into input_tbl (id, details) values
(102, null);
insert into input_tbl (id, details) values
(103, '2332042: ');
commit;
Query:
with
tokenized (pid, ord, cid, wt) as (
select i.id, q.ord, q.cid, q.wt
from input_tbl i cross apply
(
select level as ord,
regexp_substr(details, '(, |^)([^:]+):', 1, level, null, 2)
as cid,
regexp_substr(details, ':([^,]*)', 1, level, null, 1) as wt
from dual
connect by level <= regexp_count(details, ':')
) q
)
, arrayed (pid, json_arr) as (
select pid, json_arrayagg(json_object(key 'cId' value to_number(trim(cid)),
key 'wt' value to_number(trim(wt)))
)
from tokenized
group by pid
)
select pid, json_object(key 'pId' value pid, key 'cid' value json_arr) as json
from arrayed
;
Output:
PID JSON
---- -----------------------------------------------------------------------------------------------------------------------------
100 {"pId":100,"cid":[{"cId":134332,"wt":10},{"cId":2835036,"wt":1.371},{"cId":1483734,"wt":1.688},{"cId":1481422,"wt":1.976}]}
101 {"pId":101,"cid":[{"cId":134331,"wt":0.742},{"cId":2132090,"wt":0.697},{"cId":1558987,"wt":0.7},{"cId":319892,"wt":0.734}]}
102 {"pId":102,"cid":[{"cId":null,"wt":null}]}
103 {"pId":103,"cid":[{"cId":2332042,"wt":null}]}
While working on oracle json datatype and trying to extract data from it, not able to extract name & value elements from this. tried using all known notations but getting null.
select json_query(po_document, '$.actions.parameters[0]') from j_purchaseorder where ID='2';
You can use the JSON_VALUE function as follows:
SQL> select JSON_VALUE('{"_class":"123", "name":"tejash","value":"so"}', '$.name') as name,
2 JSON_VALUE('{"_class":"123", "name":"tejash","value":"so"}', '$.value') as value
3 from dual;
NAME VALUE
---------- ----------
tejash so
SQL>
Thanks for your help. got required output using below
select json_value(json_query(po_document, '$.actions.parameters[0]'),'$.value') from j_purchaseorder where ID='2' and
json_value(json_query(po_document, '$.actions.parameters[0]'),'$.name') = 'SERVERUSER';
As explained, for example, in the Oracle documentation, multiple calls to JSON_VALUE() on the same JSON document may result in very poor performance. When we need to extract multiple values from a single document, it is often best (for performance) to make a single call to JSON_TABLE().
Here is how that would work on the provided document. First I create and populate the table, then I show the query and the output. Note the handling of column (attribute) "_class", both in the JSON document and in the SQL SELECT statement. In both cases the name must be enclosed in double-quotes, because it begins with an underscore.
create table j_purchaseorder (
id number primary key,
po_document clob check (po_document is json)
);
insert into j_purchaseorder (id, po_document) values (
2, '{"_class":"hudson.model.StringParameterValue","name":"SERVERUSER","value":"avlipwcnp04"}'
);
commit;
select "_CLASS", name, value
from j_purchaseorder
cross apply
json_table(po_document, '$'
columns (
"_CLASS" varchar2(40) path '$."_class"',
name varchar2(20) path '$.name',
value varchar2(20) path '$.value'
)
)
where id = 2
;
_CLASS NAME VALUE
---------------------------------------- ------------------ ------------------
hudson.model.StringParameterValue SERVERUSER avlipwcnp04
I have to get count of each value separated by ## without
using Any table or stored procedure . i have to get count for each value repeated by
special symbol as per the given schema. I have tried alot but i want to make this result set without helping any reference table or stored procedure or temporary table.
Schema is like this :-
CREATE TABLE USER_TABLE (
id INT,
name VARCHAR(20));
INSERT INTO USER_TABLE VALUES
(1, 'A##B##C'),
(2, 'B##C'),
(3,'A'),
(4,'B##C');
Expected output is Like :-
Name COUNT
A 2
B 3
C 3
Try these query, may help you:
Select GROUP_CONCAT((name) SEPARATOR '#') as name, count(name) as count FROM your_table
I am trying to create a query that allows me to get the sum of a total stored in one table based on values in another table.
Specifically, I have one table called 'winning_bids', that I want to join with another table, called 'objects'. 'winning_bids' contains a User ID, and an Object ID (primary key of 'objects' table). The 'objects' table contains an Object ID, and the value of the object. I want to sum the value from the 'objects' table for each user, grouped by the User ID from the 'winning_bids' table.
I tried something like this, but it does not work:
SELECT SUM(o.value) AS total, w.uid
FROM winning_bids w
LEFT JOIN objects o ON (o.id = w.oid)
GROUP BY w.uid
This statement merely returns all of the User IDs, but with the total for only the first User ID in each row.
Any help would be appreciated, thanks.
It works fine for me.
Here is what I did to test your query:
CREATE TABLE winning_bids (uid INT NOT NULL, oid INT NOT NULL);
INSERT INTO winning_bids (uid, oid) VALUES
(1, 1),
(1, 2),
(2, 3);
CREATE TABLE objects (id INT NOT NULL, value INT NOT NULL);
INSERT INTO objects (id, value) VALUES
(1, 1),
(2, 20),
(3, 300);
SELECT SUM(o.value) AS total, w.uid
FROM winning_bids w
LEFT JOIN objects o ON (o.id = w.oid)
GROUP BY w.uid;
Result:
total uid
21 1
300 2
If you still think it doesn't work can you please post example input data that gives the wrong result when you run your query, and also specify what you believe that the correct result should be.