Have 2 tables
User table with
user chat_id, kunnr (cust_code) & vkorg
Delivery table with
PO # and other delivery info, cust_code & sales_org
I want to verify if the user cust_code and sales_org match with shipmentdel's table
One user can belong to multiple sales org and a PO can have many delivery orders
Below is my working SQL statement, can this be simplified ?
SELECT * FROM ( SELECT a.* FROM ztelegram.ShipmentDel a
WHERE a.Cust_PO = 'WPO-01502') as t1
LEFT JOIN
( SELECT b.* FROM ztelegram.noti_setting b WHERE b.chat_id = '12345667' ) as t2
ON t1.sales_org = t2.vkorg AND t1.Sold_to_party_name = t2.kunnr
Without the need of t1 & t2, isnt the query working? Please check.
SELECT
*
FROM
ztelegram.ShipmentDel a
LEFT JOIN ztelegram.noti_setting b ON a.sales_org = b.vkorg AND a.Sold_to_party_name = b.kunnr
WHERE
a.Cust_PO = 'WPO-01502'
and b.chat_id = '12345667'
Related
What I was trying to do is to get data from multiple tables, supposed that I have the following results in my query:
The numbers in the column ticket_item_type represents certain table. For example, 2 is for tbl_company and 3 is for tbl_lease. Then the details represents the id of a certain record in that table.
Suppose that I want to get the title of those records using ticket_item_type and details. Is it possible to embed it to the results? Or should I make separate queries for each.
I know JOIN, but I is it only for single table?
Here's my MYSQL query for the image above:
SELECT *
FROM
(SELECT *
FROM ticket_items
WHERE hs_customer = 1
AND ticket IN
(SELECT id
FROM tickets
WHERE hs_customer='1'
AND ticket_status = 'dispatch_reviewed')
AND ticket IN
(SELECT ticket
FROM ticket_items
WHERE ticket_item_type = 5
AND details = '159')) AS TB1
WHERE ticket_item_type IN (3,
2,
8)
You could try something like this:
SELECT
TB1.*,
CASE
WHEN TB1.ticket_item_type = 2 THEN t2.title
WHEN TB1.ticket_item_type = 3 THEN t3.title
WHEN TB1.ticket_item_type = 8 THEN t8.title
ELSE 'NA'
END as title
FROM
(
SELECT *
FROM ticket_items
WHERE hs_customer = 1
AND ticket IN (SELECT id FROM tickets WHERE hs_customer='1' AND ticket_status = 'dispatch_reviewed')
AND ticket IN (SELECT ticket FROM ticket_items WHERE ticket_item_type = 5 AND details = '159')
) AS TB1
LEFT JOIN tbl_company t2 ON TB1.details = t2.id
LEFT JOIN tbl_lease t3 ON TB1.details = t3.id
LEFT JOIN tbl_next t8 ON TB1.details = t8.id
WHERE ticket_item_type IN (3, 2, 8)
However, this is not a design that I would prefer. Without looking at details of your database it's going to be hard to write a query to cover multiple types of ticket_item_type. I hope this query works for you, though.
I want summary data from my 'Data' table for all companies in 'Companies' table including blank rows where there is no record in Data.
If I summarise the data in a nested SELECT clause (or in a stored query i get nothing from the data table. For example
This is the sub select
SELECT transco,
sum(m1) AS Jan15,
FROM data
WHERE (QVmeasure = 'Vol')
GROUP BY QVmeasure, transco
which outputs:
transco Jan15
0292 154373665
1419 134915098
If I use it in a sub select as follows
SELECT c.SAP_Code,
Jan15
FROM Companies AS c
LEFT JOIN (
SELECT transco,
sum(m1) as Jan15
FROM data
WHERE (QVmeasure = 'Vol')
GROUP BY QVmeasure, transco)
AS d
ON c.SAP_Code = d.transco
I get:
SAP_Code Jan15
0292
1419
I can get the correct result via a temporary table:
SELECT sum(m1) as Jan15,
transco
INTO Temp_Table
FROM data
WHERE (QVmeasure = 'Vol')
GROUP BY QVmeasure, transco
followed by
Select c.SAP_code,
jan15
FROM companies AS c
LEFT JOIN Temp_Table as i
ON (c.SAP_Code = i.transco)
giving:
SAP_code jan15
0292 154373665
1419 134915098
1423
but if I use temporary tables I will have to create macros and i want users to be able to run just a query.
The following works for this simple case but I can't apply it in other circumstances:
SELECT c.SAP_Code,
sum(m1) AS Jan15
FROM Companies AS c
LEFT JOIN data as d
ON c.SAP_Code = d.transco
WHERE (d.QVmeasure = 'Vol') OR (d.QVmeasure is null)
GROUP BY d.QVmeasure,c.sap_code
Is there something wrong with my sub select syntax or is it ACCESS (2013)
TIA
You could nest the sub-query like this:
SELECT c.SAP_Code,
(SELECT sum(d.m1)
FROM data AS d
WHERE d.QVmeasure = 'Vol' AND c.SAP_Code = d.transco
) AS [Jan15]
FROM Companies AS c
Hello I want to get a specific userid who enter specific data. When X user enter data and save h will be the last row. But How I take the last row of exemple my user id = 16. Mnay user can insert data after him but I want only the last row of my user id 16.
SELECT *
from
projetstaches
inner join
timesheets
on (timesheets.timId = projetstaches.prtTimeSheetId)
inner join
users
on (users.usrId = timesheets.timUserId)
WHERE
users.usrId = 16 AND `prtTimeSheetId` = (
SELECT MAX( `prtTimeSheetId` )
FROM projetstaches )
When I do something like this it's return blank cause the last user who entered something is 7.
So how do I retrieve my last user = 16?
Here what look my table ( this is a old ver but good to know how it's look like the data now is more completed but the name of table its the same)
http://pastebin.com/6LBwGtc3
The subquery needs to use a join to select the highest prtTimeSheetId for this user, not the entire table.
SELECT *
from
projetstaches
inner join
timesheets
on (timesheets.timId = projetstaches.prtTimeSheetId)
inner join
users
on (users.usrId = timesheets.timUserId)
WHERE
users.usrId = 16
AND prtTimeSheetId = (
SELECT MAX(preTimeSheetId)
from
projetstaches
inner join
timesheets
on (timesheets.timId = projetstaches.prtTimeSheetId)
inner join
users
on (users.usrId = timesheets.timUserId)
WHERE users.usrId = 16)
I have 3 tables
For instance
Salestable
-ID
-variableB
-customerUUID
-variableC
Customertable
-customerUUID
-contractUUID
Contracttable
-contractUUID
-variableD
So I am currently doing a SQL Query on salestable
Like:
SELECT DISTINCT variableB FROM Salestable WHERE variableD = "blah";
How can I do this? Where I can find the contract associated with the current salestable?
A bit more info
They are all a 1:1 relationship - so Contracttable is tied to 1 Customertable which is tied to 1 salestable
There is a LOT of data in my database thousands of entries - this query does not run constantly but does need to run somewhat efficent.
SELECT a.*
FROM SalesTable a
INNER JOIN CustomerTable b
ON a.customerUUID = b.customerUUID
INNER JOIN Contracttable c
ON b.contractUUID = c.contractUUID
WHERE c.variableD = 'valueHere'
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
If you sure the contract exists use this (othewise swap INNER FOR LEFT):
SELECT variableB, variableD
FROM Salestable t1
INNER JOIN Customertable t2 ON (t1.customerUUID = t2.customerUUID)
INNER JOIN Contracttable t3 ON (t3.contractUUID = t2.contractUUID)
WHERE variableD = "blah"
GROUP BY t1.variableB
I have the logic worked out, just not sure how to best write this query.
the logic is
we have a deal ID of 1
a deal is linked to multiple regions
a deal is linked to multiple interests
a user is linked to multiple regions
a user is linked to multiple interests
we want all users where....
the user is linked to the same region as a deal
userRegionLink url, dealRegionLink drl
url.regionId is in drl.regionId where drl.dealId = 1
the user is linked to the same interest as a deal
userInterestLink uil, dealInterestLink dil
uil.interestId is in dil.interestId where dil.dealId = 1
this would give us a list of the users
now we need to select distinct from the list so we only end up sending each user a single email
But I have no idea what the best way to write this query would be.
We are dealing with a few tables here
We have
users which has all the user Information in it userId and other columns not important
userInterestLink which has userId and interestId
dealInterestLink which has dealId and interestId
userRegionLink which has userId and regionId
dealRegionLink which has dealId and regionId
so what we are wanting in the end is all the user info which matches.
I take RC's answer and modify it
SELECT u.userId, uil.interestId, url.regionId FROM users u
JOIN userInterestLink uil ON (uil.userId = u.userId)
JOIN userRegionLink url ON (url.userId = u.userId)
WHERE interestId IN (
SELECT DISTINCT interestId FROM dealInterestLink WHERE dealId = 1
) AND regionId IN (
SELECT DISTINCT regionId FROM dealRegionLink WHERE dealId = 1
)
as there is no need for LEFT JOIN if I exclude the NULL rows afterwards.
A more "symmetric" version without subqueries and with USING would be
SELECT u.userId, uil.interestId, url.regionId FROM users u
JOIN userInterestLink uil USING (userId)
JOIN userRegionLink url USING (userId)
JOIN dealInterestLink dil USING (interestId)
JOIN dealRegionLink drl USING (regionId, dealId)
WHERE dealId = 1
Untested as well.
Something like:
SELECT u.userId, uil.interestId, url.regionId FROM users u
LEFT JOIN userInterestLink uil ON (uil.userId = u.userId)
LEFT JOIN userRegionLink url ON (url.userId = u.userId)
WHERE uil.interestId IS NOT NULL AND uil.interestId IN (
SELECT DISTINCT interestId FROM dealInterestLink WHERE dealId = 1
) AND url.regionId IS NOT NULL AND url.regionId IN (
SELECT DISTINCT regionId FROM dealRegionLink WHERE dealId = 1
)
? If result is OK, you can then SELECT DISTINCT u.userId FROM users u -- ...
(not tested)
SELECT `u`.*
FROM `users` AS `u`
JOIN `userRegionLink` `userReg` USING ( `userId` )
JOIN `userInterestLink` `userInt` USING ( `userId` )
JOIN `dealInterestLink` `dealInt` USING ( `interestId` )
JOIN `dealRegionLink` `dealReg` USING ( `regionId` )
JOIN `deal` `d` ON ( `dealInt`.`dealId` && `dealReg`.`dealId` && `d`.`dealId` = 1 )
GROUP BY `u`.`userId`
Tested locally using dummy data and presumed schema. Worked OK.