Pulling results as rows, easy conversion/translation to columns? - sql

I've been handed a task where I've been asked to make use of an SSIS 2008 package to create a CSV then FTP that CSV, following this tutorial here.
My query isn't quite ready.
I'm pulling results where the columns we're going after are coming out as rows. Been reading around on how to to do this with a cursor + stored procedure, but was wondering if anyone has conquered this obstacle with a simpler solution?
Query
SELECT
b.name as 'field',
a.value
FROM
[PROD_21C_Sitecore_Web].[dbo].[VersionedFields] a
INNER JOIN [PROD_21C_Sitecore_Web].[dbo].[Items] b
on a.FieldId = b.ID
WHERE
ItemId = '8C1D5767-FB1A-47A6-913C-E78AAC24ABC9'
Results
field value
-------------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
UnderGradYear 1972
Position1 <p>Cancer Centers of North Carolina, Asheville Hematology Oncology Associates - Medical Oncologist</p>
FellowshipCity1 Winston Salem, NC
FellowshipYear1 1981
__Updated by sitecore\cvance
Position3 <p>St. Joseph's Hospital - Subsection Chief of Hematology/ Oncology</p>
Languages {A5AB043B-86D3-4033-BEDD-928BCA0A13C3}
UnderGradCity Chapel Hill, NC
Interests <p></p>
Locations {6127E1AE-E2BF-4517-8BCB-46590AEB06A8}|{A7D2EA25-1B3F-48AE-B347-FC1E06F48202}
__Revision 22068c83-3504-4d89-a9bf-2a9e83a49ca3
LastName Paschal
FirstName Barton R.
MedicalSchoolCity Atlanta, GA
Fellowship1 <p>Bowman Gray School of Medicine - Medical Oncology</p>
FullName Barton R. paschal
UnderGradSchool University of North Carolina
Residency <p>Louisiana State University Medical Center - Internal Medicine</p>
ResidencyYear 1979
ResidencyCity Shreveport, LA
Specialities {B5B0F45A-E2BE-4F3F-92A6-C39C0D3016C5}|{4FCCE0BC-F3D4-4E27-AA7F-D436E61B2A1B}
AwardsHonors <p>Fellow, American College of Physicians, Phi Beta Kappa</p>
Position1City Asheville, NC
Position2City Asheville, NC
Title MD
__Created 20120509T085416
Internship <p>LSU Medical Center</p>
Photo <image mediaid="{C0F47AFC-CBAE-4043-A52F-BF8E344DC6DA}" mediapath="/Images/Physicians/paschal-web" src="~/media/Images/Physicians/paschal-web.jpg" alt="paschal" height="" width="" hspace="" vspace="" />
Position2 <p>Memorial Mission Hospital - Medical Oncologist & Chair, Department of Internal Medicine</p>
BoardCertifications Board Certified - Internal Medicine, Medical Oncology
Position3City Asheville, NC
__Updated 20130514T080506:635041155069332802
MedicalSchool Emory University School of Medicine - MD
Position4 <p>Hospice of Hendersonville County - Medical Director</p>
Position4City Henderson, NC
MedicalSchoolYear 1976
InternshipCity Shreveport, LA
InternshipYear 1979
ProfessionalAssociations {05E5C7FA-99DC-47C7-AC5E-2DA51D87DD1F}|{E519CAD2-C25F-4ADD-BD91-A4DEF861517B}|{67D7F01D-1695-4963-A149-EDF18354BBCC}
Thank you for looking.

Sounds like you want to PIVOT the data
SELECT
ItemId,
UnderGradYear,
Position1,
FellowshipCity1,
FellowshipYear1
-- add 'columns'
FROM
data -- this 'table' is the result of current query
PIVOT
(
MAX(value)
FOR field IN ([UnderGradYear], [Position1],
[FellowshipCity1], [FellowshipYear1]) -- add 'columns'
) PivotTable
demo
you can then use SSIS to create a CSV of result of above and FTP that file.
Edit: Alternatively you could also use the pivot transformation in SSIS rather than in T-SQL

Related

Merge values in a column (B) based on common values in column (A) in SQL table

Question: Using SQL, how would you Merge values in a column (B) based on common values in column (A)?
Table Structure: I have a SQL table (shown below), where Column A has ID's and Column B contains Text related to ID's and Column C contains Rank Order (the order in which text should be should be sorted).
ID
TEXT
RANK_ORDER
ABC001
ID: ABC001 - NEAREST LANDMARK - SHOPPING CENTRE
-999
ABC001
TRAVEL 80 M NORTH FROM SC
-900
ABC001
THROUGH PEDESTRIAN CROSSING
10.1
ABC002
ID: ABC002 - NEAREST LANDMARK - PUBLIC TOILET
-999
ABC002
TRAVEL 150 M NORTH FROM SC
-900
ABC002
THROUGH PARK ACCESS RD
10.1
ABC003
ID: ABC003 - NEAREST LANDMARK - REHABILITATION CENTRE
-999
ABC003
TRAVEL 1300M WEST FROM RC
-900
ABC003
THROUGH UNMADE RD
10.1
ABC003
LOCKED GATES
10.5
ABC003
CALL RC FOR ACCESS
20.1
Expected End Result: The resultant table should look like the table shown below:
ID
TEXT
ABC001
ID: ABC001 - NEAREST LANDMARK - SHOPPING CENTRETRAVEL 80 M NORTH FROM SCTHROUGH PEDESTRIAN CROSSING
ABC002
ID: ABC002 - NEAREST LANDMARK - PUBLIC TOILETTRAVEL 150 M NORTH FROM SCTHROUGH PARK ACCESS RD
ABC003
ID: ABC003 - NEAREST LANDMARK - REHABILITATION CENTRETRAVEL 1300M WEST FROM RCTHROUGH UNMADE RDLOCKED GATESCALL RC FOR ACCESS
In standard SQL, you can represent this as:
select id,
listagg(text, '; ') within group (order by rank_order) as text
from t
group by id;
Although most databases support this functionality, the exact syntax depends on the database -- as does inserting a newline character (which might also depend on the operating system).
Thanks Gordon, LISTAGG Works on SQLServer 2016 onwards, where I am on SQLServer 2012.
But I found STRING_AGG works well. The following query is the solution.
select ID, STRING_AGG(TEXT, CHAR(13)) within group (Order by RANK_ORDER) as TEXT
from t
GROUP BY ID
I am unable to get Carriage Return in results, it looks like SSMS issue.
Cheers,
Ankit

How to write a SQL query for showing route information from a flight database?

I have a set of flight data and I am trying to write a query (ex: recursive query using CTE) to show the No. of flights per routes, destination city, departure city, airline info, total time of delay per routes.
Currently I don't know a way to group total number of flights per route for each airline. I also have trouble grouping totaltimedelay for each airline's routes.
Sample flight data info - Four columns total (All the data below are from the fact table in OLAP database)
AirlineName DepartureCity DestinationCity TimeDelay(min) FlightID
CA NY CA 9 389
OA NJ TX 8 321
AA SEA NY 10 231
UA NY CA 20 098
HA NJ TX 15 321
OA NJ TX 20 123
< Expected output: 5 columns >
AirlineName DeparCity DestiCity TotalNumberofFlights TotaltimeDelay
Thanks a lot I hope I made it clear enough. Any sort of help or direction would be appreciated.
A simple GROUP BY should be enough...
SELECT
AirlineName,
DepartureCity AS DeparCity,
DestinationCity AS DestiCity,
COUNT(*) AS TotalNumberofFlights,
SUM(TimeDelay) AS TotaltimeDelay
FROM Flight
GROUP BY
AirlineName,
DepartureCity,
DestinationCity
Click here to see it in action & have a play in SqlFiddle.com

SQL Oracle - display values only once per column

I'm trying to format a select statement. The assignment specifies that it has to be formatted this way.
I have a database regarding a taxi service. I have to put together a view with the company name, passenger name, and taxi number. Easy. However, the output specifies that the company name should only appear once in the output, at the top of it's own group. So I have:
CREATE VIEW TAXITRIPS(COMPANYNAME, PASSENGERNAME, TAXI#) AS
(SELECT COMPANY.NAME, BOOKING.NAME, VEHICLES.TAXI#
FROM BOOKING JOIN VEHICLES ON BOOKING.TAXI# = VEHICLES.TAXI#
RIGHT OUTER JOIN COMPANY ON VEHICLES.NAME = COMPANY.NAME);
The right outer join is so that companies with no booking recorded are still displayed. If I now run:
SELECT * FROM TAXITRIPS ORDER BY COMPANYNAME ASC;
It will give me something like
COMPANYNAME PASSENGERNAME TAXI#
---------------------------------------------
ABC TAXIS DAVE 192
LEGION CABS
PREMIER CABS SHANE 2154
PREMIER CABS TIM 2169
SILVER SERVICE DAVE 18579
SILVER SERVICE TIM 18124
SILVER SERVICE AARON 18917
No result for legion cabs, all field displayed, et cetera. Assignment specification says it has to look like this.
COMPANYNAME PASSENGERNAME TAXI#
---------------------------------------------
ABC TAXIS DAVE 192
LEGION CABS
PREMIER CABS SHANE 2154
TIM 2169
SILVER SERVICE DAVE 18579
TIM 18124
AARON 18917
The company name should only be displayed on its first row. DISTINCT is not helping. Any advice?
Normally, you would do this at the application layer, because the result set relies on the ordering of the rows -- a bad thing in SQL.
But you can do it as:
SELECT (CASE WHEN ROW_NUMBER() OVER (PARTITION BY c.NAME ORDER BY v.TAXI#) = 1
THEN c.NAME
END) as CompanyName, b.NAME, v.TAXI#
FROM COMPANY c LEFT JOIN
VEHICLES v
ON v.NAME = c.NAME LEFT JOIN
BOOKING b
ON b.TAXI# = v.FLIGHT#
ORDER BY c.name, v.taxi#;
Note: I rearranged the joins to be LEFT JOINs. Most people find that easier to follow than RIGHT JOINs.

Trying to combine multiple rows of result set into single row

select C.customerId,(C.lastName+', '+C.firstName) as CustomerName, C.companyName,
D.companyName+' ('+D.lastName+','+D.firstName+')'
as "Parent CompanyName(Last, First)",S.siteId, S.nickName as siteName,
dbo.GetSiteTelemetryBoxList(s.siteId) as "DeviceId's",
dbo.GetSiteTelemetryBoxSKUList(S.siteId,0) as SKU
from Site S
INNER JOIN Customer C ON S.customerId = C.customerId
INNER JOIN Customer D ON D.customerId = C.parentCustomerId
where S.createDate between DATEADD(DAY, -65, GETUTCDATE()) and GETUTCDATE()
order by C.customerId, S.siteId
The above query returns values that look like this:
CID CustomerName companyName Parent CompanyName(Last, First) SiteName DeviceId SKU
888296 DeYoung, Scott DeYoung Farms Mercier Valley Irrigation (Mercier,Ralph) H E east 200241 NETB12WR
890980 Rust, Marcus NULL Chester Inc. (Young,Scott) Byroad east 346370 NETB12WR
890980 Rust, Marcus NULL Chester Inc. (Young,Scott) Byroad west 345431 NETB12WR
891094 Pirani, Mark A Pirani Farm AMX Irrigation (Burroughs,Michael) hwy 64 south 333721 UNKNOWN
891094 Pirani, Mark A Pirani Farm AMX Irrigation (Burroughs,Michael) HWY 64 North 250162 NETB12WR
891094 Pirani, Mark A Pirani Farm AMX Irrigation (Burroughs,Michael) HWY 64 West 250164 NETB12WR
891094 Pirani, Mark A Pirani Farm AMX Irrigation (Burroughs,Michael) HWY 64 East 250157 NETB12WR
891430 Gammil, Bob Gammil FArms AMX Irrigation (Burroughs,Michael) angel 333677 UNKNOWN
891430 Gammil, Bob Gammil FArms AMX Irrigation (Burroughs,Michael) cemetery 333564 UNKNOWN
The problem I face now is that if a customerId/Name is repeating in the result set. The SiteName, deviceId, SKU should be concatenated to represent the data as one value.
For example, Mark Pirani row would look like
CID CustomerName ... SiteName DeviceId's ...
891904 Pirani, Mark ... hwy 64 south, HWY 64 North, HWY 64 West, HWY 64 East 333721,250162,250164,250157 ...
You can convert the rows with something like this to transform the rows into a concatenated string:
select
distinct
stuff((
select ',' + u.username
from users u
where u.username = username
order by u.username
for xml path('')
),1,1,'') as userlist
from users
group by username
I believe this is more of a SQL query issue than a C# code issue, or more appropriately I believe it more efficient to solve this problem at the query level rather than the code level. Off the top of my head you can use SELECT DISTINCT or GROUP BY clauses.
Here is another StackOverflow question addressing this issue - How do I (or can I) SELECT DISTINCT on multiple columns?
I did some digging and found a few ways to implement it. Basically, the simple solution for this is using mysql's group_concat function. These links discuss how the group_concat can be implemented for SQL server. You can choose one based on your requirements.
Simulating group_concat MySQL function in Microsoft SQL Server 2005? -- This thread discusses a few ways to implement it.
Flatten association table to multi-value column? -- This thread discusses the CLR implemenation of it.
http://groupconcat.codeplex.com/ -- This was just perfect for me. Exactly what I was looking for. The project basically creates four aggregate functions that collectively offer similar functionality to the MySQL GROUP_CONCAT function.

SQLPlus - Count function across several tables

i am trying to count the number of riders in my data. and i am having trouble figuring this out. sample of my output is noted below. The data comes from many different tables and I had to join the tables which is not the problem I am having. I am trying to get the count number of RIDERS by EVENT by DESCRIPTION. And still display the columns as noted below.
SQL> SELECT EVENTNAME, DESCRIPTION, RIDERS2 FROM ERP_REPORT;
EVENTNAME DESCRIPTION RIDERS
------------------------------ ------------------------------
Ace Rental Car - Fair Hill Inv Day 1 Race on Fair Hill's Easy Aaron Adams
itational level Course
Ace Rental Car - Fair Hill Inv Day 1 Race on Fair Hill's Easy Aaron Adams
itational level Course
Ace Rental Car - Fair Hill Inv Day 2 Race on Fair Hill's Inte Aaron Adams
itational rmediate level Course
Huffy's Mountain Trip Weekend 1 Race 1 on Huffy Moun Sam Adams
tain's Easy level Course
Valley Spring Water Mountain B Day 3 Race on Hoola Hut Gorge' Jay Gillgan
ike Extravaganza s Intermediate level Course
I have tried a bunch of different code but this is one Sample I have tried. I am lost on this so any help would huge!
SELECT COUNT(DISTINCT RIDERS) as "RIDERS"
2 FROM ERP_REPORT;
Are you looking for something like:
SELECT COUNT(riders) AS rider_count, eventname, description
FROM erp_report
GROUP BY eventname, description;