I am trying to get the sender email adress of the entries saved in the table SOOD in SAP. I need to get this info via tables. I have seen that there might be some FM, like so_object_read, that can achieve so, but I need to relate those two fields via tables.
This table stores the info that appears in SCOT trx (SOIN - received messages and SOST sent messages).
Any suggestion?
Thank you so much!
Here is how you can reach message sender if you have SOOD record:
SELECT sost~objtp, sost~objno, sost~objyr, sost~msgv2
FROM sood
LEFT JOIN sost
ON sood~objtp = sost~objtp
AND sood~objno = sost~objno
AND sood~objyr = sost~objyr
INTO TABLE it_sost.
Sender is contained in MSGV2 and recipient in MSGV1 of SOST table.
The same logic should be used with SOIN table.
Related
Problem Introduction:
I'm working on a faker e-banking system as a side project for practicing my SQL skills (which are super bad), and I'm now stuck at a point where I want to pull data from two tables but cannot do that with joins
So, basically I have a users table which looks like this:
And I have a transactions table which saves the sender id, receiver id, amount, and date. And it looks like this:
What I want to achieve:
Now, I want to create a query that extracts the data in the transactions table in a way such that if I specified an ID of n (somewhere in the query), I'd get all of the transactions that user with ID of n made in a way that it displays the first name of both sender and receiver of all of theses transactions.
I've been doing a lot of joins/subquery stuff to extract that information but I really can't seem to find a solution.
I'm using PostgreSQL btw.
Notes:
Sender & Receiver are not the same person
I want to display the first name of both the sender & receiver
You can achieve this by joining to the users table twice (you'll have to give it a separate name for each join). The following snippet leaves out some detail but illustrates the idea -
SELECT
...
FROM
transactions t
INNER JOIN users s ON t.sender = s.id
INNER JOIN users r ON t.receiver = r.id
in the transaction table I understand that the sender and receiver columns are the ids of the customers in the users table.
Filter all the transactions in which a jane user with id 17 has received a transaction (for simplicity I will use *, but ideally you should select the columns you need)
select * from users, transactions where users.id=transactions.receiver and users.id='17'.
the same but for jane shipments
select * from users, transactions where users.id=transactions.sender and users.id='17'.
if you want all the jane transactions
select * from users, transactions where users.id=transactions.receiver and users.id=transactions.sender and users.id='17'.
if you want to further filter by an amount of money sent
select * from users, transactions where users.id=transactions.sender and users.id='17' and amount > 100
finally
select first_name from users, transactions where users.id=transactions.receiver and users.id=transactions.sender and (users.id='17' or users.id='18' )
finally finally
select first_name from users, transactions where users.id=transactions.receiver and users.id=transactions.sender
SMS Confirmations From Users
Facebook sends SMS texts when users attempt to 2FA (2-factor authenticate) to log into the platform. In order to successfully 2FA they must confirm they received the SMS text. Confirmation texts are only valid on the date they were sent. Unfortunately, there was an ETL problem where friend requests and invalid confirmation records were inserted into the logs which are stored in the 'fb_sms_sends' table. Fortunately, the 'fb_confirmers' table contains valid confirmation records so you can use this table to identify confirmed SMS texts.
Calculate the percentage of confirmed SMS texts for August 4, 2020.
fb_sms_sends
ds datetime
country varchar
carrier varchar
phone_number int
type varchar
fb_confirmers
date datetime
phone_number int
My solution -
Select s.ds, (count(c.phone_number)::Float/count(s.phone_number)::Float)*100 as perc
from fb_sms_sends s
left join fb_confirmers c
on s.phone_number = c.phone_number
where s.ds = c.date
group by s.ds
fb_sms_sends table
Not sure what is wrong here. Can someone please explain?
I think what you're not handling is the scenario
Unfortunately, there was an ETL problem where friend requests and invalid confirmation records were inserted into the logs which are stored in the 'fb_sms_sends' table. Fortunately, the 'fb_confirmers' table contains valid confirmation records so you can use this table to identify confirmed SMS texts.
You need to remove the friend requests and invalid confirmation records from the table.
If you add
type NOT IN ('confirmation', 'friend_request')
to your WHERE clause, you should get the right answer.
Instead of using date in where condition, you need to use it in the join condition, it will keep all the records from the left table. Keeping in the where makes it an inner join. I slightly modified your solution.
Select s.ds, (count(c.phone_number)::Float/count(s.phone_number)::Float)*100 as perc
from fb_sms_sends s
left join fb_confirmers c
on s.phone_number = c.phone_number and s.ds = c.date
where type <> 'friend_request'
and s.ds = '2020-08-04'
group by 1
I'm writing an application that implements a message system through a 'memos' table in a database. The table has several fields that look like this:
id, date_sent, subject, senderid, recipients,message, status
When someone sends a new memo, it will be entered into the memos table. A memo can be sent to multiple people at the same time and the recipients userid's will be inserted into the 'recipients' field as comma separated values.
It would seem that an SQL query like this would work to see if a specific userid is included in a memo:
SELECT * FROM memos WHERE recipients LIKE %15%
But I'm not sure this is the right solution. If I use the SQL statement above, won't that return everything that "contains" 15? For example, using the above statement, user 15, 1550, 1564, 2015, would all be included in the result set (and those users might not actually be on the recipient list).
What is the best way to resolve this so that ONLY the user 15 is pulled in if they are in the recipient field instead of everything containing a 15? Am I misunderstanding the LIKE statement?
I think you would be better off having your recipients as a child table of the memos table. So your memo's table has a memo ID which is referenced by the child table as
MemoRecipients
-----
MemoRecipientId INT PRIMARY KEY, IDENTITY,
MemoId INT FK to memos NOT NULL
UserId INT NOT NULL
for querying specific memos from a user you would do something like
SELECT *
FROM MEMOS m
INNER JOIN memoRecipients mr on m.Id = mr.memoId
WHERE userId = 15
No, you aren't misunderstood, that's how LIKE works.. But to achieve what you want, it would be better not to combine the recipients into 1 field. Instead try to create separate table that saves the recipient list for each memo..
For me I will use below schema, for your need:
Table_Memo
id, date_sent, subject, senderid, message, status
Table_Recipient
id_memo FK Table_Memo(id), recipient
By doing so, if you want to get specific recipients from a memo, you can do such query:
SELECT a.* FROM Table_Memo a, Table_Recipient b
WHERE a.id = "memo_id" AND a.id = b.id_memo AND b.recipient LIKE %15%
I am not sure how your application is exactly pulling these messages, but I imagine that better way would be creating a table message_recepient, which will represent many-to-many relationship between recipients and memos
id, memoId, recepientId
Then your application could pull messages like this
SELECT m.*
FROM memos m inner join message_recepient mr on m.id = mr.memoId
WHERE recepientId = 15
This way you will get messages for the specific user. Again, don't know what your status field is for but if this is for new/read/unread, you could add in your where
and m.status = 'new'
Order by date_set desc
This way you could just accumulate messages, those that are new
i need to select data that is not present in the junction table
so got three tables
trialsTable (trialID,TrialName)
VolunteerTAble(volunteerID, VolunteerName)
JunctionTAble(JunctionTableIs,TrialID,VolunteerName)
for every trial an email is sent to volunteers, but next time i want to exclude the volunteers which have received email for that trial and only send email to volunteers which have not received email for that trial, hence i created the junction table which have many to many relationship for the trial and volunteers
Assuming you have a variable or something for the #TrialId which you currently want something like:
SELECT *
FROM VolunteerTable v
WHERE NOT EXISTS(SELECT 1
FROM JunctionTable j
WHERE j.VolunteerName = v.VolunteerName
AND j.TrialID = #TrialId)
And you should probably put volunteerID in you juntion table instead of name.
I am trying to write a complicated stored procedure for the first time. My goal is to get the count with some condition from 2 tables.
Consider Merchant table and Email table.
Email table saves the Email invitations sent by the Merchant. Merchant table has all the Merchant Info along with Email IDs.
My goal is to get the count of EmailID s that are in the Merchant table by checking if the Email invitations sent by Merchant has signed up.
I have tried to make this question clear... Hope i am clear.
Thanks in advance..
Why a stored procedure? It sounds like it can be done in a single SQL query.
Let's see if I understand your question correctly: Merchants invite other people to become a Merchant as well and you want a list with the number of accepted invitations per merchant?
Something along those lines:
select MerchantName, count(1)
from Merchants, Emails
where Merchants.Id = Emails.Id
and Emails.SignedUp = 'YES!'
group by MerchantName;
It sounds like there is some confusion in that schema you're describing. Based on what I think you're trying to do I'd suggest you have a Merchant table, an Email table and a MerchantEmail table which links Merchants to email invitations sent.
The way it appears otherwise with a Merchant table that has an Email ID is a many-to-one relationship so that several Merchants could be the recipients of an email. In that case the Signed Up flag would appear in the Merchants table and not in the Email table.
CREATE PROCEDURE GetMerchantsSignedUp
#EmailId INT
AS
SELECT COUNT(*) AS MerchantSignedUp
FROM Merchant, Email
WHERE Merchant.EmailId = Email.Id
AND Merchant.SignedUp = 1
AND Email.Id = #EmailId
Please note that there is some redundancy above which suggests that you need not even include the Email table in the query. Here it acts only to indicate a foreign key relationship.