Segregating two rows with different data into two different columns - sql

I have a table with Data as follows in the following fiddle
http://sqlfiddle.com/#!18/c687d
Trying to segregate column of phone number based on phone type.
Following is the input
| LastName | PhoneNameType | PhoneNumber |
|----------|---------------|-------------|
| KRANSLER | Work | 8326244229 |
| KRANSLER | Mobile | 7239876 |
| GILBERT | Work | 2121806 |
| GILBERT | Mobile | 8406582 |
| LITZ | Work | 3462590784 |
| LITZ | Mobile | 2816284631 |
Output should be as follows
| LastName | WorkNumber | MobileNumber |
|----------|------------|--------------|
| GILBERT | 2121806 | 8406582 |
| KRANSLER | 8326244229 | 7239876 |
| LITZ | 3462590784 | 2816284631 |

One more option with conditional aggregation. (Works as expected when there is at most one row per type per name)
SELECT LastName
,max(case when phonenametype='Work' then phonenumber end) as worknumber
,max(case when phonenametype='Mobile' then phonenumber end) as mobilenumber
FROM tempo
GROUP BY LastName

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

ActiveJdbc unable to include the table

I have two tables orders and address
Order table
+----+----------------+-----------------------------------+--------------------------------------------------+
| id | trackingNumber | user_id (reference to user table) | receiverAddress_id (reference to address table ) |
+----+----------------+-----------------------------------+--------------------------------------------------+
| 1 | TRACK123 | 1 | 2 |
+----+----------------+-----------------------------------+--------------------------------------------------+
| | | | |
+----+----------------+-----------------------------------+--------------------------------------------------+
address table
+----+--------+--------------+--------------+
| id | name | addressLine1 | addressLine2 |
+----+--------+--------------+--------------+
| 1 | Mr.abc | qwertyuiop | qwertyuiop |
+----+--------+--------------+--------------+
| | | | |
+----+--------+--------------+--------------+
Need help in activeJdbc joining these two tables and get address as the children in JSON.
I have tried the relation annotatins however unable to get the desired result.
//sample code: not working
LazyList<Orders> order = Orders.find("id = 1").include(Addresses.class);
order.toJson(true);

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

Query to get data from a table as column names in another table

I created some sample tables and data to try LISTAGG
SELECT SALE_TITLE, addr_id, addr_name, addr_desc
, LISTAGG(SALES_ID, ',')
WITHIN GROUP (ORDER BY SALES_ID)
AS SALES_ID
FROM (select a.addr_id, a.addr_name, a.addr_desc,
b.SALES_ID, b.SALE_TITLE,
ROW_NUMBER () OVER (ORDER BY b.SALES_ID ) rn,
COUNT (*) OVER () cnt
from palm.adrss a, palm.sales b
where a.addr_id = b.addr_id)
GROUP BY SALE_TITLE, addr_id, addr_name, addr_desc;
When I run the above query my result set is following
+-----------------+----------+-----------+--------------+-------------+
| SALE_TITLE | ADDR_ID | ADDR_NAME | ADDR_DESC | SALES_ID |
+-----------------+----------+-----------+--------------+-------------+
| Role | 2 | saty | local test | 14,34 |
| Entitlement | 2 | saty | local test | 22,42 |
| Role | 3 | vasu | portal | 14,34 |
| Entitlement | 3 | vasu | portal | 22,42 |
| Role | 4 | sand | Golden gate | 144,344 |
| Entitlement | 4 | sand | Golden gate | 224,424 |
| Role | 5 | Gou | Data Modeler | 144 |
| Suffix | 5 | Gouri | Data Modeler | 224,424 |
| Entitlement | 5 | Gou | Data Modeler | 344 |
| Role | 6 | Mad | Data Analyst | 144 |
| Entitlement | 6 | Mad | Data Analyst | 224,344,424 |
+-----------------+----------+-----------+--------------+-------------+
Now using this result set I want to write another query to get a result set which looks like below. In this Role, Entitlement, Suffix will be column names and will have sale_id's under them
+---------------+--------------+---------+----------------------+---------+
| ADDR_NAME | ADDR_DESC | Role | Entitlement | Suffix |
+---------------+--------------+---------+----------------------+---------+
| saty | local test | 14,34 | 22,42 | |
| vasu | portal | 14,34 | 22,42 | |
| sand | Golden gate | 144,344 | 224,424 | |
| Gou | Data Modeler | 144 | 344 | 224,424 |
| Mad | Data Analyst | 144 | 224,344,424 | |
+---------------+--------------+---------+----------------------+---------+
Please let me know if this is possible and provide me some inputs
Thanks in advance
I saw you have typo on sample data, if you fix it you will get your desired output.
update Your_Table
set ADDR_NAME='Gou'
where ADDR_NAME='Gouri'
Your query should be:
select t1.ADDR_NAME,t1.ADDR_DESC,
max(case when t1.SALE_TITLE='Role' then t2.SALES_ID end) 'Role',
max(case when t1.SALE_TITLE='Entitlement' then t2.SALES_ID end) 'Entitlement',
max(case when t1.SALE_TITLE='Suffix' then t2.SALES_ID end) 'Suffix'
from Your_Table t1
join Your_Table t2
on t1.ADDR_ID=t2.ADDR_ID and t1.SALE_TITLE=t2.SALE_TITLE
group by t1.ADDR_NAME,t1.ADDR_DESC
order by Role

Understanding JOINS , Sub Query and Aggregate Functions

In MYSQL , I am trying to understand the Aggregate Functions and am trying some examples in the northwind schema.
The table employees has the following description.
+-----------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| company | varchar(50) | YES | MUL | NULL | |
| last_name | varchar(50) | YES | MUL | NULL | |
| first_name | varchar(50) | YES | MUL | NULL | |
| email_address | varchar(50) | YES | | NULL | |
| job_title | varchar(50) | YES | | NULL | |
| business_phone | varchar(25) | YES | | NULL | |
| home_phone | varchar(25) | YES | | NULL | |
| mobile_phone | varchar(25) | YES | | NULL | |
| fax_number | varchar(25) | YES | | NULL | |
| address | longtext | YES | | NULL | |
| city | varchar(50) | YES | MUL | NULL | |
| state_province | varchar(50) | YES | MUL | NULL | |
| zip_postal_code | varchar(15) | YES | MUL | NULL | |
| country_region | varchar(50) | YES | | NULL | |
| web_page | longtext | YES | | NULL | |
| notes | longtext | YES | | NULL | |
| attachments | longblob | YES | | NULL | |
+-----------------+-------------+------+-----+---------+----------------+
Also the data in the table is
mysql> select city , first_name,last_name from employees;
+----------+------------+----------------+
| city | first_name | last_name |
+----------+------------+----------------+
| Seattle | Nancy | Freehafer |
| Bellevue | Andrew | Cencini |
| Redmond | Jan | Kotas |
| Kirkland | Mariya | Sergienko |
| Seattle | Steven | Thorpe |
| Redmond | Michael | Neipper |
| Seattle | Robert | Zare |
| Redmond | Laura | Giussani |
| Seattle | Anne | Hellung-Larsen |
+----------+------------+----------------+
I am trying to understand how can I find the Average number of people from different cities.
Till Now , I have
mysql> select city,count(city) from employees group by city;
+----------+-------------+
| city | count(city) |
+----------+-------------+
| Bellevue | 1 |
| Kirkland | 1 |
| Redmond | 3 |
| Seattle | 4 |
+----------+-------------+
Also I have
SELECT SUM(inner_count_city) from
(
SELECT city AS inner_city,
COUNT(*) AS inner_count_city
FROM employees
GROUP BY inner_city
) temp_table;
+-----------------------+
| SUM(inner_count_city) |
+-----------------------+
| 9 |
+-----------------------+
I am struggling to proceed forward do this due to the following reasons.
I am not able to do a AVG(COUNT(city)) - cant do two aggregate function
I am also not sure , how to divide it by the sum of count of cities ( = 9).
Not sure if I should use unions or joins , or subqueries.
I am trying to do something like
select city , inner_count_city / sum (inner_count_city) from ..
SQL only supports one aggregation at a time; for multiple aggregations you need multiple subqueries/CTEs. If you want the average number of people in the cities, then you are almost there:
SELECT AVG(inner_count_city * 1.0) from
FROM (SELECT city AS inner_city, COUNT(*) AS inner_count_city
FROM employees
GROUP BY inner_city
) c;
Note that SQL Server does integer arithmetic on integers. So the average of 1 and 2 is 1, not 1.5. Hence the * 1.0.
You can do this without a subquery as well:
SELECT COUNT(*) * 1.0 / COUNT(DISTINCT city)
FROM employees;
You can apply AVG aggregate function in an outer query:
select avg(cnt)
FROM (select count(city) as cnt
from employees
group by city) as t
To get the percentage of people per city you can use the following query:
select city, count(city) * 100.0 / total_count as cnt
from employees
cross join (select count(*) as total_count from employees) AS t
group by city, total_count