Given value in output in db2 - db2

I have a query where I am getting child emails for a parent email after few joins
select * from user_email WHERE user_eaddr_txt in (select u.ADDR_TEXT from cust_mail c, intnl_user u where c.cust_id=u.cust_id and c.cust_eaddr_txt in ('TEST#GMAIL.COM')) ;
I get all child emails linked to that parent email.
testchild#gmail.com
Is there anyway I can get Parent email along with child email in the result set?
testchild#gmail.com TEST#GMAIL.COM

What about something like this
SELECT c.cust_eaddr_txt as parent_email
, u.ADDR_TEXT as child_email
FROM cust_mail c
INNER JOIN intnl_user u ON c.cust_id=u.cust_id
WHERE c.cust_eaddr_txt = 'TEST#GMAIL.COM'
if you need additional details from the user_email table you need to join that as well
SELECT c.cust_eaddr_txt as parent_email
, u.ADDR_TEXT as child_email
, ue.*
FROM cust_mail c
INNER JOIN intnl_user u ON c.cust_id=u.cust_id
INNER JOIN user_email ue ON ue.user_eaddr_txt = u.ADDR_TEXT
WHERE c.cust_eaddr_txt = 'TEST#GMAIL.COM'
I assumed an inner join but depending on your model and needs it could be a left join as well.

Related

Postgresql, inner join or subquery or view?

I have the following tables:
user
car
dealer
user_metrics
- user_id (FK) (required)
- dealer_id (FK) (can be null)
- car_id (FK) (can be null)
- saved
- .... other columns
A user can save a car or a dealership, when that happens the user_metrics.saved is set to true and the related car_id or dealership_id is set (car_id and dealership_id are exclusive, only one is set for a row).
I want user A to be able to see all users that have saved the same cars / dealerships.
So, if user A has saved car 1, 2,3 and dealership 5,7, I want to get all users that have saved any of those cars / dealerships.
I thought about inner join on user_metrics, but, I am not sure how to write the entire query that would deliver on this.
What query would allow me to get all users that have saved any of the cars/dealerships a certain user has saved?
If I understand as well maybe the below query solve your problem.
First should find a list of user A has been reserved after that should search which of car or dealer used by another user
with user_saved_data as (
select um.*,
u.name,
...
from user_metrics um
inner join user u
on um.user_id = u.id
where um.saved = true
and u.id = $1 -- User id of user 'A' or any username (Or use other column for create custom condition)
)
select usd.name as current_reserved_user,
u.name as reserved_by_user,
d.*,
c.*
from user_metrics um
inner join user u on um.user_id = u.id
left join user_saved_data usd on usd.dealer_id notnull and usd.dealer_id = um.dealer_id
left join user_saved_data usd on usd.car_id notnull and usd.car_id = um.car_id
left join dealer d on um.dealer_id = d.id
left join car c on um.car_id = c.id

Get distinct row by primary key, but use value from another column

I'm trying to get the sum of the total time that was spent sending all emails within a campaign.
Because of the joins in my query I end up with the 'processing_time' column duplicated over many rows. So running sum(s.processing_time) as send_time will always over represent how long it took to run.
select
c.id,
c.sender,
c.subject,
count(*) as total_items,
count(distinct s.id) as sends,
sum(s.processing_time) as send_time,
from campaigns c
left join sends s on c.id = s.campaigns_id
left join opens o on s.id = o.sends_id
group by c.id;
I'd ideally like to do something like sum(s.processing_time when distinct s.id) but I can't quite work out how to achieve that.
I have made other attempts using case but I always run into the same issue, I need to get the distinct rows based on the ID column, but work with another column.
Since you want statistics related to distinct s.id as well as c.id, group by both columns. Collect the (intermediate) data that you need,
and use this table as the inner table in a nested sub-select query.
In the outer select, group by c.id alone.
Since the inner select groups by s.id, values which are unique per s.id will not get double-counted when you sum/group by c.id.
SELECT id
, sender
, subject
, sum(total_items) as total_items
, sum(sends) as sends
, sum(processing_time) as send_time
FROM (
SELECT
c.id
, s.id as sid
, count(*) as total_items
, 1 as sends
, s.processing_time
, c.sender
, c.subject
FROM campaigns c
LEFT JOIN sends s on c.id = s.campaigns_id
LEFT JOIN opens o on s.id = o.sends_id
GROUP BY c.id, c.sender, c.subject, s.processing_time, s.id) t
GROUP BY id, sender, subject
ORDER BY id
Since the final table includes sender and subject, you'll need to group by these columns as well to avoid an error such as:
ERROR: column "c.sender" must appear in the GROUP BY clause or be used in an aggregate function
LINE 14: , c.sender

Assistance with a duplication query

When a new customer contacts us, they are allocated a reference number.
Unfortunately our contact centre sometimes logs the same person without checking if they have contacted us before and the customer ends up with two reference numbers. We want to cleanse this, so:
I would like to output instances where the customer's surname, address1 and zipcode are duplicated but only if the customer has different reference numbers.
This is the type of data that I'd like to see output:
Ref LastName Address 1 Zip
1875 Faulkner 10 Smith Street 08540
1876 Faulkner 10 Smith Street 08540
I have tried a few ideas, the latest being (forgive the huge amount of code here):
with Duplicates as
(
select r.LastName
, a.Address1
, a.ZipCode
, COUNT(*) as DuplicateCount
FROM Reference r
INNER JOIN Address a ON a.ReferenceNumber = r.ReferenceNumber
LEFT OUTER JOIN Telephone t ON r.ReferenceNumber = t.ReferenceNumber
LEFT OUTER Join Email e ON r.ReferenceNumber = e.ReferenceNumber
group by r.LastName
, a.Address1
, a.ZipCode
having COUNT(*) > 1
)
SELECT
r.ReferenceNumber
, r.LastName
, r.FirstName
,a.ReferenceNumber
, a.Address1
, a.Address2
, a.Address3
, a.Address4
, a.ZipCode
,t.ReferenceNumber
, t.TelephoneNumber
,e.ReferenceNumber
, e.EmailAddress
, d.DuplicateCount
FROM Reference r
INNER JOIN Address a ON a.ReferenceNumber = r.ReferenceNumber
LEFT OUTER JOIN Telephone t ON r.ReferenceNumber = t.ReferenceNumber
LEFT OUTER Join Email e ON r.ReferenceNumber = e.ReferenceNumber
join Duplicates d on d.LastName = r.LastName
AND d.Address1 = a.Address1
AND d.ZipCode = a.ZipCode;
Unfortunately this returns all duplicates, not those with the same surname, address1 and zipcode and different reference numbers.
Do you have any advice on how I can achieve this?
Many thanks.
Try using this part of your code in a self join by putting the data into a table variable or using aliases.
SELECT
r.ReferenceNumber
, r.LastName
, r.FirstName
,a.ReferenceNumber
, a.Address1
, a.Address2
, a.Address3
, a.Address4
, a.ZipCode
,t.ReferenceNumber
, t.TelephoneNumber
,e.ReferenceNumber
, e.EmailAddress
, d.DuplicateCount
FROM Reference r
INNER JOIN Address a ON a.ReferenceNumber = r.ReferenceNumber
LEFT OUTER JOIN Telephone t ON r.ReferenceNumber = t.ReferenceNumber
LEFT OUTER Join Email e ON r.ReferenceNumber = e.ReferenceNumber
An example of self join is here.

sql joins The multi-part identifier could not be found

here`s my query
SELECT cont.FILTER_VALUE as filter,
o.[OBJECT_ID] as Id, o.[OBJECT_NAME] as Name, o.DESCRIPTION as Description, o.CREATED as Created,
o.MODIFIED as Modified, u.[LOGIN] as LastModifiedByLogin, o.[OBJECT_NAME] as ObjectName, t.[TEMPLATE_NAME] as TemplateName--,p.[PAGE_NAME] as PageName
FROM
[OBJECT] AS o
LEFT OUTER JOIN [CONTAINER] as cont
on cont.[OBJECT_ID] = o.[OBJECT_ID]
LEFT JOIN [OBJECT_VALUES] AS ov ON
ov.[OBJECT_ID] = o.[OBJECT_ID]
LEFT JOIN [PAGE] AS p ON o.[PAGE_ID] = p.[PAGE_ID]
INNER JOIN [USERS] as u on u.[USER_ID] = o.LAST_MODIFIED_BY INNER JOIN [PAGE_TEMPLATE] as t
on o.[PAGE_TEMPLATE_ID] = t.[PAGE_TEMPLATE_ID] INNER JOIN [site] as s on t.SITE_ID = s.SITE_ID
WHERE
s.SITE_ID = '34' --AND сont.[FILTER_VALUE] is null--like '%fff%'
And it works nice, until I remove the comment.
Here's a mess of joins, still it has sense. I inner join main table with couple of others, and left join with optional, so, that I have a column, that contains cont.FILTER_VALUE as filter, its null in some records, I can get it, but I cant filter by this field.
I get The multi-part identifier "сont.FILTER_VALUE" could not be bound.
I've looked through similar topics, but found no useful information. I don't use any old SQL dialects: everywhere I use INNER/LEFT joins, tried group by and order by, tried to re-order joins - nothing helped. I guess I just don't understand something important about joins, could you tell me, please.
Thanx.
if you wrote it like this:
s.SITE_ID = '34' AND сont.[FILTER_VALUE] is null like '%fff%'
its just wrong syntax. you're missing a column for the LIKE function (in which column does the query suppose to look for the pattern?). if you didnt write it like that, please post what you did write
I don't know why you're getting your error, but try this just in case, while we wait for a better answer:
SELECT FILTER_VALUE as filter
, o.[OBJECT_ID] as Id
, o.[OBJECT_NAME] as Name
, o.DESCRIPTION as Description
, o.CREATED as Created
, o.MODIFIED as Modified
, u.[LOGIN] as LastModifiedByLogin
, o.[OBJECT_NAME] as ObjectName
, t.[TEMPLATE_NAME] as TemplateName
FROM [OBJECT] AS o
INNER JOIN [USERS] as u
ON u.[USER_ID] = o.LAST_MODIFIED_BY
INNER JOIN [PAGE_TEMPLATE] as t
ON o.[PAGE_TEMPLATE_ID] = t.[PAGE_TEMPLATE_ID]
INNER JOIN [site] as s
ON t.SITE_ID = s.SITE_ID
LEFT JOIN [CONTAINER] as cont
ON cont.[OBJECT_ID] = o.[OBJECT_ID]
LEFT JOIN [OBJECT_VALUES] AS ov
ON ov.[OBJECT_ID] = o.[OBJECT_ID]
LEFT JOIN [PAGE] AS p
ON o.[PAGE_ID] = p.[PAGE_ID]
WHERE s.SITE_ID = '34' AND FILTER_VALUE IS NULL
Well, I solved this problem, using CTE, still I wonder, why did I have problems without cte.
This way it works good
with query_CTE
AS
(
SELECT
ROW_NUMBER() OVER (ORDER BY o.[OBJECT_ID] asc) as rowNum,
o.[OBJECT_ID] as Id, o.[OBJECT_NAME] as Name, o.DESCRIPTION as Description, o.CREATED as Created,
o.MODIFIED as Modified, u.[LOGIN] as LastModifiedByLogin, o.[OBJECT_NAME] as ObjectName, t.[TEMPLATE_NAME] as TemplateName,p.[PAGE_NAME] as PageName,
s.SITE_ID, t.PAGE_TEMPLATE_ID, p.PAGE_ID, ov.VARIABLE_NAME, ov.VARIABLE_VALUE, cont.FILTER_VALUE, cont.DYNAMIC_CONTENT_VARIABLE, cont.SELECT_START,
cont.SELECT_TOTAL
FROM [OBJECT] AS o
INNER JOIN [USERS] as u
ON u.[USER_ID] = o.LAST_MODIFIED_BY
INNER JOIN [PAGE_TEMPLATE] as t
ON o.[PAGE_TEMPLATE_ID] = t.[PAGE_TEMPLATE_ID]
INNER JOIN [site] as s
ON t.SITE_ID = s.SITE_ID
LEFT JOIN [CONTAINER] as cont
ON cont.[OBJECT_ID] = o.[OBJECT_ID]
LEFT JOIN [OBJECT_VALUES] AS ov
ON ov.[OBJECT_ID] = o.[OBJECT_ID]
LEFT JOIN [PAGE] AS p
ON o.[PAGE_ID] = p.[PAGE_ID]
)
select rowNum,
Id, Name,[Description], Created, Modified, LastModifiedByLogin, ObjectName, TemplateName,PageName
from query_CTE

MS Access INNER JOIN most recent entry

I'm having some trouble trying to get Microsoft Access 2007 to accept my SQL query but it keeps throwing syntax errors at me that don't help me correct the problem.
I have two tables, let's call them Customers and Orders for ease.
I need some customer details, but also a few details from the most recent order. I currently have a query like this:
SELECT c.ID, c.Name, c.Address, o.ID, o.Date, o.TotalPrice
FROM Customers c
INNER JOIN Orders o
ON c.ID = o.CustomerID
AND o.ID = (SELECT TOP 1 ID FROM Orders WHERE CustomerID = c.ID ORDER BY Date DESC)
To me, it appears valid, but Access keeps throwing 'syntax error's at me and when I hit OK, it selects a piece of the SQL text that doesn't even relate to it.
If I take the extra SELECT clause out it works but is obviously not what I need.
Any ideas?
You cannot use AND in that way in MS Access, change it to WHERE. In addition, you have two reserved words in your column (field) names - Name, Date. These should be enclosed in square brackets when not prefixed by a table name or alias, or better, renamed.
SELECT c.ID, c.Name, c.Address, o.ID, o.Date, o.TotalPrice
FROM Customers c
INNER JOIN Orders o
ON c.ID = o.CustomerID
WHERE o.ID = (
SELECT TOP 1 ID FROM Orders
WHERE CustomerID = c.ID ORDER BY [Date] DESC)
I worked out how to do it in Microsoft Access. You INNER JOIN on a pre-sorted sub-query. That way you don't have to do multiple ON conditions which aren't supported.
SELECT c.ID, c.Name, c.Address, o.OrderNo, o.OrderDate, o.TotalPrice
FROM Customers c
INNER JOIN (SELECT * FROM Orders ORDER BY OrderDate DESC) o
ON c.ID = o.CustomerID
How efficient this is another story, but it works...