How to get month number or name in cds view - abap

I am creating a CDS view in Hana studio where i want to get month number or name from date (YYYYMMDD) in report, but I am unable to find any function like month or anything else,
Please help.

You can join table t247 that has the required information:
#AbapCatalog.sqlViewName: 'ZDD_DATE_T'
#AccessControl.authorizationCheck: #NOT_REQUIRED
define view zdd_date_test
with parameters p_date:abap.dats(8)
as select from demo_expressions left outer join t247 as date_information on date_information.spras = $session.system_language {
key mandt,
key id,
num1,
num2,
date_information.ltx as long_text
} where date_information.mnr = substring(:p_date, 5, 2);
This will return the following data from table demo_expressions:
id,num1,num2,long_text
0,90,18,November
1,19,99,November
2,83,82,November
3,87,92,November
4,15,56,November
5,29,4,November
6,38,87,November
7,74,13,November
8,26,99,November
9,35,50,November
The use of substring(:p_date, 5, 2) is what you use to extract the month number and then join table t247.

Related

SQL - Returning fields based on where clause then joining same table to return max value?

I have a table named Ticket Numbers, which (for this example) contain the columns:
Ticket_Number
Assigned_Group
Assigned_Group_Sequence_No
Reported_Date
Each ticket number could contain 4 rows, depending on how many times the ticket changed assigned groups. Some of these rows could contain an assigned group of "Desktop Support," but some may not. Here is an example:
Example of raw data
What I am trying to accomplish is to get the an output that contains any ticket numbers that contain 'Desktop Support', but also the assigned group of the max sequence number. Here is what I am trying to accomplish with SQL:
Queried Data
I'm trying to use SQL with the following query but have no clue what I'm doing wrong:
select ih.incident_number,ih.assigned_group, incident_history2.maxseq, incident_history2.assigned_group
from incident_history_public as ih
left join
(
select max(assigned_group_seq_no) maxseq, incident_number, assigned_group
from incident_history_public
group by incident_number, assigned_group
) incident_history2
on ih.incident_number = incident_history2.incident_number
and ih.assigned_group_seq_no = incident_history2.maxseq
where ih.ASSIGNED_GROUP LIKE '%DS%'
Does anyone know what I am doing wrong?
You might want to create a proper alias for incident_history. e.g.
from incident_history as incident_history1
and
on incident_history1.ticket_number = incident_history2.ticket_number
and incident_history1.assigned_group_seq_no = incident_history2.maxseq
In my humble opinion a first error could be that I don't see any column named "incident_history2.assigned_group".
I would try to use common table expression, to get only ticket number that contains "Desktop_support":
WITH desktop as (
SELECT distinct Ticket_Number
FROM incident_history
WHERE Assigned_Group = "Desktop Support"
),
Than an Inner Join of the result with your inner table to get ticket number and maxSeq, so in a second moment you can get also the "MAXGroup":
WITH tmp AS (
SELECT i2.Ticket_Number, i2.maxseq
FROM desktop D inner join
(SELECT Ticket_number, max(assigned_group_seq_no) as maxseq
FROM incident_history
GROUP BY ticket_number) as i2
ON D.Ticket_Number = i2.Ticket_Number
)
SELECT i.Ticket_Number, i.Assigned_Group as MAX_Group, T.maxseq, i.Reported_Date
FROM tmp T inner join incident_history i
ON T.Ticket_Number = i.Ticket_Number and i.assigned_group_seq_no = T.maxseq
I think there are several different method to resolve this question, but I really hope it's helpful for you!
For more information about Common Table Expression: https://www.essentialsql.com/introduction-common-table-expressions-ctes/

Join 3 tables complex condition

I have a requirement to join three tables which have the same structure as shown below.
I need to join these tables based on the store and date field values as shown below:
The complex part here is that even if the row in a table does not match with the other tables, it should create a new row with the other values as zero. For example if there is no shipped and allocated quantities for a store on a particular day but if there is a canceled quantity on that day, a new row should be created with shipped $ allocated values set to 0 and cancelled field value set to the correct value. Can anybody suggest the best method to approach this?
this should work
select alldates.procdate,
alldates.store,
st.quantity as ship_quantity,
ct.quantity as cancel_quantity,
at.quantity as allocation_quantity
from (select procdate, store from shp_table union
select procdate, store from cnl_table union
select procdate, store from aloc_table) alldates
left join shp_table st on alldates.procdate = st.procdate and alldates.store = st.store
left join cnl_table ct on alldates.procdate = ct.procdate and alldates.store = ct.store
left join aloc_table at on alldates.procdate = at.procdate and alldates.store = at.store

displaying data in dropdownlist using sql with querystring

I have three tables:
tbprd -- prdcod int PK, prdtit v(100), prddefuom int FK to prduomcod (tbprduom)
tbuom -- uomcod int PK, uomnam v(100)
tbprduom -- prduomcod int PK, prduomprdcod int FK prdcod(tbprd), prduomuomcod int FK uomcod(tbuom)
What I need is to display uomnam in dropdownlist display field and uomcod in data value field from prdcod which I have passed through query string but if the chosen prdcod is 4 then the dropdownlist should display all the uomnam matching w.r.t all matching prdtit
Query I am using:
select uomcod, uomnam
from tbuom, tbprd, tbprduom
where prdcod = 4
and prduomuomcod = uomcod
and prddefprduomcod = prduomcod
Look at the images for more clearer idea about what I want and table data
https://drive.google.com/drive/folders/17p3d1WXMppndIq1bXf6EwShLWBrh-IsC?usp=sharing
Based on what I understand, you want to get all products that have the same title as the product with the given prdcod. If that's correct, then here's one way to do it:
Write a query to get the prdtit value for prdcod = 4 (return one column from one row):
SELECT prdtit
FROM ...
Use this value to filter the rest of the rows in the tbprd table:
SELECT *
FROM tbrpd
WHERE prdtit = (<Query in Step 1>)
Add two joins to the tbprduom and tbuom tables to Step #2 to get the remaining info:
SELECT tbuom.uomcod, tbuom.uomnam
FROM tbrpd
WHERE prdtit = (<Query in Step 1>)
LEFT JOIN tbprduom ON ... <PK/FK fields) -- Get uomcod for products
LEFT JOIN tbuom ON ... <PK/FK fields) -- Get "uom" fields to return
Give it a try and see how far you get. If you get stuck on a step, just let me know.

LEFT JOIN include data

I have an application which handles school vacation. Unfortunately there are three kinds of different school vacations: Country wide, Federal State wide and City wide vacations. I store all the information in a table days, a table vacation_periods and a connection table slots:
days {
id:integer
date_value:date
}
slots {
id:integer
day_id:integer
vacation_period_id:integer
}
vacation_periods {
id:integer
starts_on:date
ends_on:date
name:string
country_id:integer
federal_state_id:integer
city_id:integer
}
I want to select all days within a specific time frame. Let's say Jan 1st of 2017 to Jan 31st of 2017. I can get those days with:
SELECT * FROM days WHERE date_value >= '2017-01-01' AND
date_value <= '2017-01-31';
But for my vacation calendar I don't just need the days but also the information which vacation_periods are within. Assuming I search for all vacation_periods which are in that time frame and which have
country_id == 1 or federal_state_id == 5 or city_id == 30
I've read about JOINS and LEFT JOINS which seem to be the solution to the problem. But I can't get everything together.
Is it possible to send one SQL request which returns all days within the requested time frame and the additional information if a vacation_period that fits the country_id == 1 or federal_state_id == 5 or city_id == 30 rule is connected via slots to each day. Including the name of that vacation_period?
If one request is not possible: Which is the quickest way to solve this within the database? How many requests? What kind of requests?
If possible I'd like to get a result in some kind of this form:
- date_value: "2017-01-01"
- date_value: "2017-01-02"
- date_value: "2017-01-03"
* vacation_period.id: 15
* vacation_period.name: "foobar"
- date_value: "2017-01-04"
* vacation_period.id: 15
* vacation_period.name: "foobar"
- date_value: "2017-01-05"
* vacation_period.id: 15
* vacation_period.name: "foobar"
- date_value: "2017-01-06"
- date_value: "2017-01-07"
...
The following query might give you the answer you are looking for:
SELECT * FROM days WHERE date_value >= '2017-01-01' AND date_value <='2017-01-31'
INNER JOIN slots ON days.id = slots.day_id
INNER JOIN vacation_periods ON vacation_periods.id = slots.vacation_period_id
I think you can get an unformatted version of what you want (that could be processed into a hierarchical output) with
CREATE TYPE vacation_authority AS ENUM
('COUNTY', 'FED-STATE', 'CITY');
/* not necessary, but cleans up the vacation_period table */
change to let vacation_period have only one id, and a new field authority of type vacation_authority. You can now make a primary key out of either the id field or (id, authority), depending on how the vacation data comes into the system.
SELECT date_value, vp.name, vp.id /* is the ID meaningful or arbitrary? */
FROM dates LEFT JOIN vacation_periods vp
WHERE date_value BETWEEN vp.starts_on AND vp.ends_on; -- inclusive range
Now if there are multiple holidays spanning a given date, this will be multiple records in the output. It's not clear what you want in this case.
None of the other answers was able to solve my problem but they let me to the solution so I'm grateful for them. Here's the solution:
SELECT days.date_value, slots.period_id, vacation_periods.name FROM days
LEFT OUTER JOIN slots ON (days.id = slots.day_id)
LEFT OUTER JOIN vacation_periods ON (slots.period_id = vacation_periods.id)
WHERE days.date_value >= '2017-01-05'
AND days.date_value <='2017-01-15'
AND (vacation_periods.id IS NULL
OR vacation_periods.country_id = 1
OR vacation_periods.federal_state_id = 5)
ORDER BY days.date_value;

Determine values in a database that occur before a certain date, but not after that date

I need to determine if a certain field value in a database table occurs before a certain date, but not after that date.
I can determine the values that occur before the cutoff date with a simple select, but there may be records after that date.
The field values that I am using are the 'entereddate' and the value I am looking for (in this case a carriercode).
Thanks for your help!
This is the best I can do without seeing the data structure.
SELECT *
FROM BillTBL a
INNER JOIN carriertbl b ON a.carrier_key = b.carrier_key
WHERE a.billentereddate < '2009-09-01'
AND NOT EXISTS (SELECT 1
FROM BillTBL
WHERE whatever_the_key_is = a.whatever_the_key_is
AND billentereddate > '2009-09-01')
select a.carriercode
from carriertbl as a
inner join BillTBL as b ON b.carrier_key = a.carrier_key and b.enteredate < '2009-09-01'
Maybe you have to ajust some column name...