I have a table that has the following columns: Netting_Pool, Counterparty and Account. My goal is to run a SQL query to show when there is a Netting_Pool with more than 1 Counterparty, and to show the Accounts linked to those Counterparties.
An example:
Netting_Pool Counterparty Account
1 ----- A ----- ASD
1 ----- A ----- XYZ
1 ----- B ----- DEF
2 ----- C ----- YUI
3 ----- D ----- TRE
4 ----- E ----- DDW
5 ----- F ----- QWE
I would like the query to have the following Return:
1 ----- A ----- ASD
1 ----- A ----- XYZ
1 ----- B ----- DEF
So far the closest I have come is the following:
SELECT netting_pool, count (distinct counterparty)
FROM Table
GROUP BY netting_pool
HAVING count(distinct counterparty) > 1'
Which returns:
Netting_Pool, Count (distinct Counterparty)
1 2
I have not been able to incorporate the Counterparty or Account values to my query and have it produce the results I want. Any help would be much appreciated!
Your query is aggregating, so you are only going to be getting one row. Another way to do this is with window/analytic functions, which are supported by most but not all databases.
Unfortunately, count(distinct) is not generally supported as a window function. But you can work around this by looking at the maximum and minimum values:
select Netting_Pool, Counterparty, Account
from (select t.*,
min(account) over (partition by Netting_Pool) as mina,
max(account) over (partition by Netting_Pool) as maxa
from table t
) t
where mina <> maxa;
Related
Let's say I have data like so:
PersonID Goal
-------- ----
1 one
1 two
1 three
2 alpha
2 beta
PersonID Note
-------- ----
1 hello
1 world
2 i
2 like
2 squirrels
And I want to put all the goals and notes for each person into a single row:
PersonID Goal 1 Goal 2 Goal 3 Note 1 Note 2 Note 3
-------- ------ ------ ------ ------ ------ ------
1 one two three hello world
2 alpha beta i like squirrels
With the maximum goal and note numbers computed to be equal to the maximum count of goals and notes belonging to any one person - so if I add a third person who has five goals then two more goal columns will be added, for instance.
Is there any way to write a SQL query that combines the rows in this fashion? Or do I have to do that sort of data manipulation in the application itself (as I am currently), which is slower?
edit: I'm using SQL Server and it would be OK to instead have a delimited list, e.g.
PersonID Goals Notes
-------- ------ ------
1 one|two|three hello|world
2 alpha|beta i|like|squirrels
It's impossible to return an unknown amount of columns in sql.
What you CAN do is use is GROUP_CONCAT if you use mysql (other databases have other solutions)
https://www.geeksforgeeks.org/mysql-group_concat-function/
You will end up with data like this.
PersonID Goal Note
-------- -------------- ------------------
1 one,two,three hello,world
2 alpha,beta i,like,squirrels
Basically I need to generate a frequency table using sql, and I have a sample table like this:
user_id user_label code1 date
------ ----------- ----- ------
1 x a 01-01
1 x a 01-01
1 x a 01-02
1 x b 01-01
1 x c 01-02
1 y a 01-01
2 x a 01-01
etc
The rule to count occurrences is if two rows have the same user_id ,user_label and date ,then repeated codes should only be counted once.
For example, for the first two rows the frequency table should be :
user_id user_label code1 count_code_1
-------- ----------- ----- ------------
1 x a 1
Because even though there are two instances of a, but they happen on the same date so should only be counted once and I need do this for every unique codes in code_1 column
for all combinations of user_id + user_label
After processing the third row , the frequency table should be :
user_id user_label code_1 count_code_1
-------- ----------- ------ ------------
1 x a 2
Since although is the same code ('a') but it happens on a different date (01-02)
In the end, for the sample table given above, the desired result should be
user_id user_label code_1 count_code_1
-------- ----------- ------ -------------
1 x a 2
1 x b 1
1 x c 1
1 y a 1
2 x a 1
What I have so far is
select t.user_id, t.user_label, t.code_1, count(###)
from t
group by t.code_1,t.user_id, t.user_label
The problem is
1. I don't know what to put inside the count 2. I don't know how to incorporate the condition on date in to this query.
Any suggestion, correction would be greatly appreciated.
You seem to want count(distinct date):
select t.user_id, t.user_label, t.code_1,
count(distinct date)
from t
group by t.code_1,t.user_id, t.user_label
I have a table with 3 different columns pid,org,amount as shown below.
pid org amount
---- ---- ------
1 1 5
1 1 6
2 1 2
2 1 4
I need the records grouped by pid and org with the maximum amount.
As,Rich functionalities of sql are not supported in hive need an easy way of obtaining it.
The result table should be like
pid org amount
---- ---- ------
1 1 6
2 1 4
select pid,org,max(amount) from table1 group by pid,org;
use max function
Returns the maximum value of the column in the group
select pid,org,max(amount) from data
group by pid,org;
if not work, convert amount in double;
select pid,org,max(CAST(amount as double)) from data
group by pid,org;
My column called type-row has the following values (A, B, C, D, E...) I need to make a query which will order the result sorting by the column type-row but I don't want them all together I want the result to be interpolated as follow
id --- some-column -- type-row
1 --- 'bla1' ------- A
2 --- 'bla2' ------- B
3 --- 'bla3' ------- C
4 --- 'bla4' ------- D
5 --- 'bla5' ------- E
6 --- 'bla6' ------- A
7 --- 'bla7' ------- B
8 --- 'bla8' ------- C
9 --- 'bla9' ------- D
and so on...
Is there anyway to make this on SQL or even JPA? I'm using Postgresql with JPA
You can do this in SQL by enumerating the values for each value of type-row, which you can do using window functions:
select t.*
from table t
order by row_number() over (partition by typerow order by id),
typerow;
Firstly, thank you for your time taken out to read this and please excuse the title, I wasn't quite sure how to describe my problem.
I have two tables. One for SystemUsers and another for PrintingPermissions.
PrintingPermissions Table
ID ---- SystemUserID --- PrintGroupTypeID --- CanPrint
1 ----------- 22 ------------------------- 1 -------------------True
2 ----------- 22-------------------------- 2 -------------------True
3 ----------- 22 ------------------------- 3 -------------------False
4 ----------- 23 ------------------------- 1 -------------------True
.
.
SystemUsers Table
ID ----------- Name
22 ----------- Robert
23 ----------- John
24 ----------- Simon
25 ----------- Kate
I need a select query that will generate a list of all users and their and their PrintPermissions based on the PrintingPermissions.PrintGroupTypeID.
The thing to note is that if the User is NOT listed in the PrintPermissions table I would still like their object to be created but with a CanPrintValue of FALSE or NULL.
i.e. the output of the supplied table data above should be like the following when selecting WHERE PrintGroupTypeID = 1.
.
.
RESULT (WHERE PrintGroupTypeID = 1)
Name ----- SystemUserID ----- CanPrint
Robert --------- 22 -------------------- True
John ----------- 23 --------------------- True
Simon --------- 24 --------------------- False //-- NOT in permission table, default false created
Kate ----------- 25 --------------------- False //-- NOT in permission table, default false created
Again, thank you very much for your time and please do let me know if you don't fully understand what I'm trying to achieve.
Rob
SELECT DISTINCT su.Name, su.ID, ISNULL(pp.CanPrint, CAST 0 AS BIT) as CanPrint
FROM SystemUser su
LEFT JOIN Printing Permissions pp ON su.ID = pp.SystemUserID AND pp.PrintGroupTypeID = #TargetPrintGroupTypeID
If you want null instead of false, you can omit the ISNULL function and just select CanPrint directly. This will give you three state results (true, false, null), and will allow you to determine which users are disallowed (false) and which are not in the print group (null).
How about:
Select s.name as name,
s.id as SystemUserID,
isnull(p.canprint, 'false') as CanPrint
From systemusers s
Left outer Join printingpermissions p on s.id = p.systemuserid
Where p.printgrouptypeid = 1
Ok, I've just managed to work it out.
Here's the query:
SELECT SystemUsers.Name, ISNULL(PrintingPermissions.CanPrint, 'FALSE') AS CanPrint
FROM SystemUsers LEFT OUTER JOIN
PrintingPermissions ON SystemUsers.ID = PrintingPermissions.SystemUserID AND
PrintingPermissions.PrintingGroupTypeID = #ID