SQL Complex Counting - sql

I want to get all the possible type from a table and then count the rows under a group who has this type.
To better illustrate consider the following table.
Object (o)
id name group_id type
-----------------------------------------------------------
1 Computer 100 A
2 Monitor 100 A
3 Chair 100 B
4 Table 100 B
5 Telephone 100 C
6 Notebook 200 D
7 Pen 200 D
8 Wire 100 E
What I want to get is the following result:
group_id type total
-------------------------------
100 A 2
100 B 2
100 C 1
100 D 0
100 E 1
The group_id doesn't have to be included in the result, I just wanted to let you see here that I'm using a WHERE clause in my query. Basically I only want the results for group | 100 but I want the counts for all type.
Using my query below
SELECT o.group_id, o.type, COUNT(*) AS total
FROM object o
WHERE o.group_id = 100
GROUP BY o.type
I only get the following result:
group_id type total
-------------------------------
100 A 2
100 B 2
100 C 1
100 E 1
You will notice that I am missing the row for type | D. I also want to include that.

You can use the following query:
SELECT 100 AS group_id,
o.type,
COUNT(CASE WHEN o.group_id = 100 THEN 1 END) AS total
FROM object o
GROUP BY o.type
This query groups by type and uses conditional aggregation so as to count the rows under each group who have type = 100.

The problem is that Type D is not returned in the query for that ID, so it is not included in the groupings. To do this, we need to get the list of ALL Types in the table, then do the counts for your ID in the table. Something like this:
SELECT o_list.type, COUNT(o.id) AS total
FROM object o
RIGHT OUTER JOIN ( SELECT DISTINCT type from object ) o_list on o_list.type = o.type
WHERE o.group_id = 100
GROUP BY o_list.type
Giorgos's version is, arguably, cleaner to read and will always run in one full scan of the table. Mine is more complex, but if there are indexes on group_id and type, will run on index scans and so may be significantly faster on a large data set.
And you get to see that there are always options on how to solve a problem. :)

Since Type isn't in group 100 I first get a distinct set of types, then join back to objects since you want 100 for type D, I always show 100 when the type doesn't exist.
SELECT coalesce(O.Group_ID,100) as Group_ID, T.Type, count(O.Group_ID) as Total
FROM (Select Distinct Type from object) T
LEFT JOIN Object O
on T.Type = O.Type
GROUP BY coalesce(O.Group_ID,100), T.Type

Related

Inner join + group by - select common columns and aggregate functions

Let's say i have two tables
Customer
---
Id Name
1 Foo
2 Bar
and
CustomerPurchase
---
CustomerId, Amount, AmountVAT, Accountable(bit)
1 10 11 1
1 20 22 0
2 5 6 0
2 2 3 0
I need a single record for every joined and grouped Customer and CustomerPurchase group.
Every record would contain
columns from table Customer
some aggregation functions like SUM
a 'calculated' column. For example difference of other columns
result of subquery to CustomerPurchase table
An example of result i would like to get
CustomerPurchases
---
Name Total TotalVAT VAT TotalAccountable
Foo 30 33 3 10
Bar 7 9 2 0
I was able to get a single row only by grouping by all the common columns, which i dont think is the right way to do. Plus i have no idea how to do the 'VAT' column and 'TotalAccountable' column, which filters out only certain rows of CustomerPurchase, and then runs some kind of aggregate function on the result. Following example doesn't work ofc but i wanted to show what i would like to achieve
select C.Name,
SUM(CP.Amount) as 'Total',
SUM(CP.AmountVAT) as 'TotalVAT',
diff? as 'VAT',
subquery? as 'TotalAccountable'
from Customer C
inner join CustomerPurchase CR
on C.Id = CR.CustomerId
group by C.Id
I would suggest you just need the follow slight changes to your query. I would also consider for clarity, if you can, to use the terms net and gross which is typical for prices excluding and including VAT.
select c.[Name],
Sum(cp.Amount) as Total,
Sum(cp.AmountVAT) as TotalVAT,
Sum(cp.AmountVAT) - Sum(CP.Amount) as VAT,
Sum(case when cp.Accountable = 1 then cp.Amount end) as TotalAccountable
from Customer c
join CustomerPurchase cp on cp.CustomerId = c.Id
group by c.[Name];

postgreSQL - how to filter by json_agg value

I have join table between t_table and s_table.
there are many to many relationships between them.
s_table
id
s_value
1
1
2
2
3
3
t_table
id
t_value
1
100
2
200
3
300
t_id_s_id_table
s_id
t_id
1
1
1
2
2
2
2
3
3
1
3
3
First, I aggregated t_value group by s_table id by this query
SELECT
t_id_s_id_table.s_id,
JSON_AGG(t_value) AS json_agg
FROM
t_id_s_id_table
LEFT JOIN
t_table
ON
t_table.id = t_id_s_id_table.t_id
GROUP BY
t_id_s_id_table.s_id
And I got this result.
s_id
json_agg
1
100, 200
2
200, 300
3
100, 300
What I would like to do
I want to obtain all s_ids whose associated json_agg value includes 100.
(It means s_id = 1 and 3)
I tried the following query
SELECT *
FROM (
SELECT
t_id_s_id_table.s_id,
JSON_AGG(t_value) AS json_agg
FROM
t_id_s_id_table
LEFT JOIN
t_table
ON
t_table.id = t_id_s_id_table.t_id
GROUP BY
t_id_s_id_table.s_id
)
WHERE COUNT(json_agg = 100) > 0
but it doesn't work for me.
I got error operator does not exist: json = integer.
How can I make SQL in order to obtain get this result?
I am using PostgreSQL 11.2.
Thank you in advance.
Regardless of your query's business logic - as you need to count how many json_agg array elements are equal to 100, your where clause shall be
WHERE (
select count(*)
from json_array_elements_text(json_agg) jae
where jae::integer = 100
) > 0
or simpler, whether an array element equal to 100 exists
WHERE exists (
select from json_array_elements_text(json_agg) jae
where jae::integer = 100
)
And btw better do not use the name of a function (json_agg) as a column name.
The easy way would be to use HAVING instead of WHERE to act on each group, and use the BOOL_OR operator to compare if any item in the group is equal to 100;
SELECT
t_id_s_id_table.s_id,
JSON_AGG(t_value) AS json_agg
FROM
t_id_s_id_table
LEFT JOIN
t_table
ON
t_table.id = t_id_s_id_table.t_id
GROUP BY
t_id_s_id_table.s_id
HAVING BOOL_OR(t_value=100)
A DBfiddle to test with.

Count different groups in the same query

Imagine I have a table like this:
# | A | B | MoreFieldsHere
1 1 1
2 1 3
3 1 5
4 2 6
5 2 7
6 3 9
B is associated to A in an 1:n relationship. The table could've been created with a join for example.
I want to get both the total count and the count of different A.
I know I can use a query like this:
SELECT v1.cnt AS total, v2.cnt AS num_of_A
FROM
(
SELECT COUNT(*) AS cnt
FROM SomeComplicatedQuery
WHERE 1=1
-- AND SomeComplicatedCondition
) v1,
(
SELECT COUNT(A) AS cnt
FROM SomeComplicatedQuery
WHERE 1=1
-- AND SomeComplicatedCondition
GROUP BY A
) v2
However SomeComplicatedQuery would be a complicated and slow query and SomeComplicatedCondition would be the same in both cases. And I want to avoid calling it unnessesarily. Aside from that if the query changes, you need to make sure to change it in the other place too, making it prone to error and creating (probably unnessesary) work.
Is there a way to do this more efficiently?
Are you looking for this?
SELECT COUNT(*) AS total, COUNT(DISTINCT A) AS num_of_A
FROM (. . . ) q

SQL - Count Results of 2 Columns

I have the following table which contains ID's and UserId's.
ID UserID
1111 11
1111 300
1111 51
1122 11
1122 22
1122 3333
1122 45
I'm trying to count the distinct number of 'IDs' so that I get a total, but I also need to get a total of ID's that have also seen the that particular ID as well... To get the ID's, I've had to perform a subquery within another table to get ID's, I then pass this into the main query... Now I just want the results to be displayed as follows.
So I get a Total No for ID and a Total Number for Users ID - Also would like to add another column to get average as well for each ID
TotalID Total_UserID Average
2 7 3.5
If Possible I would also like to get an average as well, but not sure how to calculate that. So I would need to count all the 'UserID's for an ID add them altogether and then find the AVG. (Any Advice on that caluclation would be appreciated.)
Current Query.
SELECT DISTINCT(a.ID)
,COUNT(b.UserID)
FROM a
INNER JOIN b ON someID = someID
WHERE a.ID IN ( SELECT ID FROM c WHERE GROUPID = 9999)
GROUP BY a.ID
Which then Lists all the IDs and COUNT's all the USERID.. I would like a total of both columns. I've tried warpping the query in a
SELECT COUNT(*) FROM (
but this only counts the ID's which is great, but how do I count the USERID column as well
You seem to want this:
SELECT COUNT(DISTINCT a.ID), COUNT(b.UserID),
COUNT(b.UserID) * 1.0 / COUNT(DISTINCT a.ID)
FROM a INNER JOIN
b
ON someID = someID
WHERE a.ID IN ( SELECT ID FROM c WHERE GROUPID = 9999);
Note: DISTINCT is not a function. It applies to the whole row, so it is misleading to put an expression in parentheses after it.
Also, the GROUP BY is unnecessary.
The 1.0 is because SQL Server does integer arithmetic and this is a simple way to convert a number to a decimal form.
You can use
SELECT COUNT(DISTINCT a.ID) ...
to count all distinct values
Read details here
I believe you want this:
select TotalID,
Total_UserID,
sum(Total_UserID+TotalID) as Total,
Total_UserID/TotalID as Average
from (
SELECT (DISTINCT a.ID) as TotalID
,COUNT(b.UserID) as Total_UserID
FROM a
INNER JOIN b ON someID = someID
WHERE a.ID IN ( SELECT ID FROM c WHERE GROUPID = 9999)
) x

DB2 SQL filter query result by evaluating an ID which has two types of entries

After many attempts I have failed at this and hoping someone can help. The query returns every entry a user makes when items are made in the factory against and order number. For example
Order Number Entry type Quantity
3000 1 1000
3000 1 500
3000 2 300
3000 2 100
4000 2 1000
5000 1 1000
What I want to the query do is to return filter the results like this
If the order number has an entry type 1 and 2 return the row which is type 1 only
otherwise just return row whatever the type is for that order number.
So the above would end up:
Order Number Entry type Quantity
3000 1 1000
3000 1 500
4000 2 1000
5000 1 1000
Currently my query (DB2, in very basic terms looks like this ) and was correct until a change request came through!
Select * from bookings where type=1 or type=2
thanks!
select * from bookings
left outer join (
select order_number,
max(case when type=1 then 1 else 0 end) +
max(case when type=2 then 1 else 0 end) as type_1_and_2
from bookings
group by order_number
) has_1_and_2 on
type_1_and_2 = 2
has_1_and_2.order_number = bookings.order_number
where
bookings.type = 1 or
has_1_and_2.order_number is null
Find all the orders that have both type 1 and type 2, and then join it.
If the row matched the join, only return it if it is type 1
If the row did not match the join (has_type_2.order_number is null) return it no matter what the type is.
A "common table expression" [CTE] can often simplify your logic. You can think of it as a way to break a complex problem into conceptual steps. In the example below, you can think of g as the name of the result set of the CTE, which will then be joined to
WITH g as
( SELECT order_number, min(type) as low_type
FROM bookings
GROUP BY order_number
)
SELECT b.*
FROM g
JOIN bookings b ON g.order_number = b.order_number
AND g.low_type = b.type
The JOIN ON conditions will work so that if both types are present then low_type will be 1, and only that type of record will be chosen. If there is only one type it will be identical to low_type.
This should work fine as long as 1 and 2 are the only types allowed in the bookings table. If not then you can simply add a WHERE clause in the CTE and in the outer SELECT.