a query to compare data from two tables A AND B and give the results as per table A - sql

I have a list of phone numbers that am sending messages to. in table A I record all the delivery status of each number.
I created another table B and stored some contacts in it.
I want an SQL query that I can use to compare the data in A and B such that, I want to get delivery status of numbers in table B from table A. If a number is appearing in the table A and B then I get the delivery status of that number.
I am using this SQL query:
SELECT address, delivery_status FROM safaricom_receipt
WHERE address IN (SELECT mobile FROM saf_dispute);

This will work for you :
SELECT address, delivery_status
FROM safaricom_receipt
INNER JOIN saf_dispute
ON safaricom_receipt.address=saf_dispute.mobile;

Assuming you are using SQL Server; this should work for you
SELECT sd.mobile, sr.delivery_status
FROM saf_dispute sd with (nolock)
LEFT OUTER JOIN safaricom_receipt sr with (nolock) on sd.mobile=sr.address
where sr.delivery_status is NOT NULL
you would get duplicate rows if you are storing multiple rows for the same mobile number in any table. I would suggest adding a datetime column in the future

Related

Comparing 3 tables in an Access Database

I am trying to compare a series of tables within an access database, 2 local and one linked.
Table A (local) contains UserID, Title, Position; Table B (linked) contains UserID, Title, and Position from the previous week (records could possibly change on a week to week basis); Table C (local) contains UNIQUE UserID's and Titles.
I need to ensure that all UserID's contained in Table C still exist
in Table A.
I need to ensure that all UserID's contained in Table C have not had
a change in Title or Position from the previous week. If so Add to a temp table.
I'd prefer to use Access VBA or SQL in accomplish this task and the information will be displayed in a report.
Basically the same logic for both examples. use a left join to to identify mismatches.
Identify missing users in A
Insert into TableA (userID,Title)
select TableC.UserID, TableC.Title
from TableC
left join TableA on TableC.UserID=TableA.UserID
where TableA.UserID is null
Identify changes from B to A
insert into temp (userID,title,position)
select c.userID,c.title,c.position
from TableA a
left join tableB b on b.userid=a.userID and b.title=a.title and b.position=a.position
where b.userID is null

How to query 2 different schema tables with different column name same information in postgresql

When I query I do not get match list side by side and database reference fails, why?
I tried using one connection and two different schemas, first schema name table has same reference details with second table column, and I would like to compare side by side what equals exactly from rw.reference table 1, with table 2 qt.ref_number because they should match between both. I only get output data from top first query with rd.reference and second table does not list anything with, qt.ref_number, why postgresql does not cross database reference?
Query:
Select distinct
rd.reference,
region1_scorecard
From local_dev.user.rawdata rd
Inner join customerid as id
on rd.site=id.site
union all
Select distinct
qt.ref_number,
region2_decision
From local_dev.account.quote qt
Inner join rep_id id
on qt.application=id.application
Order by rd.reference

Joining two tables to update results in third table using like

I am trying to join two tables and update the results in the third table.
So table A is the results table and it has the columns customer number and score.
Table B has customer number and ind_code and table C has ind_code and ind_score.
So the output of the query should be such that the ind_code in table B and C should join together based on the first two digits and ind_score should be updated in Table A in the score column. Table A and Table B should be joined on the basis of customer number.
Could anyone please help. I tried multiple queries but nothing seems to work. i am using oracle sql developer
Generally, the JOIN operation mustn't cut field information but if your structure (for me not correct) is that ...
If I understand better:
UPDATE TableA
SET score =
(SELECT MAX(C.ind_score)
FROM TableC C
JOIN TableB B
ON C.ind_code = SUBSTRING(B.ind_code, 1, 2)
WHERE B.customernumber = TableA.customernumber)
I use on subquery MAX aggregate function, because I don't know if your cut of ind_code of TableB can be not unique (i.e. you have ind_code 5555 and 5554)

query satisying the given condition

I am using sql server management studio 2012. Please tae a minute or two to understand this problem, It's really important:(
The scenario is :
I have three table, Patient, patientAccount and Message.
Now, I am inserting values into tables Patient and Message first, and then after some time I am inserting values into PatientAccount Table.Now, the problem is in the software i am using sql with) i had this query:
select
*
from Patient
join Message on Patient.GUID = Message.PatientGUID
where Message.GUID = "..Data
and if I add PatientAccount in this query as well, like
select
*
from Patient
join Message on Patient.GUID = Message.PatientGUID, PatientAccount
where Message.GUID = "..Data
It becomes mandatory to add values to the PatientAccount table at the same moment when I am adding them to Patient and Message table, but what I really want is values to be taken up by query if there is data in the PatientAccount table, and if there is no values inserted into patientAccount table, It should pick up values from Patient and Message table only.
and I want all of it in one query.
You can use an outer join for this purpose so that the select query doesn't depend on the PatientAccount table. Similar to the query below
SELECT *
FROM patient p
LEFT JOIN patientaccount pa
ON p.guid = pa.patientguid
LEFT JOIN message m
ON p.guid = m.patientguid
WHERE m.guid = "id"
This select query will always return records from the PatientAccount table but if there's no data for a specific Patient then the columns from the PatientAccount will be null

Select average rating from another datatable

I have 3 data tables.
In the entries data table I have entries with ID (entryId as primary key).
I have another table called EntryUsersRatings in there are multiple entries that have entryId field and a rating value (from 1 to 5).
(ratings are stored multiple times for one entryId).
Columns: ratingId (primary key), entryId, rating (integer value).
In the third data table I have translations of entries in the first table (with entryId, languageId and title - translation).
What I would like to do is select all entries from first data table with their titles (by language ID).
On a top of that I want average rating of each entry (which can be stored multiple times) that is stored in EntryUsersRatings.
I have tried this:
SELECT entries.entryId, EntryTranslations.title, AVG(EntryUsersRatings.rating) AS AverageRating
FROM entries
LEFT OUTER JOIN
EntryTranslations ON entries.entryId = EntryTranslations.entryId AND EntryTranslations.languageId = 1
LEFT OUTER JOIN
EntryUsersRatings ON entries.entryId = EntryUsersRatings.entryId
WHERE entries.isDraft=0
GROUP BY title, entries.entryId
isDraft is just something that means that entries are not stored with all information needed (just incomplete data - irrelevant for our case here).
Any help would be greatly appreciated.
EDIT: my solution gives me null values for rating.
Edit1: this query is working perfectly OK, I was looking into wrong database.
We also came to another solution, which gives us the same result (I hope someone will find this useful):
SELECT entries.entryId, COALESCE(x.EntryUsersRatings, 0) as averageRating
FROM entries
LEFT JOIN(
SELECT rr.entryId, AVG(rating) AS entryRating
FROM EntryUsersRatings rr
GROUP BY rr.entryId) x ON x.entryId = entries.entryId
#CyberHawk: as you are using left outer join with entries, your result will give all records from left table and matching record with your join condition from right table. but for unmatching records it will give you a null value .
check out following link for the deta:
http://msdn.microsoft.com/en-us/library/ms187518(v=sql.105).aspx