I want to get a result from three tables. i dont how to write the sql query. Please help me.
"I want to display Name,Username and Product_Name where Id=007"
table "register"
Name Username Id
Arj arjun 007
xyz abcd 008
abcd asdf 007
table "products"
Product_Id Product_Name Price
101 Clothes 200
102 Games 100
table "purchase" //products.Product_Id=purchase.Item
Username item Id
arjun 102 007
abcd 101 008
asdf 102 007
Try this query :
SELECT a.Name,a.Username,c.Product_Name
FROM register as a
JOIN purchase as b on a.Username=b.Username
JOIN products as c on b.item=c.Product_Id
It should work like this. To read more on SQL Joins try : http://www.w3schools.com/sql/sql_join.asp
Enjoy!
That should be like this
SELECT * FROM
register r
JOIN purchase p on p.username = r.username
JOIN products pr on pr.product_id = p.item
you can revise this basic code to include specific columns and add where and order clause.
SELECT Username, Id, Product_Name
FROM register
INNER JOIN purchase ON purchase.Id = register.Id
INNER JOIN products ON purchase.Item = products.Product_Id
WHERE Id = '007'
As a side note: you shouldn't save the Username in the purchase table too.
Related
I have the following example table structure. Want to retrieve customer who didnt have notificationtypeid = 2 ie who have never been sent a reminder email.
Can anyone help me how to write the sql query for this?
CustomerNotification:
-------------------
CustomerId Notificationtypeid
8201 1
8201 2
8202 1
8203 1
10209 1
Customer:
--------
CustomerId
8201
8202
8203
10209
NotificationType:
----------------
Notificationtypeid Name
1 Invite email
2 Reminder
i tried the following but not working as i am sure this not how it should be written :
select cd.customerId
tn.notificationtypeid from Customer cd
inner join CustomerNotification tn
on cd.customerid= tn.customerid
where cd.customerid not in( select customrid from customnotification where notificationtypeid=2
)
order by cd.customerid
Expected Result:
CustomerId Notificationtypeid
8202 1
8203 1
10209 1
But using my query i get:
CustomerId Notificationtypeid
8201 1 <-- I dont want this in result
8202 1
8203 1
10209 1
select cd.customerId
tn.notificationtypeid from Customer cd
inner join CustomerNotification tn
on cd.customerid= tn.customerid
where cd.customerid not in( select customrid from customnotification where notificationtypeid=2)
This query solved my issue. Let me know if there is better way.
You can try this SQL Server query to get desired output:-
select CustomerId, Notificationtypeid from CustomerNotification
where Notificationtypeid =1
I have an issue grabbing data from two tables base on a single field in one of the tables The field does not exist in both tables.
Example:
Table 1 (call it invoices):
invoice email customer_name original_bill invoice_status
1 a#g.com bob 5.00 P
2 a#g.com harry 23.00 P
3 a#g.com sally 4.00 P
4 b#g.com loretta 14.00 P
5 b#g.com hamish 74.00 P
Table 2 (customer invoice edits):
invoice email date_last_modified_timestamp mod_status mod_amount
1 a#g.com 2019-05-01 A 3.00
1 a#g.com 2019-04-01 D
3 b#g.com 2019-10-25 A
What I want
a list of all invioces and their mods but I only wish each invoice to appear once in the list. based on the latest date in Table two.
example:
invoice email customer_name original_bill invoice_status date_last_modified_timestamp mod_status mod_amount
1 a#g.com bob 5.00 P 2019-05-01 A 3.00
2 a#g.com harry 23.00 P
3 a#g.com sally 4.00 P 2019-10-25 A
4 b#g.com loretta 14.00 P
5 b#g.com hamish 74.00 P
How im pulling it now:
select invoice, email, customer_name, original bill, invoice status, max(date_last_modified_timestamp), mod status, mod_amount
from table one
left join on table1.invoice = table 2.invoice
group by
invoice, email, customer_name, original bill, invoice status, max(date_last_modified_timestamp), mod status, mod_amount
I've tried a gazzillion variation to no avail.
What i get
i do get accurate results, but those results inclues a duplicate row containing each invoice that has been modified. I only want one invoice per row. I want the one that's been modified last. Is this even possible? What am I doing wrong?
You can join and use a correlated subquery for filtering:
select i.*, e.*
from invoices i
inner join customer_invoice_edits e
on e.invoice = i.invoice
and e.date_last_modified_timestamp = (
select max(date_last_modified_timestamp)
from customer_invoice_edits e1
where e1.invoice = e.invoice
)
This will give you one record per row in the invoices table that has a match in customer_invoice_edits, along with the latest corresponding record in customer_invoice_edits.
If some invoices have no corresponding record in the dependant table, they will not appear in the resultset. If you do want to see them, then you can use a left join instead.
Is this what you want?
select
invoice,
email
from (
select
invoice,
email,
mod_status,
mod_amount,
date_last_modified_timestamp,
row_number() over (
partition by invoice, email
order by date_last_modified_timestamp desc
) rn
from table2
)
where rn=1
I'm pretty sure you want this field-by-field. Here is an example:
select i.invoice,
nz( (select top (1)
from invoice_edits as ie
where ie.invoice = i.invoice and ie.email is not null
), i.email
) as email,
nz( (select top (1)
from invoice_edits as ie
where ie.invoice = i.invoice and ie.mod_amount is not null
), i.original_bill
) as original_bill,
. . .
from invoices as i;
I have a view that returns results of a query. The output looks something like this:
ORDER_ID DESC Package_Route_ID
123 ABC 212
456 XYZ 175
I have another table that has full order ID for some of the ORDER_IDs. The table looks like following:
ORDER_ID FULL_ORDER_ID
456 45678
So I want to display FULL_ORDER_ID if ORDER_ID from both the results matches or else it should return ORDER_ID from view.
Desired output:
ORDER_ID DESC Package_Route_ID
123 ABC 212
45678 XYZ 175
You just need left outer join
select isnull(od.FULL_ORDER_ID, v.ORDER_ID) ORDER_ID, v.[DESC], v.Package_Route_ID
from view v
left join ordertable od on od.ORDER_ID = v.ORDER_ID;
Ok, this seems like it should be an easy one so I will try to simplify what I am trying to do.
Say I have this select statement:
SELECT a.customer, b.fruit
FROM Customer a
INNER JOIN Order b ON a.customer = b.Customer
Which would return the following:
Customer Fruit
-------------------------
Jane Apple
Jane Banana
Bob Apple
Bob Orange
Bob Grape
John Apple
John Banana
Ann Tangerine
Ann Orange
Ann Banana
What I would like to pull from this result set is a list of customers who have never ordered, say, a 'Tangerine', resulting in the following list:
Customer
--------
Jane
Bob
John
How would one go about do this? Thanks in advance!
There are many ways to do this:
SELECT *
FROM Customer c
WHERE NOT EXISTS(SELECT 1 FROM Order
WHERE customer = c.customer
And Fruit = 'Tangerine');
Using NOT IN:
SELECT *
FROM Customer c
WHERE c.Customer NOT IN (SELECT Customer FROM Order
WHERE Customer IS NOT NULL
And Fruit = 'Tangerine');
Here is one method using left join:
SELECT c.customer
FROM Customer c LEFT JOIN
Order o
ON c.customer = o.Customer AND o.fruit = 'Tangerine'
WHERE o.Customer IS NULL;
Existing answers are missing distinct customers
SELECT distinct c.customer
FROM Customer c
WHERE NOT EXISTS ( SELECT 1
FROM Customer T
WHERE T.customer = c.customer
And T.Fruit = 'Tangerine'
);
And I don't like tables and column the same
That would be more and Order table
good day, i have these 3 tables...i.e.;
customer table
cust_id cust_name sales_employee
1 abc 1
2 cde 1
3 efg 2
transaction table
order_num cust_id sales_employee
1001 1 1
1002 2 2
sales_employee table
sales_employee employee name
1 john doe
2 jane doe
how can i show the employee name on both customer table and transaction table?
notice how the sales_employee can change per transaction, it does not necessarily have to be the same per customer.
please help.
To select customers with sales person name
select
C.*, E.employee_name
from
Customers as C
inner join Sales_Employees as E on E.sales_employee = C.sales_employee
To select transactions with customer name and salesperson name (at the point in time of the transaction)
select
T.*,
E.employee_name as Trans_employee,
C.cust_name,
EC.employee_name as Cust_employee
from
Transactions as T
inner join Sales_Employees as E on E.sales_employee = T.sales_employee
inner join Customers as C on C.cust_id= T.cust_id
inner join Sales_Employees as EC on EC.sales_employee = C.sales_employee
This code is meant to guide you, you will need to adjust it to match your table and field names.