sqlplus select record between merged dates - sqlplus

table name :document_archive_doc_perms
table desc:
- location char(1), document_id number(5), permission_id number(5),
permission_type char(1), date_time_from date, date_time_to date,
ecode number(5), ecode_s number(2), approved_by number(5),
approved_by_ecode number(2), approved_on date, primary
key(location,document_id,permission_id));
my query :
select permission_id,document_id,date_time_from,date_time_to,approved_on,permission_type from document_archive_doc_perms
where document_id=3 and ecode=1695 and approved_on is not null and (sysdate between date_time_from and date_time_to);
my output is
PERMISSION_ID DOCUMENT_ID DATE_TIME DATE_TIME APPROVED_ P
------------- ----------- --------- --------- --------- -
5 3 01-DEC-14 31-DEC-14 08-DEC-14 V
7 3 09-DEC-14 31-DEC-14 09-DEC-14 P
here my need is the latest permission from records(ie max of permission_id)
how to do this.?

You could use an anaylitic rank() call:
SELECT permission_id,
document_id,
date_time_from,
date_time_to,
approved_on,
permission_type
FROM (SELECT permission_id,
document_id,
date_time_from,
date_time_to,
approved_on,
permission_type,
RANK() OVER (ORDER BY perission_id ASC) AS rk
FROM document_archive_doc_perms
WHERE document_id = 3 AND
ecode = 1695 AND
approved_on IS NOT NULL AND
SYSDATE BETWEEN date_time_from AND date_time_to
)
WHERE rk = 1

Related

How to create a list of objects from 2 tables in KSQLDB

I currently have 2 tables in KSQLDB:
CREATE STREAM "source-mysql-person" (
"_uid" STRING,
"_created" TIMESTAMP,
"_updated" TIMESTAMP,
"_disabled" TIMESTAMP,
"name" STRING,
"age" INT
) WITH (
KAFKA_TOPIC = 'source-mysql-person',
VALUE_FORMAT = 'AVRO'
);
/*
Field | Type
--------------------------------------------
_uid | VARCHAR(STRING) (primary key)
_created | TIMESTAMP
_updated | TIMESTAMP
_disabled | TIMESTAMP
name | VARCHAR(STRING)
age | INTEGER
--------------------------------------------
*/
CREATE TABLE "table-mysql-enriched-person_contact" WITH (
KAFKA_TOPIC = 'table-mysql-enriched-person_contact',
VALUE_FORMAT = 'AVRO'
) AS SELECT
"pc"."_uid" AS "_uid",
"pc"."_created" AS "_created",
"pc"."_updated" AS "_updated",
"pc"."_disabled" AS "_disabled",
"pc"."is_default" AS "is_default",
"pc"."value" AS "value",
"pc"."person_uid" AS "person_uid",
AS_MAP(
ARRAY['_uid', 'value'],
ARRAY["ct"."_uid", "ct"."value"]
) AS "contact_type"
FROM "table-mysql-person_contact" "pc"
INNER JOIN "table-mysql-contact_type" "ct" ON
"ct"."_uid" = "pc"."contact_type_uid"
EMIT CHANGES;
/*
Field | Type
-----------------------------------------------
_uid | VARCHAR(STRING) (primary key)
_created | TIMESTAMP
_updated | TIMESTAMP
_disabled | TIMESTAMP
is_default | INTEGER
value | VARCHAR(STRING)
person_uid | VARCHAR(STRING)
contact_type | MAP<STRING, VARCHAR(STRING)>
-----------------------------------------------
*/
I want to create a table table-mysql-enriched-person that has the data of table-mysql-person and for each "person", a list of "person_contact" related to that "person". For this I am trying to use the following querie:
CREATE TABLE "table-mysql-enriched-person" WITH (
KAFKA_TOPIC = 'table-mysql-enriched-person',
VALUE_FORMAT = 'AVRO'
) AS SELECT
"p"."_uid" AS "_uid",
"p"."_created" AS "_created",
"p"."_updated" AS "_updated",
"p"."_disabled" AS "_disabled",
"p"."name" AS "name",
"p"."age" AS "age",
AS_MAP(
ARRAY[
'_uid',
'_created',
'_updated',
'_disabled',
'is_default',
'value',
'contact_type'
],
ARRAY[
"e"."_uid",
"e"."_created",
"e"."_updated",
"e"."_disabled",
"e"."is_default",
"e"."value",
"e"."contact_type"
]
) AS "list_person_contact"
FROM "table-mysql-enriched-person_contact" "e"
INNER JOIN "table-mysql-person" "p" ON
"p"."_uid" = "e"."person_uid"
GROUP BY
"p"."_uid",
"p"."_created",
"p"."_updated",
"p"."_disabled",
"p"."name",
"p"."age"
EMIT CHANGES;
Theoretically the query is correct because I have primary keys in the 2 queries and, thinking that the table "person_contact" is a child table of "person" and that the field person._uid is represented as person_contact.person_uid, I should manage to create the table but ksqldb is returning me the following message:
Could not determine output schema for query due to error: GROUP BY requires aggregate functions
in either the SELECT or HAVING clause.

How to convert the columns to lowercase in join condition in Kusto Database

TableA
| where GuidId == "123"
| where Desc has_any ("processor")
| join kind=leftouter TableB on
$left.SubId == $right.SubId,
$left.ProductName == $right.Name,
$left.GuidId == $right.GuidId
| distinct SubId, PriceTags, ResourceType, ProductName, Name
ProductName is in lower case and Name is in camel case. How to bring ProductName and Name to same case in Join condition.
Thanks
something like:
| extend Name=tolower(Name)
TableA
| where GuidId == "123"
| where Desc has_any ("processor")
| join kind=leftouter (TableB | extend Name=tolower(Name)) on $left.SubId == $right.SubId, $left.ProductName==$right.Name, $left.GuidId==$right.GuidId
|distinct SubId, PriceTags, ResourceType, ProductName, Name
extend Name=tolower(Name)
You'll need to 'normalize' the values before the join.
Ideally you'll do this before ingestion, or at ingestion time (using an update policy).
Given the current non-normalized values, you can do it at query time (performance would be sub-optimal):
TableA
| where GuidId == "123"
| where Desc has "processor"
| join kind=leftouter (
TableB
| extend Name = tolower(Name)
) on
$left.SubId == $right.SubId,
$left.ProductName == $right.Name,
$left.GuidId == $right.GuidId
| distinct SubId, PriceTags, ResourceType, ProductName, Name

I have a column in table named "client_name".... i need to select column value from last row in my join query

i have table with column name policy_refer, client name and issue_date
policy_refer Client_Name issue_date(entry_date)
0001 Ajaz 01-Jan-2019
0001 Ajaz 05-Jan-2019
0001 Anita 10-Jan-2019
i want to select last update/insert client_name where policy_refer = 0001 , in my select/join query ....
select policy_master.CLIENT_NAME
,POLICY_INSURER_DETAIL.INSURER_NAME
,POLICY_INSURER_DETAIL.INSURER_BRANCH
,POLICY_INSURER_DETAIL.policy_number
,policy_master.policy_refer
,policy_master.POLICY_CLASS
,policy_master.POLICY_PRODUCT
,policy_master.P_ISSUE_DATE
,policy_master.EXPIRY_DATE
,sum(policy_master.TOTAL_SUMINSURED)
,sum(policy_master.GROSS)
,sum(policy_master.PERMIUM)
from POLICY_MASTER,POLICY_INSURER_DETAIL
where policy_master.policy_refer = POLICY_INSURER_DETAIL.POLICY_REFER
and POLICY_MASTER.POL_ID = POLICY_INSURER_DETAIL.POL_ID
and POLICY_MASTER.EXPIRY_DATE ='19-AUG-20'
and POLICY_MASTER.DOC_STATUS ='Posted'
group by POLICY_MASTER.policy_refer
,POLICY_INSURER_DETAIL.INSURER_NAME
,POLICY_INSURER_DETAIL.INSURER_BRANCH
,POLICY_INSURER_DETAIL.policy_number
,policy_master.policy_refer
,policy_master.EXPIRY_DATE
,policy_master.CLIENT_NAME
,policy_master.POLICY_CLASS
,policy_master.POLICY_PRODUCT
,policy_master.P_ISSUE_DATE;
One option is to find which one's the first (using analytic functions), and then fetch that row. For example:
select column_list_goes_here
from (
--> this is your current query, with the ROW_NUMBER addition
select all your columns here,
--
row_number() over (partition by policy_refer order by issue_date desc) rn
--
from your_tables
where ...
group by ...
--> end of your current query
)
where rn = 1
It appears the table you are talking about is the POLICY_MASTER table and you want the latest row (by ISSUE_DATE) for each policy.
Then you want to filter something like this:
select pm.CLIENT_NAME
,POLICY_INSURER_DETAIL.INSURER_NAME
,POLICY_INSURER_DETAIL.INSURER_BRANCH
,POLICY_INSURER_DETAIL.policy_number
,pm.policy_refer
,pm.POLICY_CLASS
,pm.POLICY_PRODUCT
,pm.P_ISSUE_DATE
,pm.EXPIRY_DATE
,sum(pm.TOTAL_SUMINSURED)
,sum(pm.GROSS)
,sum(pm.PERMIUM)
from (
SELECT *
FROM (
SELECT POL_ID,
CLIENT_NAME,
policy_refer,
POLICY_CLASS,
POLICY_PRODUCT,
P_ISSUE_DATE,
EXPIRY_DATE,
TOTAL_SUMINSURED,
GROSS,
PERMIUM,
ROW_NUMBER() OVER ( PARTITION BY policy_refer /*, pol_id */
ORDER BY ISSUE_DATE DESC ) AS rn
FROM POLICY_MASTER
WHERE EXPIRY_DATE = DATE '2020-08-19'
AND DOC_STATUS ='Posted'
)
WHERE rn = 1
) pm
INNER JOIN POLICY_INSURER_DETAIL pid
ON ( pm.policy_refer = pid.POLICY_REFER
AND pm.POL_ID = pid.POL_ID )
GROUP BY pm.policy_refer
,pid.INSURER_NAME
,pid.INSURER_BRANCH
,pid.policy_number
,pm.policy_refer
,pm.EXPIRY_DATE
,pm.CLIENT_NAME
,pm.POLICY_CLASS
,pm.POLICY_PRODUCT
,pm.P_ISSUE_DATE;
I guess you need the first row after sorting them in descending from issue date -
select policy_master.CLIENT_NAME
,POLICY_INSURER_DETAIL.INSURER_NAME
,POLICY_INSURER_DETAIL.INSURER_BRANCH
,POLICY_INSURER_DETAIL.policy_number
,policy_master.policy_refer
,policy_master.POLICY_CLASS
,policy_master.POLICY_PRODUCT
,policy_master.P_ISSUE_DATE
,policy_master.EXPIRY_DATE
,sum(policy_master.TOTAL_SUMINSURED)
,sum(policy_master.GROSS)
,sum(policy_master.PERMIUM)
from POLICY_MASTER,POLICY_INSURER_DETAIL
where policy_master.policy_refer = POLICY_INSURER_DETAIL.POLICY_REFER
and POLICY_MASTER.POL_ID = POLICY_INSURER_DETAIL.POL_ID
and POLICY_MASTER.EXPIRY_DATE ='19-AUG-20'
and POLICY_MASTER.DOC_STATUS ='Posted'
and rownum = 1
group by POLICY_MASTER.policy_refer
,POLICY_INSURER_DETAIL.INSURER_NAME
,POLICY_INSURER_DETAIL.INSURER_BRANCH
,POLICY_INSURER_DETAIL.policy_number
,policy_master.policy_refer
,policy_master.EXPIRY_DATE
,policy_master.CLIENT_NAME
,policy_master.POLICY_CLASS
,policy_master.POLICY_PRODUCT
,policy_master.P_ISSUE_DATE
ORDER BY P_ISSUE_DATE DESC;

Output parameter issue in a Store procedure

I have 2 table's Customer and Beneficiary. There are 2 or more Beneficiary added with a single Customer Mobile number.
I have Created a Stored proc to get the Beneficiary name related to that Mobile Number
create Proc Customer
(
#Mobile bigint,
#Beneficiary varchar(100) out,
#Beneficiary2 varchar(100) out
)
as
Begin
Select #Beneficiary = Beneficiary_Name, #Beneficiary2 = Beneficairy_Name from Beneficiary_master
as a
join Customer_Master as b
on a.CustomerID= b.CustomerID
where Mobile_Number = #Mobile
End
Declare #Beneficiary varchar(100)
Declare #Beneficiary2 varchar(100)
Exec spCustomer 999999999, #Beneficiary out, #beneficiary2 out
How to get both the Beneficiary Names, as i am getting only 1 name
Your Select Query will return the record(s) matching the select statement, and will assign the result of the last record to the variables #Beneficiary and #Beneficiary2
You have to change the select query to get the desired o/p. Please have a try on this statement, and if suit's your requirement make change in SP accordingly
Declare #Mobile bigint =123
Declare #Beneficiary varchar(100)
Declare #Beneficiary2 varchar(100)
Select #Beneficiary = Beneficiary_Name
from Beneficiary_master as a
join Customer_Master as b on a.CustomerID= b.CustomerID
where Mobile_Number = #Mobile
ORDER BY Beneficiary_Name ASC
OFFSET 0 ROWS
FETCH NEXT 1 ROWS ONLY
Select #Beneficiary2 = Beneficiary_Name
from Beneficiary_master as a
join Customer_Master as b on a.CustomerID= b.CustomerID
where Mobile_Number = #Mobile
ORDER BY Beneficiary_Name ASC
OFFSET 1 ROWS
FETCH NEXT 1 ROWS ONLY
Select #Beneficiary Beneficiary, #Beneficiary2 Beneficiary2

How to find record with integer value instead of string

I have column field as string for ex. start_age("20") and end_age("40").
Now I want user whose age(24 as integer) is between start_age and age_age. So How can I get this user?
I have tried with this query
User.where("start_age < (?) AND end_age > (?)", age, age)
but getting this error:
PG::UndefinedFunction: ERROR: operator does not exist: character varying < integer
Thanks in advance.
Do write :
User.where("start_age::integer <= :age AND end_age::integer >= :age", age: age)
# or equivalent
User.where(":age BETWEEN start_age::integer AND end_age::integer", age: age)
Here is some demo :
test_dev=# select 20 between '1'::integer and '10'::integer as result;
result
--------
f
(1 row)
test_dev=# select 5 between '1'::integer and '10'::integer as result;
result
--------
t
(1 row)
test_dev=# select '12'::integer as number;
number
--------
12
(1 row)

Resources