SQL Concat Id column with another column - sql

Here is what I want to do:
I have this table
+----+-------------+
| id | data |
+----+-------------+
| 1 | max |
| 2 | linda |
| 3 | sam |
| 4 | henry |
+----+-------------+
and I want to Update the data with concatenating Id column with data, which will look like this:
+----+-------------+
| id | data |
+----+-------------+
| 1 | max1 |
| 2 | linda2 |
| 3 | sam3 |
| 4 | henry4 |
+----+-------------+

Sounds like this is basically what you want (T-SQL, Other platforms may have different methods for type conversion and concatenation):
update myTable
set data=data+convert(varchar(50),id)

Related

postgresql & json - counting distinct values

In PostgreSQL, I have a table that looks like,
| id | json |
| -- | ------------------------------- |
| 1 | {"id":1,"customer":"BANK"} |
| 1 | {"id":1,"customer":"BANK"} |
| 2 | {"id":2,"customer":"GOVT"} |
| 3 | {"id":3,"customer":"BANK"} |
| 4 | {"id":4,"customer":"ASSET MGR"} |
| 4 | {"id":4,"customer":"ASSET MGR"} |
I need the output of counting the occurrences of customers with unique ids, such as
| customer | count |
| ----------- | ----- |
| "BANK" | 2 |
| "GOVT" | 1 |
| "ASSET MGR" | 1 |
Is there a good way to achieve using PostgreSQL & json? I currently am able to extract the customer and IDs, but am having difficulty counting the unique json objects.
select count(distinct id), jsondata ->> 'customer' customer
from data
group by customer
count | customer
----: | :--------
1 | ASSET MGR
2 | BANK
1 | GOVT
db<>fiddle here

Postgres key-value table, select values as columns

I have the following table:
+----+---------+-------+
| id | Key | Value |
+----+---------+-------+
| 1 | name | Bob |
| 1 | surname | Test |
| 1 | car | Tesla |
| 2 | name | Mark |
| 2 | cat | Bobby |
+----+---------+-------+
Key can hold basically anything. I would like to arrive at the following output:
+----+------+---------+-------+-------+
| id | name | surname | car | cat |
+----+------+---------+-------+-------+
| 1 | Bob | Test | Tesla | |
| 2 | Mark | | | Bobby |
+----+------+---------+-------+-------+
Then I would like to merge the output with another table (based on the id).
Is it possible to do, if I don't know what the Key column holds? Values there are dynamic.
Could you point me to the right direction?

How to concat rows based on ID,

Using standard SQL in bigquery:
Given a table such as: Where the values have been counted so only appear once
| id | key | value |
--------------------
| 1 | read | aa |
| 1 | read | bb |
| 1 | name | abc |
| 2 | read | bb |
| 2 | read | cc |
| 2 | name | def |
| 2 | value| some |
| 3 | read | aa |
How can I make it so each row is one user and their respective values? e.g. NEST
So the table would look like:
| id | key | value |
--------------------
| 1 | read | aa |
| | read | bb |
| | name | abc |
| 2 | read | bb |
| | read | cc |
| | name | def |
| | value| some |
| 3 | read | aa |
I've tried using ARRAY_AGG on the column, which ends up listing all the values of that column.
I just need to have each row as a single user with multiple values, as shown above.
Like BigQuery does here, this is what I want it to look like:
Below is for BigQuery Standard SQL
#standardSQL
SELECT id, ARRAY_AGG(STRUCT(key AS key, value AS value)) params
FROM `project.dataset.table`
GROUP BY id
if to apply to your sample data - result is

SQL 'Sum' Text Fields, Delim with commas

I have a table like this:
+----+-------+-----------------+
| ID | Name | Email |
+----+-------+-----------------+
| 1 | Jane | Jane#doe.com |
| 2 | Will | Will#gmail.com |
| 3 | Will | wsj#example.com |
| 4 | Jerry | jj2#test.com |
+----+-------+-----------------+
Unfortunately I have records that are duplicates due to multiple emails. I would like to run a sql query to generate this:
+----+-------+---------------------------------+
| ID | Name | Email |
+----+-------+---------------------------------+
| 1 | Jane | Jane#doe.com |
| 2 | Will | Will#gmail.com, wsj#example.com |
| 4 | Jerry | jj2#test.com |
+----+-------+---------------------------------+
I know with numbers you'd do something like this, but I don't know how to 'sum' text fields:
SELECT *,
SUM(Number_Field) AS Number_Field,
FROM table
Thanks!
Edit: I am using MS Access

Multiplying values of same PKs

I have the following table:
+-----+------+
| qwe | asdd |
+-----+------+
| a | 3 |
| a | 4 |
| b | 5 |
| b | 6 |
+-----+------+
The result should be something like this:
+-----+------+
| qwe | asdd |
+-----+------+
| a | 12 |
| b | 30 |
+-----+------+
I wrote a code that may be only applied to the actual table, but if we add a row or more, it is not working well:
select qwe, (SUM(asd) - MIN(asd)) * MIN(asd) a from t
group by qwe
How would you recommend me to modify this code to make it work properly with tables like this?
+-----+------+
| qwe | asdd |
+-----+------+
| a | 3 |
| b | 4 |
| b | 5 |
| a | 6 |
| a | 7 |
+-----+------+
And get table like this:
+-----+------+
| qwe | asdd |
+-----+------+
| a | 12 |
| b | 126 |
+-----+------+
There is no built in PRODUCT() function. Alas.
Assuming all your values are positive, you can do:
select qwe, exp(sum(log(asdd))) as aggregate_product
from t
group by qwe;
Note: This can be extended to handle 0 and negative values. That just adds a lot of extra stuff to the expression, which hides the fundamental logic.
To prevent problems with zero:
select qwe, coalesce(exp(sum(log(nullif(asdd, 0)))), 0) as aggregate_product
Negative numbers are a bit trickier.