SQL commands with subtraction and multiplication

In summary, you are asked to write general SQL statements that can be applied to other data with the same format. You should also be able to replace the given input, "Lagerreorganisation", with another project name and still get the desired output. Additionally, it is important to use primary keys in your queries, such as the combination of mnr and kompetenz for mitarb, and to not hardcode specific values, such as projnr 'p17'.
  • #1
mathmari
Gold Member
MHB
5,049
7
Hey! :giggle:

The following relations of a project administration of a company are given, where the primary key of the respective relations are underlined.
1643099717292.png


An employee can be assigned to several projects. Furthermore, an employee can have different competencies, which are billed at different daily rates. Each employee can do a task only on a daily basis. The assignment of employees to projects is contained in the relationship projektzuord. The relations mitarb und projekte contain the master data for the existing employees and projects.

1643099741392.png


1643099772008.png


1643099795899.png


In order to solve the tasks in the following, enter general SQL statements. The commands of the task should be as generally as possible, i.e. they should also be applicable to other data of the same form. Hint: You can calculate with the data type DATE like with an INTEGER

1) Which different employees (first name and last name) are assigned to the project with the name Lagerreorganisation ?
2) Which different employees (first name and last name) generate the most sales? Give a sorted list. The turnover is calculated from the number of planned days multiplied by the daily rate.
3) Which competencies (designation of competency) will be needed at least twice in the planned projects ?
4) How many employees with which skills are available in the period from 03/15/2022 to 04/09/2022, that is, not assigned to other projects?
5) There is a new project p42 called Datenreorganisation of customer kn99 and a Volume of 12000 euros. The employee Maier will be responsible for this new project from 01/03/2022 to 31/05/2022. Give SQL commands to update the database.
I have done the following :1)
SELECT DISTINCT m.vorname, m.name
FROM mitarb m, projektzuord z
WHERE m.mnr = z.mitarbnr AND z.projnr = 'p17'Is that correct ? :unsure:2)
SELECT m.vorname, m.name, MAX(DATEDIFF(day, z.datumbis, z.datumvon) * m.tagesastz)
FROM mitarb m, projektzuord z
WHERE m.mnr = z.mitarbnr AND m.kompetenz = z.kompetenzIs that the correct way to check the employees with the most sales? :unsure:
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
At (3) I have done :

SELECT p.projektbez
FROM projektzuord z, projekte p
GROUP BY p.projektbez, p.projnr
HAVING COUNT(p.projektbez)>=2 AND p.projnr = ANY (SELECT z.projnr FROM projektzuord z)I think it is correct! :unsure:
At (4) I have done :

SELECT COUNT(m.mnr), m.kompetenz
FROM projektzuord z, mitarb m
WHERE ((z.datumbis < '20220315' OR z.datumvon > '20220409') AND (m.mnr = z.mitarbnr)) OR m.mnr != ALL (SELECT z.mitarbnr FROM projektzuord z)
GROUP BY m.mnr, m.kompetenzI don't get a result. Is that correct, i.e. in this period there is not available emplyee, is there? :unsure:
 
  • #3
At (2) I have done :

SELECT m.vorname, m.name, MAX(DATEDIFF(day, z.datumbis, z.datumvon)*m.tagessatz)
FROM mitarb m, projektzuord z
WHERE m.mnr = z.mitarbnr AND m.kompetenz = z.kompetenz
GROUP BY m.vorname, m.name
ORDER BY MAX(DATEDIFF(day, z.datumbis, z.datumvon)*m.tagessatz) But "DATEDIFF" is not working. How do we use that corrctly ? :unsure:
 
  • #4
mathmari said:
In order to solve the tasks in the following, enter general SQL statements. The commands of the task should be as generally as possible, i.e. they should also be applicable to other data of the same form. Hint: You can calculate with the data type DATE like with an INTEGER

1) Which different employees (first name and last name) are assigned to the project with the name Lagerreorganisation ?

SELECT DISTINCT m.vorname, m.name
FROM mitarb m, projektzuord z
WHERE m.mnr = z.mitarbnr AND z.projnr = 'p17'

Hey mathmari!

You have matched the employee on the column "mnr", but that is not a primary key is it?
That is, it does not uniquely identify an employee. (Worried)

Furthermore, it "should also be applicable to other data of the same form".
Suppose we want want to use it for "Facilitymanagement" instead of "Lagerreorganisation", then the query won't work, will it? (Worried)
 
  • #5
Klaas van Aarsen said:
You have matched the employee on the column "mnr", but that is not a primary key is it?
That is, it does not uniquely identify an employee. (Worried)

"mnr" is a primary kry for mitarb, it is underlined. :unsure:
Klaas van Aarsen said:
Furthermore, it "should also be applicable to other data of the same form".
Suppose we want want to use it for "Facilitymanagement" instead of "Lagerreorganisation", then the query won't work, will it? (Worried)

This one was also unclear to me. What does this mean that it should be also be applicable to other data of the same form? How can it be general ? :unsure:
 
  • #6
mathmari said:
"mnr" is a primary kry for mitarb, it is underlined.

There is only one primary key in a table, which is a combination of 1 or more columns. 🧐
In this case the primary key should consist of the combination of mnr and kompetenz.
To be fair, it does look a bit strange that they made kompetenz part of the primary key, but that is what it says.

Consider the table projectzuord. It has projnr underlined, but that is not a primary key on its own is it?
Any projnr will definitely be duplicated in multiple rows as there will usually be more than 1 employee that is assigned to a project. 🤔
mathmari said:
This one was also unclear to me. What does this mean that it should be also be applicable to other data of the same form? How can it be general ?
The question has a desired output, and a given input.
The given input is "Lagerreorganisation". To make it applicable to other data of the same form, we should be able to replace 'Lagerreorganisation' by some other project name and then the query should still give the desired output. Since you have hardcoded the projnr 'p17', that won't work. 🤔
 
Last edited:
  • #7
Klaas van Aarsen said:
The question has a desired output, and a given input.
The given input is "Lagerreorganisation". To make it applicable to other data of the same form, we should be able to replace 'Lagerreorganisation' by some other project name and then the query should still give the desired output. Since you have hardcoded the projnr 'p17', that won't work. 🤔

How do we give that "Lagerreorganisation" as input ? Do we set a variable equal to that befor we write SELECT ? :unsure:
 
  • #8
mathmari said:
How do we give that "Lagerreorganisation" as input ? Do we set a variable equal to that befor we write SELECT ?
There are multiple ways to do it.
If I'm not mistaken, we can use a nested select in the WHERE clause. Or a nested select in the FROM clause. Or a WITH clause. Or we can use a VIEW that we create beforehand. Or instead, we can also just use another table in the query. 🤔
 
  • #9
Klaas van Aarsen said:
If I'm not mistaken, we can use a nested select in the WHERE clause.

You mean to write WHERE "Lagerreorganisation" is from the given table? Or how do you mean that? I haven't really understood that. :unsure:
 
  • #11
Klaas van Aarsen said:

So do you mean it as follows ?

SELECT DISTINCT m.vorname, m.name
FROM mitarb m, projektzuord z, projekte p
WHERE m.mnr = z.mitarbnr AND p.projektbez = (SELECT p.projektbez FROM projekte p WHERE p.projektbez = 'Lagerreorganisation' ) AND p.projnr = z.projnr :unsure:
 
  • #12
mathmari said:
So do you mean it as follows ?

SELECT DISTINCT m.vorname, m.name
FROM mitarb m, projektzuord z, projekte p
WHERE m.mnr = z.mitarbnr AND p.projektbez = (SELECT p.projektbez FROM projekte p WHERE p.projektbez = 'Lagerreorganisation' ) AND p.projnr = z.projnr

Did you check if it works? 🤔

Either way, I think it should be:

SELECT DISTINCT m.vorname, m.name
FROM mitarb m, projektzuord z
WHERE m.mnr = z.mitarbnr AND z.projnr IN (SELECT p.projnr FROM projekte p WHERE p.projektbez = 'Lagerreorganisation' )

That is, we don't need `projekte p` in the FROM clause, and we don't need to refer to it in the WHERE clause either.
I've also changed the "=" into "IN" since the result of the sub query is a set. 🤔Btw, alternatively we can also do:

SELECT DISTINCT m.vorname, m.name
FROM mitarb m, projektzuord z, projekte p
WHERE m.mnr = z.mitarbnr AND p.projnr = z.projnr AND p.projektbez = 'Lagerreorganisation'

can't we? 🤔
 
  • #13
Klaas van Aarsen said:
Did you check if it works? 🤔

Yes, I got the correct result, the same with the result I get with your commands! :geek:
Klaas van Aarsen said:
Either way, I think it should be:

SELECT DISTINCT m.vorname, m.name
FROM mitarb m, projektzuord z
WHERE m.mnr = z.mitarbnr AND z.projnr IN (SELECT p.projnr FROM projekte p WHERE p.projektbez = 'Lagerreorganisation' )

That is, we don't need `projekte p` in the FROM clause, and we don't need to refer to it in the WHERE clause either.
I've also changed the "=" into "IN" since the result of the sub query is a set. 🤔Btw, alternatively we can also do:

SELECT DISTINCT m.vorname, m.name
FROM mitarb m, projektzuord z, projekte p
WHERE m.mnr = z.mitarbnr AND p.projnr = z.projnr AND p.projektbez = 'Lagerreorganisation'

can't we? 🤔

I see ! 🤩

At (2) I have written the below commands :

SELECT m.vorname, m.name, MAX(DATEDIFF(day, z.datumbis, z.datumvon)*m.tagessatz)
FROM mitarb m, projektzuord z
WHERE m.mnr = z.mitarbnr AND m.kompetenz = z.kompetenz
GROUP BY m.vorname, m.name
ORDER BY MAX(DATEDIFF(day, z.datumbis, z.datumvon)*m.tagessatz)


I get an error because of the DATEDIFF but in Google I found that it is defined like that. What am I writing wrong? And is the idea in ngeneral of that commands correct ? Do we have to use also here some nested WHERE ? :unsure:
 
  • #14
mathmari said:
At (2) I have written the below commands :

SELECT m.vorname, m.name, MAX(DATEDIFF(day, z.datumbis, z.datumvon)*m.tagessatz)
FROM mitarb m, projektzuord z
WHERE m.mnr = z.mitarbnr AND m.kompetenz = z.kompetenz
GROUP BY m.vorname, m.name
ORDER BY MAX(DATEDIFF(day, z.datumbis, z.datumvon)*m.tagessatz)I get an error because of the DATEDIFF but in Google I found that it is defined like that. What am I writing wrong? And is the idea in ngeneral of that commands correct ? Do we have to use also here some nested WHERE ?
What is the error that you get? 🤔

I think DATEDIFF will work, but we're not using it right. We shouldn't take a MAX of it. Instead we should list for each employee what their turnover is, give the column a name (for instance "AS turnover"), and sort on that name in the ORDER clause in descending order. 🤔
 
  • #15
Klaas van Aarsen said:
What is the error that you get? 🤔

I get an error about "day" : no such column: day. :unsure:
 
  • #16
mathmari said:
I get an error about "day" : no such column: day.
What do you get if you run:

SELECT DATEDIFF(day, '2011-08-25', '2017-08-25') AS DateDiff;

🤔
 
  • #17
Klaas van Aarsen said:
What do you get if you run:

SELECT DATEDIFF(day, '2011-08-25', '2017-08-25') AS DateDiff;

🤔
The same error as previously. :unsure:
 
  • #19
Klaas van Aarsen said:
Which SQL are you using? 🤔

I use this one . So is it because of the compiler or am I doing something wrong? :unsure:
 
  • #20
mathmari said:
I use this one . So is it because of the compiler or am I doing something wrong?
Yes.

I found that we get that error if we try to use PostgreSQL.
In its manual I found that we can use for instance:
SELECT DATE_PART('day', '2011-12-31 01:00:00'::timestamp - '2011-12-29 23:00:00'::timestamp);
instead. 🤔

Alternatively we can use a different SQL compiler.
If we pick:
1643237228435.png

then it works for me. 🤔
 
  • #21
Klaas van Aarsen said:
I think DATEDIFF will work, but we're not using it right. We shouldn't take a MAX of it. Instead we should list for each employee what their turnover is, give the column a name (for instance "AS turnover"), and sort on that name in the ORDER clause in descending order. 🤔

Do we not do that as follows ?

SELECT m.vorname, m.name, DATEDIFF(day, z.datumbis, z.datumvon)*m.tagessatz
FROM mitarb m, projektzuord z
WHERE m.mnr = z.mitarbnr AND m.kompetenz = z.kompetenz
GROUP BY m.vorname, m.name
ORDER BY MAX(DATEDIFF(day, z.datumbis, z.datumvon)*m.tagessatz) I get the error : " Column 'projektzuord.datumbis' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. "

:unsure:
 
  • #22
mathmari said:
I get the error : " Column 'projektzuord.datumbis' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. "
That's because you have GROUP BY and ORDER BY clauses that have certain requirements. If we leave out those clauses, it works just fine. 🤔
 
  • #23
Klaas van Aarsen said:
That's because you have GROUP BY and ORDER BY clauses that have certain requirements. If we leave out those clauses, it works just fine. 🤔

So is the following correct ?

SELECT m.vorname, m.name, DATEDIFF(day, z.datumvon, z.datumbis)*m.tagessatz
FROM mitarb m, projektzuord z
WHERE m.mnr = z.mitarbnr AND m.kompetenz = z.kompetenzDo we just have to order the list that we get? :unsure:
 
  • #24
mathmari said:
So is the following correct ?

SELECT m.vorname, m.name, DATEDIFF(day, z.datumvon, z.datumbis)*m.tagessatz
FROM mitarb m, projektzuord z
WHERE m.mnr = z.mitarbnr AND m.kompetenz = z.kompetenz

Do we just have to order the list that we get?
If I run that, then I get:
1643238850506.png


I see a couple of problems:
1. Armin shows up twice.
2. The 3rd column doesn't have a header.
3. The table doesn't have the employee with the biggest sales at the top.
(Worried)
 
  • #25
Klaas van Aarsen said:
If I run that, then I get:
View attachment 11689

I see a couple of problems:
1. Armin shows up twice.
2. The 3rd column doesn't have a header.
3. The 3rd column has negative numbers.
(Worried)

I corrected the problems 2 and 3. Do we not solve the first problem with DISTINCT ?

I have now this but I get again Armin several times. :unsure:
 
  • #26
mathmari said:
I corrected the problems 2 and 3. Do we not solve the first problem with DISTINCT ?

I have now this but I get again Armin several times.
Armin can work both as a programmer and as a system administrator and he gets paid different rates for those roles.
I think we have to combine them somehow. 🤔
 
  • #27
Klaas van Aarsen said:
Armin can work both as a programmer and as a system administrator and he gets paid different rates for those roles.
I think we have to combine them somehow. 🤔

Do we do that with GROUP BY ? :unsure:
 
  • #28
mathmari said:
Do we do that with GROUP BY ?
Yes. 🤔
 
  • #29
Klaas van Aarsen said:
Yes. 🤔

Do we not write that as follows ?

SELECT DISTINCT m.vorname, m.name, DATEDIFF(day, z.datumvon, z.datumbis)*m.tagessatz AS Umsatz
FROM mitarb m, projektzuord z
WHERE m.mnr = z.mitarbnr AND m.kompetenz = z.kompetenz
GROUP BY m.vorname, m.name Because I get the error :
Column 'projektzuord.datumvon' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

:unsure:
 
  • #30
mathmari said:
Because I get the error :
Column 'projektzuord.datumvon' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

We are trying to group the 2 rows that refer to Armin into 1 row.
Those 2 rows have different numbers for the Umsatz.
How should those 2 numbers be combined (aka aggregated)? 🤔
 
  • #31
Klaas van Aarsen said:
We are trying to group the 2 rows that refer to Armin into 1 row.
Those 2 rows have different numbers for the Umsatz.
How should those 2 numbers be combined (aka aggregated)? 🤔

We have to add the corresponding values of Umsatz, right? So do we write something like :

SELECT DISTINCT m.vorname, m.name, DATEDIFF(day, z.datumvon, z.datumbis)*m.tagessatz AS Umsatz
FROM mitarb m, projektzuord z
WHERE m.mnr = z.mitarbnr AND m.kompetenz = z.kompetenz
GROUP BY m.vorname, m.name,SUM(DATEDIFF(day, z.datumvon, z.datumbis)*m.tagessatz) :unsure:
 
  • #32
mathmari said:
We have to add the corresponding values of Umsatz, right? So do we write something like
Something like that yes (Nod)

Does it work? 🤔
 
  • #33
Klaas van Aarsen said:
Something like that yes (Nod)

Does it work? 🤔

No, I get an error : I get this :unsure:
 
  • #34
mathmari said:
No, I get an error : I get this
It says:
> Cannot use an aggregate or a subquery in an expression used for the group by list of a GROUP BY clause.

Perhaps we shouldn't put the SUM in the GROUP BY clause, but somewhere else?
Can we find an example how to use a GROUP BY clause? 🤔
 
  • #35
Klaas van Aarsen said:
It says:
> Cannot use an aggregate or a subquery in an expression used for the group by list of a GROUP BY clause.

Perhaps we shouldn't put the SUM in the GROUP BY clause, but somewhere else?
Can we find an example how to use a GROUP BY clause? 🤔

Ah I fixed it ! Now it works! 🤩
Is my below attempt correct ? :unsure:

At (3) :

SELECT p.projektbez
FROM projektzuord z, projekte p
GROUP BY p.projektbez, p.projnr
HAVING COUNT(p.projektbez)>=2 AND p.projnr = ANY (SELECT z.projnr FROM projektzuord z)At (4) :

SELECT COUNT(m.mnr), m.kompetenz
FROM projektzuord z, mitarb m
WHERE ((z.datumbis $<$ '20220315' OR z.datumvon $>$ '20220409') AND (m.mnr = z.mitarbnr)) OR m.mnr != ALL (SELECT z.mitarbnr FROM projektzuord z)
GROUP BY m.mnr, m.kompetenzAt (5) do we make INSERT the new values in the corresponding tables?

INSERT INTO projekte
VALUES
('p42', 'Datenreorganisation', 'kn99', 12000);

INSERT INTO projektzuord
VALUES
('p42', 123, 'k3', '2022-03-01', '2022-05-31');
:unsure:
 
Last edited by a moderator:
<h2>1. What is the syntax for using subtraction in SQL commands?</h2><p>The syntax for using subtraction in SQL commands is "SELECT column1 - column2 FROM table_name;" where column1 and column2 are the columns you want to subtract from each other and table_name is the name of the table containing the columns.</p><h2>2. Can you use subtraction with multiple columns in a SQL command?</h2><p>Yes, you can use subtraction with multiple columns in a SQL command by adding additional columns after the first subtraction sign. For example, "SELECT column1 - column2 - column3 FROM table_name;"</p><h2>3. How do you multiply values in SQL commands?</h2><p>To multiply values in SQL commands, you can use the asterisk (*) symbol. The syntax is "SELECT column1 * column2 FROM table_name;" where column1 and column2 are the columns you want to multiply.</p><h2>4. Can you use multiplication and subtraction in the same SQL command?</h2><p>Yes, you can use both multiplication and subtraction in the same SQL command. The order of operations follows the standard mathematical rules, so you can use parentheses to specify which operation should be performed first.</p><h2>5. Are there any limitations when using subtraction and multiplication in SQL commands?</h2><p>There are no specific limitations when using subtraction and multiplication in SQL commands, but it is important to ensure that the columns you are using are of the correct data type and that the resulting values make sense in the context of your data.</p>

Related to SQL commands with subtraction and multiplication

1. What is the syntax for using subtraction in SQL commands?

The syntax for using subtraction in SQL commands is "SELECT column1 - column2 FROM table_name;" where column1 and column2 are the columns you want to subtract from each other and table_name is the name of the table containing the columns.

2. Can you use subtraction with multiple columns in a SQL command?

Yes, you can use subtraction with multiple columns in a SQL command by adding additional columns after the first subtraction sign. For example, "SELECT column1 - column2 - column3 FROM table_name;"

3. How do you multiply values in SQL commands?

To multiply values in SQL commands, you can use the asterisk (*) symbol. The syntax is "SELECT column1 * column2 FROM table_name;" where column1 and column2 are the columns you want to multiply.

4. Can you use multiplication and subtraction in the same SQL command?

Yes, you can use both multiplication and subtraction in the same SQL command. The order of operations follows the standard mathematical rules, so you can use parentheses to specify which operation should be performed first.

5. Are there any limitations when using subtraction and multiplication in SQL commands?

There are no specific limitations when using subtraction and multiplication in SQL commands, but it is important to ensure that the columns you are using are of the correct data type and that the resulting values make sense in the context of your data.

Back
Top