SQL Server view with [with, distinct, STRING_AGG] - sql

I would like to add an distinct concatenated string for each row to this view.
SELECT
i.Id, i.Name, oavName.AttributeValue AS BelongingToName,
ie.ExecutionDate, i.Categories
FROM
dbo.[GAB.Inspection] AS i
INNER JOIN
dbo.Object AS o ON o.Id = i.ObjectId
INNER JOIN
dbo.ObjectHistory AS oh ON oh.ObjectId = o.Id
AND oh.IsLast = 'true'
LEFT OUTER JOIN
dbo.[GAB.InspectionExecution] AS ie ON i.Id = ie.InspectionId
AND ie.IsLast = 'true'
INNER JOIN
dbo.ObjectAttributeValue AS oavName ON oavName.ObjectHistoryId = oh.Id
INNER JOIN
dbo.ObjectAttributeTemplate AS oatName ON oavName.ObjectAttributeTemplateId = oatName.Id
AND oatName.AttributeLabel = 'Name'
This returns the following result:
+----+-----------------+------------------+---------------+------------+
| Id | Name | BelongingToName | ExecutionDate | Categories |
+----+-----------------+------------------+---------------+------------+
| 1 | Demo Inspection | Demo Main Object | 2022-04-14 | DemoCat |
+----+-----------------+------------------+---------------+------------+
Now I have an other table where I get all the available intervals in it like this:
SELECT InspectionId, Turnus AS Interval
FROM [GAB.QuestionTemplate]
Result:
+--------------+----------+
| InspectionId | Interval |
+--------------+----------+
| 1 | 1 |
| 1 | 1 |
| 1 | 1 |
| 1 | 5 |
| 1 | 5 |
+--------------+----------+
And distincted
SELECT InspectionId, Turnus AS Interval
FROM [GAB.QuestionTemplate]
GROUP BY InspectionId, Turnus AS Interval
+--------------+----------+
| InspectionId | Interval |
+--------------+----------+
| 1 | 1 |
| 1 | 5 |
+--------------+----------+
So far so good... Now what I need is an concatenated String based on the Intervals.
WITH Turnusse AS
(
SELECT Turnus
FROM [GAB.QuestionTemplate]
GROUP BY Turnus
)
SELECT
STRING_AGG(ISNULL(Turnus, ' '), ';') WITHIN GROUP (ORDER BY Turnus ASC) AS Intervals
FROM
Turnusse
Result:
+-----------+
| Intervals |
+-----------+
| 1;5 |
+-----------+
What I now need is to add this Intervals belonging to the InspectionId as an separate column in the view.
Should look like this then:
+----+-----------------+------------------+---------------+------------+-----------+
| Id | Name | BelongingToName | ExecutionDate | Categories | Intervals |
+----+-----------------+------------------+---------------+------------+-----------+
| 1 | Demo Inspection | Demo Main Object | 2022-04-14 | DemoCat | 1;5 |
+----+-----------------+------------------+---------------+------------+-----------+
This is what I don't get... How can I implement the STRING_AGG into the view to give the expected output?
The table [GAB.QuestionTemplate] has the InspectionId which is the i.Id in the view.

Related

Joining table on two columns only joins it on a single

How do I correctly join a table on two columns. My issue is that the result is not correct as it only joins on a single column.
This question started of in this other question: SQL query returns product of results instead of sum . I am creating a new question as there is an other issue I am trying to solve.
I join a table of materials on a table which contains multiple supply and disposal movements. Each movement references a material id. I would like to join the material on each movement.
My query:
SELECT supply_material_refer, disposal_material_refer, material_id, material_name
FROM "construction_sites"
JOIN projects ON construction_sites.project_refer = projects.project_id
JOIN addresses ON construction_sites.address_refer = addresses.address_id
cross join lateral ( select *
from (select row_number() over () as rn, *
from supplies
where supplies.supply_project_refer = projects.project_id) as supplies
full join (select row_number() over () as rn, *
from disposals
where disposals.disposal_project_refer = projects.project_id
) as disposals
on (supplies.rn = disposals.rn)
) as combined
LEFT JOIN materials material ON combined.disposal_material_refer = material.material_id
OR combined.supply_material_refer = material.material_id
WHERE (projects.project_name = 'Project 15')
ORDER BY construction_site_id asc;
The result of the query:
+-----------------------+-------------------------+-------------+---------------+
| supply_material_refer | disposal_material_refer | material_id | material_name |
+-----------------------+-------------------------+-------------+---------------+
| 1 | 1 | 1 | Materialtest |
| 2 | 1 | 1 | Materialtest |
| 2 | 1 | 2 | Dirt |
| 1 | 1 | 1 | Materialtest |
| 2 | 1 | 1 | Materialtest |
| 2 | 1 | 2 | Dirt |
| 1 | (null) | 1 | Materialtest |
| 4 | (null) | 4 | Stones |
+-----------------------+-------------------------+-------------+---------------+
An example line I have issues with:
+------------------------+-------------------------+-------------+---------------+
| supply_material_refer | disposal_material_refer | material_id | material_name |
+------------------------+-------------------------+-------------+---------------+
| 2 | 1 | 1 | Materialtest |
+------------------------+-------------------------+-------------+---------------+
A prefered output would be like:
+------------------------+----------------------+-------------------------+------------------------+
| supply_material_refer | supply_material_name | disposal_material_refer | disposal_material_name |
+------------------------+----------------------+-------------------------+------------------------+
| 2 | Dirt | 1 | Materialtest |
+------------------------+----------------------+-------------------------+------------------------+
I have created a sqlfiddle with dummy data: http://www.sqlfiddle.com/#!17/863d78/2
To my understanding the solution would be to have a disposal_material column and and supply_material column for the material names. I do not know how I can achieve this goal though...
Thanks for any help!

PostgreSQL can't make Self Join

I have a table:
| acctg_cath_id | parent | description |
| 1 | 20 | Bills |
| 9 | 20 | Invoices |
| 20 | | Expenses |
| 88 | 30 |
| 89 | 30 |
| 30 | |
And I want to create a self join in order to group my items under a parent.
Have tried this, but it doesn't work:
SELECT
accounting.categories.acctg_cath_id,
accounting.categories.parent
FROM accounting.categories a1, accounting.categories a2
WHERE a1.acctg_cath_id=a2.parent
I get error: invalid reference to FROM-clause entry for table "categories"
When I try:
a.accounting.categories.acctg_cath_id
b.accounting.categories.acctg_cath_id
I get error: cross-database references are not implemented: a.accounting.categories.acctg_cath_id
Desired output:
Expenses (Parent 20)
Bills (Child 1)
Invoices (Child 9)
What am I doing wrong here?
It seems you merely want to sort the rows:
select *
from accounting.categorie
order by coalesce(parent, acctg_cath_id), parent nulls first, acctg_cath_id;
Result:
+---------------+--------+-------------+
| acctg_cath_id | parent | description |
+---------------+--------+-------------+
| 20 | | Expenses |
| 1 | 20 | Bills |
| 9 | 20 | Invoices |
| 30 | | |
| 88 | 30 | |
| 89 | 30 | |
+---------------+--------+-------------+
Your syntax is performing a cross join:
FROM accounting.categories a1, accounting.categories a2
Try the following:
SELECT
a2.acctg_cath_id,
a2.parent
FROM accounting.categories a1
JOIN accounting.categories a2 ON (a1.acctg_cath_id = a2.parent)
;
Examine the DBFiddle.
You don't need grouping, only self join:
select
c.acctg_cath_id parentid, c.description parent,
cc.acctg_cath_id childid, cc.description child
from (
select distinct parent
from categories
) p inner join categories c
on p.parent = c.acctg_cath_id
inner join categories cc on cc.parent = p.parent
where p.parent = 20
You can remove the WHERE clause if you want all the parents with all their children.
See the demo.
Results:
> parentid | parent | childid | child
> -------: | :------- | ------: | :-------
> 20 | Expences | 1 | Bills
> 20 | Expences | 9 | Invoices
You don't need a self-join. You don't need aggregation. You just need a group by clause:
SELECT ac.*
FROM accounting.categories ac
ORDER BY COALESCE(ac.parent, ac.acctg_cath_id),
(CASE WHEN ac.parent IS NULL THEN 1 ELSE 2 END),
ac.acctg_cath_id;

Can't show all records with the same id while join in oracle xe 11g

I'm getting this message while using this query, is there anything wrong?
SELECT t.tanggal_transaksi, o.nama_lengkap, SUM(td.harga * td.qty) total
FROM transaksi t, transaksi_detail td, operator o
WHERE td.transaksi_id = t.transaksi_id AND o.operator_id = t.operator_id
GROUP BY t.transaksi_id
Updated :
After using the answer from #Barbaros Özhan using this query :
SELECT t.tanggal_transaksi, o.nama_lengkap, SUM(td.harga * td.qty) total
FROM transaksi t
INNER JOIN transaksi_detail td ON ( td.transaksi_id = t.transaksi_id )
INNER JOIN operator o ON ( o.operator_id = t.operator_id )
GROUP BY t.tanggal_transaksi, o.nama_lengkap;
the data is successfully displayed. but, there are few problems that occur, the value of the same operator_id cannot appear more than 1 time. Here is the sample data :
+--------------+-------------+-------------------+
| TRANSAKSI_ID | OPERATOR_ID | TANGGAL_TRANSAKSI |
+--------------+-------------+-------------------+
| 1 | 5 | 09/29/2018 |
| 2 | 3 | 09/29/2018 |
| 3 | 3 | 09/29/2018 |
| 4 | 1 | 09/29/2018 |
| 5 | 1 | 09/29/2018 |
+--------------+-------------+-------------------+
After use the query command, the output is :
+-------------------+------------------+--------+
| TANGGAL_TRANSAKSI | NAMA_LENGKAP | TOTAL |
+-------------------+------------------+--------+
| 09/29/2018 | Lina Harun | 419800 |
| 09/29/2018 | Titro Kusumo | 484000 |
| 09/29/2018 | Muhammad Kusnadi | 402000 |
+-------------------+------------------+--------+
When viewed from the operator table, there are 2 data with the same operator_id that is unreadable
+-------------+------------------+
| OPERATOR_ID | NAMA_LENGKAP |
+-------------+------------------+
| 1 | Muhammad Kusnadi |
| 3 | Lina Harun |
| 5 | Tirto Kusumo |
+-------------+------------------+
You need to include the columns in the SELECT-list t.tanggal_transaksi, o.nama_lengkap, also in the GROUP BY-list but not the others like t.transaksi_id. So, you might use the following without any issue :
SELECT t.tanggal_transaksi, o.nama_lengkap, SUM(td.harga * td.qty) total
FROM transaksi t
INNER JOIN transaksi_detail td ON ( td.transaksi_id = t.transaksi_id )
INNER JOIN operator o ON ( o.operator_id = t.operator_id )
GROUP BY t.tanggal_transaksi, o.nama_lengkap;
Or this one :
SELECT t.transaksi_id, SUM(td.harga * td.qty) total
FROM transaksi t
INNER JOIN transaksi_detail td ON ( td.transaksi_id = t.transaksi_id )
GROUP BY t.transaksi_id;
P.S. Prefer using ANSI-92 JOIN standard rather than old-style comma-type JOIN.

How to apply a SUM operation without grouping the results in SQL?

I have a table like this one:
+----+---------+----------+
| id | group | value |
+----+---------+----------+
| 1 | GROUP A | 0.641028 |
| 2 | GROUP B | 0.946927 |
| 3 | GROUP A | 0.811552 |
| 4 | GROUP C | 0.216978 |
| 5 | GROUP A | 0.650232 |
+----+---------+----------+
If I perform the following query:
SELECT `id`, SUM(`value`) AS `sum` FROM `test` GROUP BY `group`;
I, obviously, get:
+----+-------------------+
| id | sum |
+----+-------------------+
| 1 | 2.10281205177307 |
| 2 | 0.946927309036255 |
| 4 | 0.216977506875992 |
+----+-------------------+
But I need a table like this one:
+----+-------------------+
| id | sum |
+----+-------------------+
| 1 | 2.10281205177307 |
| 2 | 0.946927309036255 |
| 3 | 2.10281205177307 |
| 4 | 0.216977506875992 |
| 5 | 2.10281205177307 |
+----+-------------------+
Where summed rows are explicitly repeated.
Is there a way to obtain this result without using multiple (nested) queries?
IT would depend on your SQL server, in Postgres/Oracle I'd use Window Functions. In MySQL... not possible afaik.
Perhaps you can fake it like this:
SELECT a.id, SUM(b.value) AS `sum`
FROM test AS a
JOIN test AS b ON a.`group` = b.`group`
GROUP BY a.id, b.`group`;
No there isn't AFAIK. You will have to use a join like
SELECT t.`id`, tsum.sum AS `sum`
FROM `test` as t GROUP BY `group`
JOIN (SELECT `id`, SUM(`value`) AS `sum` FROM `test` GROUP BY `group`) AS tsum
ON tsum.id = t.id

Problems counting joined rows with conditional

I am having some problems with MySQL and selecting column count from a joined table. I have a feeling this is going to require a sub-select statement to fetch the rows I would like to join instead of doing it in my primary where condiutional.
I am trying to select a list of project which are linked to a certain user. I would also like to include a count of tasks which are assigned to this project and this user (but only those of a certain status).
I have it working almost - which means not at all. It will only return project id 1, and not id 2. This is because of my 'tasks.status<9' where clause.
Any help would be great. Let me know if I need to explain anything else.
+-------------------------+
| projects_to_users |
+-----------+-------------+
+ user_id | project_id |
+-----------+-------------+
+ 1 | 1 |
+-----------+-------------+
+ 1 | 2 |
+-----------+-------------+
+-------------------------+
| projects |
+--------------+----------+
+ project_id | name |
+--------------+----------+
+ 1 | Foo |
+--------------+----------+
+ 2 | Bar |
+--------------+----------+
+------------------------------------------------+
| tasks |
+-----------+--------------+----------+----------+
+ task_id | project_id | status | name |
+-----------+--------------+----------+----------+
+ 1 | 1 | 1 | Do it |
+-----------+--------------+----------+----------+
+ 2 | 1 | 1 | Do itt |
+-----------+--------------+----------+----------+
+ 3 | 1 | 9 | Do not |
+-----------+--------------+----------+----------+
SELECT count( tasks.task_id ) AS task_count, projects.*
FROM (projects)
LEFT JOIN tasks ON tasks.project_id = projects.project_id
LEFT JOIN projects_to_users ON projects.project_id=projects_to_users.project_id
WHERE tasks.status<9
AND tasks.assigned_user_id = '1'
AND projects_to_users.user_id = '1'
GROUP BY projects.project_id
RETURNS:
+--------------+--------------+--------+
+ task_count | project_id | name |
+--------------+--------------+--------+
+ 2 | 1 | Foo |
+--------------+--------------+--------+
SHOULD RETURN:
+--------------+--------------+--------+
+ task_count | project_id | name |
+--------------+--------------+--------+
+ 2 | 1 | Foo |
+--------------+--------------+--------+
+ 0 | 2 | Bar |
+--------------+--------------+--------+
If you want to show per-project, per-user task counts, you should also group by the projects_to_users.user_id field.
Also, the reason why you're not seeing the zero-count project is because your WHERE condition expects there to have been a match by means of the LEFT JOIN clauses. If you move the WHERE criteria into the joins themselves, this should work better for you.
For example:
SELECT count( tasks.task_id ) AS task_count,
projects.*
FROM (projects)
LEFT JOIN tasks ON tasks.project_id = projects.project_id AND tasks.status < 9 AND tasks.assigned_user_id = '1'
LEFT JOIN projects_to_users ON projects.project_id=projects_to_users.project_id AND projects_to_users.user_id = '1'
GROUP BY projects.project_id
Why can't you do a simple join like this:
select count(*) from projects_to_users inner join tasks on projects_to_users.project_id=tasks.project_id;