How do I convert SQL data from one view to this other view in Postgres - sql

I have table with data like this
id group order value
-------------------------
1 1 1 23
2 1 2 34
3 2 1 234
4 2 2 77
5 2 3 102
I want to insert into table so I have one row per group, with the value showing a string of comma-separated values orders based on the order.
id group value
----------------
1 1 23,34
2 2 234,77,102
How do I do this? I'm using Postgres 9.3

Postgres supports string_agg():
select row_number() over () as id, "group", string_agg(value, ',' order by "order")
from t
group by "group";

I would look at PostgreSQL's string_agg aggregate function.

Related

SUM a column in SQL, based on DISTINCT values in another column, GROUP BY a third column

I'd appreciate some help on the following SQL problem:
I have a table of 3 columns:
ID Group Value
1 1 5
1 1 5
1 2 10
1 2 10
1 3 20
2 1 5
2 1 5
2 1 5
2 2 10
2 2 10
3 1 5
3 2 10
3 2 10
3 2 10
3 4 50
I need to group by ID, and I would like to SUM the values based on DISTINCT values in Group. So the value for a group is only accounted for once even though it may appear multiple for times for a particular ID.
So for IDs 1, 2 and 3, it should return 35, 15 and 65, respectively.
ID SUM
1 35
2 15
3 65
Note that each Group doesn't necessarily have a unique value
Thanks
the CTE will remove all duplicates, so if there a sdiffrenet values for ID and Group, it will be counted.
The next SELECT wil "GROUP By" ID
For Pstgres you would get
WITH CTE as
(SELECT DISTINCT "ID", "Group", "Value" FROM tablA
)
SELECT "ID", SUM("Value") FROM CTE GROUP BY "ID"
ORDER BY "ID"
ID | sum
-: | --:
1 | 35
2 | 15
3 | 65
db<>fiddle here
Given what we know at the moment this is what I'm thinking...
The CTE/Inline view eliminate duplicates before the sum occurs.
WITH CTE AS (SELECT DISTINCT ID, Group, Value FROM TableName)
SELECT ID, Sum(Value)
FROM CTE
GROUP BY ID
or
SELECT ID, Sum(Value)
FROM (SELECT DISTINCT * FROM TableName) CTE
GROUP BY ID

How to sum values in a column based on values in a different column SQL

For example, lets say I have
id
values
1
10
1
12
1
10
2
2
2
5
2
4
then i would want sql to return
id
values
1
32
2
11
This is very basic sql.
select id, sum(values) as values
from foo
group by id
Select ID,sum(values)
From table
Group by ID;

SQL compares the value of 2 columns and select the column with max value row-by-row

I have table something like:
GROUP
NAME
Value_1
Value_2
1
ABC
0
0
1
DEF
4
4
50
XYZ
6
6
50
QWE
6
7
100
XYZ
26
2
100
QWE
26
2
What I would like to do is to groupby group and select the name with highest value_1. If their value_1 are the same, compare and select the max with value_2. If they're still the same, select the first one.
The output will be something like:
GROUP
NAME
Value_1
Value_2
1
DEF
4
4
50
QWE
6
7
100
XYZ
26
2
The challenge for me here is I don't know how many categories in NAME so a simple case when is not working. Thanks for help
You can use window functions to solve the bulk of your problem:
select t.*
from (select t.*,
row_number() over (partition by group order by value1 desc, value2 desc) as seqnum
from t
) t
where seqnum = 1;
The one caveat is the condition:
If they're still the same, select the first one.
SQL tables represent unordered (multi-) sets. There is no "first" one unless a column specifies the ordering. The best you can do is choose an arbitrary value when all the other values are the same.
That said, you might have another column that has an ordering. If so, add that as a third key to the order by.

Compare column entry to every other entry in the same column

I have a Column of values in SQLite.
value
-----
1
2
3
4
5
For each value I would like to know how many of the other values are larger and display the result. E.g. For value 1 there are 4 entries that have higher values.
value | Count
-------------
1 | 4
2 | 3
3 | 2
4 | 1
5 | 0
I have tried nested select statements and using the Count(*) function but I do not seem to be able to extract the correct levels. Any suggestions would be much appreciated.
Many Thanks
You can do this with a correlated subquery in SQLite:
select value,
(select count(*) from t t2 where t2.value > t.value) as "count"
from t;
In most other databases, you would use a ranking function such as rank() or dense_rank(), but SQLite doesn't support these functions.

Search string value from mssql column, regex, group by

These data:
ID Desc
1 CUSTSEG
2 CUSTSEG;CARDMNU;CRC;CRCBISOA;CARDMNU;CRC;CRCBISOA
3 CUSTSEG;HKM
4 CUSTSEG;HKM;HKM
5 CUSTSEG;HKM;HKM;HKM;HKM;HKM;HKM;HKM
6 CUSTSEG;PHPM
7 CUSTSEG;PHPM;CARDMNU
8 CUSTSEG;PHPM;CARDMNU;ATM
must be queried into this format:
COUNT Desc
1 ATM
4 CARDMNU
2 CRC
2 CRCBISOA
8 CUSTSEG
10 HKM
3 PHPM
How can I achieve this using? Substring? I've tried this:
SELECT COUNT(*), CallTraversalLog
FROM [IVR].[dbo].[tblReportData]
WHERE CallTraversalLog Like '%CUSTSEG%'
GROUP BY CallTraversalLog
But the resultset I got is
COUNT Desc
1 CUSTSEG;PHPM;CARDMNU;CRC;ATM
1 CUSTSEG;PHPM;CARDMNU;CRC;CARDMNU;CRC
1 CUSTSEG;PHPM;CARDMNU;CRC;CARDMNU;CRC;CRCBISOA
2 CUSTSEG;PHPM;CARDMNU;CRC;CC
3 CUSTSEG;PHPM;CARDMNU;CRC;CRC
2 CUSTSEG;PHPM;CARDMNU;CRC;CRC;CARDMNU;CRC
1 CUSTSEG;PHPM;CARDMNU;CRC;CRC;CRC;CRC;CARDMNU;CRC
25 CUSTSEG;PHPM;CARDMNU;CRC;CRCACTIVATION
4 CUSTSEG;PHPM;CARDMNU;CRC;CRCACTIVATION;CRCENROLL
55 CUSTSEG;PHPM;CARDMNU;CRC;CRCAPST
I would split the strings and count the items. You need a table valued function that splits a string by delimiter. If you don't want to write your own function you can easily google one. Then CROSS APPLY the function to your table and count the items.
SELECT s.item, count(*)
FROM [IVR].[dbo].[tblReportData] d
CROSS APPLY dbo.fnSplitString(d.CallTraversalLog, ';') s
GROUP BY s.item