I am trying to get this documents (sales orders) in SAP Business One, those that are not forwarded to Delivery or those that have no PO.
I have tried this:
SELECT * FROM ORDR T0 INNER JOIN RDR1 T1 ON T0.DocEntry=T1.Docentry
WHERE T1.TrgtEntry IS NULL AND T1.BaseEntry IS NULL.
The problem is, we allow partial delivery of sales orders. So if sales order has 10 items and 5 of these items are partially delivered. This kind of documents are included in my query.
What can i do? I though this is easy but it's making me crazy. If only the header document can identify if it already have linking tables.
Thank you.
SAP B1 DB Schema
**POR1 child table of purchase order (OPOR)
**DLN1 child table of Delivery (ODLN)
DocEntry is the relationship between Header(ORDR) and child(RDR1)
BaseEntry is the relationship between child(POR1) to child(RDR1)
TrgtEntry is the relationship between child(RDR1) to child(DLN1)
Haven't tested this, included the delivery lines table. See if it suits you.
SELECT * FROM ORDR T0
INNER JOIN RDR1 T1 ON T0.DocEntry=T1.DocEntry
LEFT JOIN DLN1 T2 ON T2.BaseEntry = T1.DocEntry
WHERE T1.TrgetEntry IS NULL AND T1.BaseEntry IS NULL AND T2.BaseEntry IS NULL
Related
I am using the Chinook database for a project and I have two difficult queries to execute, but both provide errors.
I am looking for all the orders (invoice) that were sent to 'New York' and contain tracks that belong to more than one genre. [InvoiceId, amount of products, total1, total2]. Total1 should be unitprice*quantity and total2 is total. It should show only 2 rows.
So far I have come up with this. I have also tried switching up with left join, full outer join, etc
CREATE TEMPORARY TABLE temp AS
SELECT *
FROM track join invoiceline USING (TrackId)
WHERE (select * from track t1 where EXISTS (select * from track t2 where t1.GenreId <> t2.GenreId));
SELECT invoice.InvoiceId, invoiceline.Quantity, invoiceline.UnitPrice*invoiceline.Quantity, invoice.Total
FROM (SELECT * FROM invoice JOIN invoiceline
WHERE invoice.BillingCity LIKE '%New York%') JOIN temp cc ON invoiceline.TrackId
GROUP BY invoiceline.InvoiceId;
DROP TABLE temp;
It provides the error:
Operand should contain 1 column(s)
I am looking for clients (in couples) that have bought more than two of the same tracks. It should provide 14 rows.
Until now I have come up with this.
SELECT CONCAT(FIRSTNAME,',', LASTNAME) AS name1 FROM customer
JOIN invoice ON customer.CustomerId = invoice.CustomerId
JOIN invoiceline ON invoice.InvoiceId = invoiceline.InvoiceId
JOIN track ON invoiceline.TrackId = track.TrackId
UNION
(
SELECT CONCAT(FIRSTNAME,',', LASTNAME) AS name2 FROM customer
JOIN invoice ON customer.CustomerId = invoice.CustomerId
JOIN invoiceline ON invoice.InvoiceId = invoiceline.InvoiceId
JOIN track ON invoiceline.TrackId = track.TrackId
);
So A) Does anybody know why it provides that error?
B) Could anyone give any tips or suggest a better way to write these queries?
Here are two helpful schemas:ER diagram
relational diagram
Answer to you first question:
The error comes up because many rows would have a single genre id. This method is also very redundant.
You should use count of genre Ids and take track Ids with count more than 1 as shown below:
CREATE TEMPORARY TABLE temp AS
SELECT *
FROM track join invoiceline USING (TrackId)
WHERE TrackId in
(select TrackId from (select TrackId, count(distinct GenreId) as genres from track group by 1 having genres>1));
SELECT invoice.InvoiceId, invoiceline.Quantity, invoiceline.UnitPrice*invoiceline.Quantity, invoice.Total
FROM (SELECT * FROM invoice JOIN invoiceline
WHERE invoice.BillingCity LIKE '%New York%') JOIN temp cc ON invoiceline.TrackId
GROUP BY invoiceline.InvoiceId;
DROP TABLE temp;
I have assumed that track id is the primary key here.
For the second question, I assume that you want to find customers buying the same records. You can use a query like the one below:
SELECT invoiceline.TrackId, group_concat(customer.CustomerId) as customers FROM customer
JOIN invoice ON customer.CustomerId = invoice.CustomerId
JOIN invoiceline ON invoice.InvoiceId = invoiceline.InvoiceId
JOIN track ON invoiceline.TrackId = track.TrackId
group by 1
This will give you comma separated customer ids who have bought the same track. Also, use customer id instead of first name and last name since some customers can have the same name. Using primary key is best.
Since you mentioned, you want customers buying the same records in couples, I would suggest reading up on market basket analysis or association analysis using apriori algorithm. You can import your dataset into R or Python whichever you are comfortable with and build a visualization. Python is faster and can handle more data but its visualizations are bad. R is a bit slow at handling large amounts of data but has good visualizations for apriori algorithm
I have a database that contains the following tables I am concerned with.
JobAreas (Base table for which I want to query other tables)
JobSkills (Every Job Skill belongs to a Job Area via foreign key i.e. parent_id)
Jobs (Every job must belong to a Job Area via foreign key i.e. category_id)
UserSkills (This table contains the JobSkill that is related to a Job Area)
I am attaching the table structures.
I am trying to create a SQL query that can give me the number of skills, number of jobs and number of people for various Job Areas. Though calculating Users who offer services in a particular Job Area appears to be tough because it is connected indirectly. I tried to get Number of Skills and Number of Jobs for all Job Areas using the following query.
select
t.id,
t.title,
count(s.parent_id) as skillsCount,
count(m.category_id) as jobCount
from
job_areas t
left join skill_types s ON s.parent_id = t.id
left join job_requests m ON m.category_id = t.id
group by
t.id
But it is not giving the correct data. Can someone guide me in right direction on how to achieve this.
You are joining along different dimensions. The quick-and-dirty way to fix this is to use count(distinct):
select t.id, t.title,
count(distinct s.parent_id) as skillsCount,
count(distinct m.category_id) as jobCount
from job_areas t left join
skill_types s
ON s.parent_id = t.id left join
job_requests m
ON m.category_id = t.id
group by t.id;
This works fine if there are just a handful of skills and categories for each job. If there are many, you want to pre-aggregate before the join or use correlated subqueries.
Context
I have been fiddling with a small fooseball hobby database to keep track of matches, players and goals. And came across a problem i don't quite know how to fix.
The match table has two foreign keys both pointing to tID in the team table.
The thought was that i later would be able to do a SELECT to see what teams (by name) played against eachother in a given match.
select * from `Fooseball`.`match`
INNER JOIN team T1
ON Fooseball.`match`.mHome_Team = T1.tID
INNER JOIN team T2
ON Fooseball.`match`.mAway_Team = T2.tID
WHERE mID=1
Question
1 Is their a better way to archive this, than creating two primary keys. Like, an intermediate table?
2 How can i construct my select statement so i can name the tName columns as "home" and "away" or something else? When i try and say
INNER JOIN team AS T1
Nothing changes.
Unstated additional requirements notwithstanding, this is pretty much how I would do it.
To rename columns in the result, you would do something like
SELECT m.mDate AS match_date, T1.tName AS home_team, T2.tName AS away_team
FROM Fooseball.`match` m
INNER JOIN team T1
ON m.mHome_Team = T1.tID
INNER JOIN team T2
ON m.mAway_Team = T2.tID
WHERE mID=1
For reporting, you can alias your columns with mixed case and spaces (eg. "Home Team") by enclosing the alias in double quotes.
I have a little dilemma in joining data from 4 tables in one SQL query, I am using MySQL for the DB part and would appriciate any help you can give me.
Here is the task...
I have for tables with columns and data
Sale Items Owner Salesman
-------------- ----------- ----------- --------------
*Salesman_id Item_type *Owner_id *Salesman_id
*Owner_id Item_color Owner_name Salesman_name
Buyer_id *Owner_id
Price
I want to query these tables on the columns I have marked with emphases text. So I can get result like
Item type, Item color, Owner name, Salesman name, Salesman number.
I have gone through a countless number of iteration trying to achieve this both with JOIN and nested queries without sufficient result.
If there is a one-to-one relation you can use inner join
SELECT i.Item_type , i.Item_color ,o.Owner_name,sm.Salesman_name,sm.Salesman_id
FROM Salesman sm
INNER JOIN Sale s ON (s.Salesman_id = sm.Salesman_id )
INNER JOIN Owner o ON (s.Owner_id=o.Owner_id)
INNER JOIN Items i ON (i.Owner_id=o.Owner_id)
If there is one -to- many try with Left join
try this
SELECT Item_type, Item_color, Owner_name, Salesman_name, Sale.Salesman_id FROM Items
INNER JOIN Owner USING(Owner_id)
INNER JOIN Sale USING(Owner_id)
INNER JOIN Salesman ON Salesman.Salesman_id=Sale.Salesman_id
why doesn't the Items table have a primary key?
A solution when we're not joining, and you want it them to just display their values, you can do something like (I know this doesn't directly answer OP's question, but I'm getting there...)
SELECT sale.`salesman_id`,sale.`owner_id`,
items.`order_id`,
owner.`owner_id`,
salesman.`salesman_id`
FROM `Sale` sale,
`Items` items,
`Owner` owner,
`Salesman` salesman
And that should return everything.
However, your question states that we are joining. Could you put some data into something like SQLFiddle so I have some visual representation? and a brief summary of what you're trying to accomplish - as in where you want the joins?
Much more to it but there are 3 tables...
employees
employee_id
licenses
license_id
employee_id
companies
company_id
employee_id
SELECT * FROM employees
LEFT JOIN licenses
ON employees.employee_id = licenses.employee_id
LEFT JOIN companies
ON employees.employee_id = companies.employee_id
GROUP BY employees.employee_id
I can only get it so either licenses OR companies returns a value and the other returns NULL. How can I get it so BOTH tables return values? Seems so easy but it isn't working in this case and I can't figure out why...
EDIT: Here is some more info.
Not every employee has a license.
Not every employee has a company.
Would like to return employee_id license_id (if exists, else NULL) company_id (if exists, else NULL)
Take the case where an employee has both a license_id and a company_id. By removing one of the JOIN clauses, can return the corresponding value. However, when both are combined, only return the company_id and license_id returns NULL.
Weird, right? Any ideas or is more info needed?
DATA:
employee_id
1
2
3
employee_id license_id
1 1
2 1
3 2
employee_id company_id
1 1
2 1
3 2
SORRY FOR WASTING TIME
The table schema is screwed up and redundant. This was an inherited schema and I was just considering the SQL, not the underlying structure. Database needs restructuring.
This is very difficult to answer without seeing the data in the tables. So I'll make the assumption, that there are rows in each table that all have a single Employee_ID that is the same, so the joins work. While your testing this I would suggest picking one Employee_id to work with too, just to simplify the output while you test.
Based on my assumptions, I switched your queries to inner joins, this will only show rows that match on the Employee_id. I also used "aliasing". The single letter I put after each table pointer saves a lot of typing.
SELECT *
FROM employees e
INNER JOIN licenses l
ON e.employee_id = l.employee_id
INNER JOIN companies
ON e.employee_id = c.employee_id
GROUP BY employee_id
If you're new to SQL joins, this article may be helpful too. Best of luck!
There are alot of different JOIN types. My suggestion would be to do some reading about them to figure it out. You can use a FULL OUTER JOIN
SELECT *
FROM employees
FULL OUTER JOIN licenses
ON employees.employee_id = licenses.employee_id
FULL OUTER JOIN companies
ON employees.employee_id = companies.employee_id
GROUP BY employee_id
It is difficult to figure exactly what you want without seeing any data. But here is a handy visual explanation of SQL Joins. I keep it bookmarked for those times that I need some help.
http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html
This is my favorite article on the subject.
http://www.codeproject.com/KB/database/Visual_SQL_Joins.aspx
Try
FULL OUTER JOIN