Click here to monitor SSC
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in
Home       Members    Calendar    Who's On

Add to briefcase

Flatten results from a SQL table Expand / Collapse
Posted Saturday, February 22, 2014 2:34 PM


Group: General Forum Members
Last Login: Thursday, August 21, 2014 7:50 PM
Points: 19, Visits: 111
Hi all,
I have a table that contains phone number and email addresses, this table was created in a way that every time a new phone number or email address is created, for the same client, it creates a new row.

I need a way to get a report that shows all the phone numbers and email addresses per client.

UniqID, UniqClient, PhoneNumber, EmailWeb
3, 66, 4164445555
5, 66, ,

In this example, the first row contains a phone number and the second row the email address (for the same client - 66).

I need to get one row with phone number and email address.

Any help is truly appreciated.

Thank you very much.

Post #1544261
Posted Saturday, February 22, 2014 4:17 PM

SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 1:12 PM
Points: 4,013, Visits: 4,735
This is one simple solution (didn't count on the performance):

create table CustInfo
(UniqId int identity(1,1),
UniqClient int,
PhoneNumber varchar(20),
EmailWeb varchar(50)
insert into CustInfo (UniqClient,PhoneNumber,EmailWeb)

select top(1) t.UniqClient,
(select top (1) t2.PhoneNumber from dbo.CustInfo t2 where t2.UniqClient = t.UniqClient and len(isnull(t2.PhoneNumber,'')) > 0) as PhoneNumber,
(select top (1) t3.EmailWeb from dbo.CustInfo t3 where t3.UniqClient = t.UniqClient and len(isnull(t3.EmailWeb,'')) > 0) as EmailWeb
from dbo.CustInfo t
where t.UniqClient = 66

You can add an ORDER BY if you need a reason for.


Igor Micev,
SQL Server developer at Seavus
Post #1544265
Posted Sunday, February 23, 2014 6:02 PM

Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Wednesday, February 24, 2016 6:28 AM
Points: 3,977, Visits: 6,431
Perhaps this?

WITH SampleData (UniqID, UniqClient,PhoneNumber,EmailWeb) AS
SELECT 3, 66,'4164445555','' UNION ALL SELECT 5, 66,'',''
SELECT UniqClient, PhoneNumber=MAX(PhoneNumber), EmailWeb=MAX(EmailWeb)
FROM SampleData
GROUP BY UniqClient;

My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Post #1544334
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse