Here is an example of data:
TABLE: DRIVERS
ID | FNAME | LNAME
------+-------+------
WR558 | WILL | RIKER
WW123 | WALT | WHITE
TABLE: ACCIDENTS
DRIVER | ACCIDENT_NBR | ACCI_DATE | ACCI_CITY | ACCI_ST
-------+--------------+------------+-----------+--------
WW123 | 4-7777 | 2014-01-01 | Chicago | IL
WW123 | 4-7782 | 2014-01-03 | Houston | TX
WW123 | 4-7988 | 2014-01-15 | El Paso | NM
There could be any number of accidents listed for this driver in this table or there could be none
What i need to see is this:
ID | FNAME | LNAME | ACCIDENT1_NBR | ACCI1_DATE | ACCI1_CITY | ACCI1_ST | ACCIDENT2_NBR | ACCI2_DATE | ACCI2_CITY | ACCI2_ST | ACCIDENT3_NBR | ACCI3_DATE | ACCI3_CITY | ACCI3_ST | ... | ACCIDENT10_NBR | ACCI10_DATE | ACCI10_CITY | ACCI10_ST
------+-------+--------+---------------+------------+------------+----------+---------------+
WR558 | WILL | RIKER | | | | | | ...
WW123 | WALT | WHITE | 4-7777 | 2014-01-01 | Chicago | IL | 4-7782 | ...
I need to pull the driver info and the 10 most recent accidents (if any) onto this one line. I'm unsure if I need to use a PIVOT table or a FETCH. Either way, I'm not sure how to iterate into the columns as needed.
Anyone's assistance is greatly appreciated!
I'm using SQL Server 2012 and this will ultimately get pulled into a .XLS SSRS file. The pipes I included are only for display and will not be part of the final result
You should use PIVOT for this as ugly as it will be it is the correct choice if coding a C# solution is not viable.
Here is an example: Simple Pivot sample
Related
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?
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
Consider below tables:
T_WORK
-------------------
| Work_id | Cre_d |
-------------------
| 1 | 2016 |
| 2 | 2017 |
| 3 | 2018 |
-------------------
T_WORK_PARAM
-----------------------------------
| Work_id | Param_nm | Param_val |
-----------------------------------
| 1 | Name | John |
| 1 | Place | London |
| 1 | Date | 01-01-2018 |
| 2 | Name | Trump |
| 2 | Place | Newyork |
| 2 | Date | 02-02-2018 |
-----------------------------------
I need an output in below format
-----------------------------------
| | Name | John |
| 1 | Place | London |
| | Date | 01-01-2018 |
-----------------------------------
| | Name | Trump |
| 2 | Place | Newyork |
| | Date | 02-02-2018 |
-----------------------------------
In Oracle, I can achieve this with this query:
SELECT
T1.Work_id,
CAST (MULTISET (SELECT Param_nm, Param_val
FROM T_work_param T2
WHERE T2.Work_id = T1.Work_id) AS type_param_tbl)
FROM
T_work T1
Where type_param_tbl is table of (Param_nm varhar2(1000), PAram_val varhar2(1000));
How to write a similar query in SQL Server ?
If it is not possible in SQL Server - what is the best/usual way to return the desired output to the caller (web service)?
The way to achieve the result the way you want is actually by pivoting the Param_nm column. This will produce your result into a tabular format where you could parse/map it into your application.
Please, click here for see a SQLFiddle that shows what I'm talking about.
I hope this may help you!
Unfortunately you cannot do that in SQL server.
You may consider just joining the tables for somewhat similar result.
I've created a very simple employee hierarchy in a matrix, looks something like:
Buildings
-------------------------------------------------
Employee | One | Two | R2D2 | Three |
----------------------------------------------------------------------
Miss Piggy | 15 | | | |
Walter Muppet | | 50 | | |
80's robot | | | 2 | |
Andy Pig | | | | 2 |
Randy Pig | | | | 1 |
Animal Muppet | 15 | 50 | 3 | |
...
...
The SQL is a recursive cte, returns: EmployeeName, ReportsToID, GroupLevel, Building, EmployeeID. In the matrix I have the employee column with these settings to create the hierarchy grouping. To get the count of employees by building in the matrix I have this expression:
=IIF(Fields!GroupLevel.Value="0",
IIF(InScope("Building"),
IIF(InScope("EmployeeName"),sum(Fields!Count.Value, "Building", Recursive),""),""),
IIF(Fields!GroupLevel.Value,Sum(Fields!Count.Value, "EmployeeName", recursive),""))
And so on, you can expand the hierarchy until there are no more managers. The count is the sum of all the direct and indirect reports in that sit in the respective building. etc.
What I want is to have the total of all employees in all buildings for each manager.
I can add row/column totals to get the sum for Miss Piggy, however I also want totals for all buildings for the other managers. Meaning Miss Piggy has people that sit in Buildings: Two, R2D2 and three. Animal Muppet has people that sit in Buildings: One, Two and Three.
Ideally I would like the report to look like this.
Buildings
-------------------------------------------------
Employee | One | Two | R2D2 | Three |
----------------------------------------------------------------------
Miss Piggy | 15 | 100 | 5 | 6 |
Walter Muppet | | 50 | | |
80's robot | | | 2 | |
Andy Pig | | | | 2 |
Randy Pig | | | | 1 |
Animal Muppet | 15 | 50 | | 3 |
... | | | | 2 |
... | | | 1 | |
Any ideas how to accomplish?
I have a table as follows
+----------------+----------+--------+
| purchase_order | text_seq | text |
+----------------+----------+--------+
| 1001 | 1 | screw |
| 1001 | 2 | m5x10 |
| 1001 | 3 | socket |
| 1002 | 1 | washer |
| 1002 | 2 | m5x10 |
+----------------+----------+--------+
From view need to get data as follows
+----------------+-------------------------+
| Purchase_order | text |
+----------------+-------------------------+
| 1001 | screw,m5x10,socket head |
| 1002 | washer,m5 |
+----------------+-------------------------+
There's not an easy way to do what you want. If you are using a recent version of PSQL, you can create a stored procedure or function to create the "text" field in the order you want (based on text_seq I would guess) and then use that result in the overall results.
My personal preference would be to retrieve the results from the database and then format them to my needs.