How to Pivot with Concat SQL Server Query? - sql-server-2016

please I want some help to choose from the following 3 tables
(I Normalise My database where each column from these 3 columns is in a different table)
Customer | type | company
--------- | ---------- | -----------
John | Benz | Mercedes
Mark | C200 | Mercedes
Judy | Lancer | Mitsubishi
John | Corolla | Toyota
John | Galant | Mitsubishi
I want the Results as
Customer | type1 | type2 | type3 | Others
------- | ---- | ----- | ----- | -------
John | Benz | Corolla | [Empty String]| Galant
Mark | C200 | [Empty String]| [Empty String]| [Empty String]
So, I want to pivot type and select specific values from it where company equals specific value; then Concat the other type values where the company is not equal my selection as others.
Please, anyone can help me to reach that approach,
Thanks in Advance.
ur.com/zkvf6.png

Related

Using a dropdown in Access, display and save a name field pulling from either of 3 tables with a common key

In my Access DB, I have 3 tables with the following fields:
Contributors
| ContributorId (PK) | ContributorName |
+--------------------+-----------------+
| 1 | Jane Doe |
| 2 | George Wish |
ContributorSpouses
| SpouseId (PK) | SpouseName | ContributorId (FK) |
+---------------+------------------+--------------------+
| 1 | Karen Wish | 2 |
| 2 | John Doe | 1 |
ContributorBusinesses
| BusinessId (PK) | BusinessName | ContributorId (FK) |
+-----------------+---------------------+--------------------+
| 1 | John's Pizza, Inc | 2 |
| 2 | Do Doe Dough, LLP | 2 |
| 3 | Jane's Nail Salon | 2 |
| 4 | Georges Car Wash | 1 |
I have a 4th table -
Donations
| DonationId (PK) | DonationAmount | ContributorId (FK) |
+-----------------+----------------+--------------------+
| 1 | 180.00 | 1 |
| 2 | 300.00 | 2 |
| 3 | 10.00 | 2 |
| 4 | 4000.00 | 2 |
This table is being used in the following form:
This was good as long I only needed Contributors.ContributorName to show up in the "Contributor" dropdown in the above screenshot.
(Note: ContributorId is currently bound to the first column of this query (row source)
SELECT c.ContributorId, c.ContributorName FROM Contributors c ORDER BY c.ContributorName;
and therefore displays the ContributorName Contributor dropdown, but actually saves only the ContributorId.)
However, a contributor may sometimes donate from their business and another time donate personally (or through their spouse) and they would want their tax receipt to reflect that.
I want the Contributor dropdown to display a union of ContributorName, SpouseName and BusinessName as follows:
George Wish
Jane Doe
John Doe
Karen Wish
Do Doe Dough, LLP
Georges Car Wash
Jane's Nail Salon
John's Pizza, Inc
and save the selection along with the ContributorId to the Donations table.
I created a new table:
ContributorNameTypes
| ContributorNameTypeId | ContributorNameType |
+-----------------------+-------------------------+
| 1 | ContributorName |
| 2 | ContributorSpouseName |
| 3 | ContributorBusinessName |
I was thinking of making 1 more table which would have
ContributorNames
| ContributorNameId | ContributorNameTypeId |
+-------------------+-----------------------+
with both fields combining as a PK.
The ContributorNameId in this table refers to one of the following - Contributors.ContributorId, ContributorSpouses.SpouseId, or
ContributorBusinesses.BusinessId.
However, I'm getting stuck here.
How do I make that relationship between one of the 3 name types and the tables they should connect to.
After that, how would I have everything show up in the drop down?

Is there a function/way in oracle sql to collect all the data from a column and turn it into a row according to an id?

with a simplified example I would have a database with IDs(and other values that are the same like names etc) and would have a list of cars they own
---------------
| Id | Value |
---------------
| 1 | BMW |
| 1 | Skoda |
| 1 | Lada |
| 2 | Audi |
| 2 | Fiat |
---------------
What I would like to achieve is
---------------------------------------
| Id | Column1 | Column2 | Column3 |
---------------------------------------
| 1 | BMW | Skoda | Lada |
| 2 | Audi | Fiat | (empty) |
---------------------------------------
So far I tried using Pivot but only managed to flip the values not collect them so I imagine maybe it should be put in a sub-query somehow I just couldn't figure out how

Teradata SQL Assistant - How can I pivot or transpose large tables with many columns and many rows?

I am using Teradata SQL Assistant Version TD 16.10.06.01 ...
I have seen a lot people transpose data for set smallish tables but I am working on thousands of clients and need the break the columns up into Line Item Values to compare orders/highlight differences between orders. Problem is it is all horizontally linked and I need to transpose it to Id,Transaction id,Version and Line Item Value 1, Line Item Value 2... then another column comparing values to see if they changed.
example:
+----+------------+-----------+------------+----------------+--------+----------+----------+------+-------------+
| Id | First Name | Last Name | DOB | transaction id | Make | Location | Postcode | Year | Price |
+----+------------+-----------+------------+----------------+--------+----------+----------+------+-------------+
| 1 | John | Smith | 15/11/2001 | 1654654 | Audi | NSW | 2222 | 2019 | $ 10,000.00 |
| 2 | Mark | White | 11/02/2002 | 1661200 | BMW | WA | 8888 | 2016 | $ 8,999.00 |
| 3 | Bob | Grey | 10/05/2002 | 1667746 | Ford | QLD | 9999 | 2013 | $ 3,000.00 |
| 4 | Phil | Faux | 6/08/2002 | 1674292 | Holden | SA | 1111 | 2000 | $ 5,800.00 |
+----+------------+-----------+------------+----------------+--------+----------+----------+------+-------------+
hoping to change the data to :
+----+----------+----------+----------+----------------+----------+----------+----------------+---------+-----+
| id | trans_id | Vers_ord | Item Val | Ln_Itm_Dscrptn | Org_Val | Updt_Val | Amndd_Ord_chck | Lbl_Rnk | ... |
+----+----------+----------+----------+----------------+----------+----------+----------------+---------+-----+
| 1 | 1654654 | 2 | 11169 | Make | Audi BLK | Audi WHT | Yes | 1 | |
| 1 | 1654654 | 2 | 11189 | Location | NSW | WA | Yes | 2 | |
| 1 | 1654654 | 2 | 23689 | Postcode | 2222 | 6000 | Yes | 3 | |
+----+----------+----------+----------+----------------+----------+----------+----------------+---------+-----+
Recently with smaller data I created a table added in Values then used a case statement when value 1 then xyz with a product join ... and the data warehouse admins didn't mention anything out of order. but I only had row 16 by 200 column table to transpose ( Sum, Avg, Count, Median(function) x 4 subsets of clients) , which were significantly smaller than my current tables to make comparisons with.
I am worried my prior method will probably slow the data Warehouse down, plus take me significant amount of time to type the SQL.
Is there a better way to transpose large tables?

SQL Server - Pivot Out Delimited Column Data Into Rows

I have two columns of delimited data that I would like to pivot out into individual rows for each data item. In the starting table below, the delimited data is represented in the DataPointA and DataPointB columns. Also, note that each ID is a unique identifier for each person. The starting table looks like this:
----------------------------------------------------------
| ID | FirstName | LastName | DataPointA | DataPointB |
----------------------------------------------------------
| A1234 | Bill | Jones | 1,3,7,8 | 1,4 |
| B5678 | Jane | Smith | 2,4,6,9 | 1,5 |
----------------------------------------------------------
I would like to take the DataPoint column data that is delimited by commas and create one row for each DataPoint value, while also condensing into one field. So the end result will look like this:
-------------------------------------------------------------
| ID | FirstName | LastName | DataPoint | DataPointType |
-------------------------------------------------------------
| A1234 | Bill | Jones | 1 | A |
| A1234 | Bill | Jones | 3 | A |
| A1234 | Bill | Jones | 7 | A |
| A1234 | Bill | Jones | 8 | A |
| A1234 | Bill | Jones | 1 | B |
| A1234 | Bill | Jones | 4 | B |
| B5678 | Jane | Smith | 2 | A |
| B5678 | Jane | Smith | 4 | A |
| B5678 | Jane | Smith | 6 | A |
| B5678 | Jane | Smith | 9 | A |
| B5678 | Jane | Smith | 1 | B |
| B5678 | Jane | Smith | 5 | B |
-------------------------------------------------------------
My first instinct was to use UNPIVOT but I am not able to get it to work on two columns. Is there another method I should be using? Thank you in advance.
You don't need pivoting. You need string splitting:
select t.ID, t.FirstName, t.LastName, v.*
from t cross apply
(select 'A' as DataPointType, a.value as DataPoint
from string_split(t.DataPointA, ',') a
union all
select 'B' as DataPointType, b.value as DataPoint
from string_split(t.DataPointB, ',') b
) ab;
string_split() is only available in the most recent versions of SQL Server. In older versions, you can use your own split function, which can readily be found on the web.

Postgres: select n unique rows for ID

Using Postgres I have a scenario where I need to return a variable number of rows for a each unique id in a sql statement.
Consider I have a table of the cars a user has owned over the years.
+----+----------+---------+-------+
| ID | make | model | type |
+----+----------+---------+-------+
| 1 | toyota | camry | sedan |
| 1 | ford | mustang | coupe |
| 1 | toyota | celica | coupe |
| 1 | bmw | z4 | coupe |
| 1 | honda | accord | sedan |
| 2 | buick | marque | sedan |
| 2 | delorean | btf | coupe |
| 2 | mini | cooper | coupe |
| 3 | ford | f-150 | truck |
| 3 | ford | mustang | coupe |
| 1 | ford | taurus | sedan |
+--------+----------+-------+-----+
From this table I'd only want to return two rows for each user that has a coupe and ignore the rest.
So something like. I'd also like to preserve the empty columns so the second result for ID 3 would be empty because there is only one car of type coupe. I am also working with restrictions as this has to run AWS Reshift. So, I can't use many functions. It seems this would be easy using a Top statement like in SQL server, but with Redshift restrictions and my lack of knowledge I'm not sure of the best way.
+----+----------+---------+-------+
| ID | make | model | type |
+----+----------+---------+-------+
| 1 | ford | mustang | coupe |
| 1 | toyota | celica | coupe |
| 2 | delorean | btf | coupe |
| 2 | mini | cooper | coupe |
| 3 | ford | mustang | coupe |
| 3 | | | |
+--------+----------+-------+-----+
Thanks a lot for your help.
As far as I know, Redshift supports window functions:
select id, make, model, type
from (
select id, make, model, type,
row_number() over (partition by id order by make) as rn
from the_table
where type = 'coupe'
) t
where rn <= 2
order by id, make;