How to convert each column in a dataframe to a row with ColumnName and ColumnValue - scala

I need to convert the following dataframe:
╔══════╦════════╦════════╦════════╗
║ Year ║ ColA ║ ColB ║ ColC ║
╠══════╬════════╬════════╬════════╣
║ 2017 ║ 1 ║ 2 ║ 3 ║
║ 2018 ║ 4 ║ 5 ║ 6 ║
║ 2019 ║ 7 ║ 8 ║ 9 ║
╚══════╩════════╩════════╩════════╝
Into this:
╔══════╦════════╦═══════╗
║ Year ║ColName ║ Value ║
╠══════╬════════╬═══════╣
║ 2017 ║ ColA ║ 1 ║
║ 2017 ║ ColB ║ 2 ║
║ 2017 ║ ColC ║ 3 ║
║ 2018 ║ ColA ║ 4 ║
║ 2018 ║ ColB ║ 5 ║
║ 2018 ║ ColC ║ 6 ║
║ 2019 ║ ColA ║ 7 ║
║ 2019 ║ ColB ║ 8 ║
║ 2019 ║ ColC ║ 9 ║
╚══════╩════════╩═══════╝
This needs to support any number of columns besides the first "Year" one, which could be 1 or many. And it should be a generic solution, meaning it should not use hard-coded column names anywhere, but it should read the column names directly from the original dataframe.
I'm using Databricks with a notebook written in Scala. Very new to both Spark and Scala.
UPDATE
I've found this solution in Python that works well, but I'm having a hard time converting it to Scala.
def columnsToRows(df, by):
# Filter dtypes and split into column names and type description.
# Only get columns not in "by".
cols, dtypes = zip(*((c, t) for (c, t) in df.dtypes if c not in by))
# Create and explode an array of (column_name, column_value) structs
kvs = F.explode(F.array([
F.struct(F.lit(c.strip()).alias("ColName"), F.col(c).alias("Value")) for c in cols
])).alias("kvs")
return df.select(by + [kvs]).select(by + ["kvs.ColName", "kvs.Value"])

You can use stack to transpose the data
val fixedColumns = Seq("Year", "FixedColumn")
val cols = df.columns
.filter(c => !(fixedColumns.contains(c)))
.map(c => (s"'${c}', ${c}" ))
val exp= cols.mkString(s"stack(${cols.size}, ", "," , ") as (Point, Value)")
df.select($"Year", expr(exp))
Output:
+----+------+-----+
|Year|Point |Value|
+----+------+-----+
|2017|PointA|1 |
|2017|PointB|2 |
|2017|PointC|3 |
|2018|PointA|4 |
|2018|PointB|5 |
|2018|PointC|6 |
|2019|PointA|7 |
|2019|PointB|8 |
|2019|PointC|9 |
+----+------+-----+

Your python-code translates like this:
val colsToKeep = Seq("year").map(col)
val colsToTransform = Seq("colA","colB","colC")
df.select((colsToKeep :+
explode(
array(colsToTransform.map(c => struct(lit(c).alias("colName"),col(c).alias("colValue"))):_*)
).as("NameValue")):_*)
.select((colsToKeep :+ $"nameValue.colName":+$"nameValue.colValue"):_*)
.show()

Related

PostgreSQL Select filled columns

I have a database where not all columns will be filled. I just want to select columns with minimum one value. So if the column is empty from top to bottom it should not be selected. All I could find looking for an answer where searches along the rows so if a column is empty the hole row will not be selected.
╔═══════════════╦════════════╦══════════════╦════════════╦═════════════╦════════════════╦════════════════╗
║ "receiver_id" ║ "gps_week" ║ "gps_second" ║ "latitude" ║ "longitude" ║ "altitude_msl" ║ "altitude_hae" ║
╠═══════════════╬════════════╬══════════════╬════════════╬═════════════╬════════════════╬════════════════╣
║ 1 ║ ║ ║ 38.0517465 ║ 15.660851 ║ ║ 691.379883 ║
║ 1 ║ ║ ║ 38.0517465 ║ 15.660851 ║ ║ 691.389404 ║
║ 1 ║ ║ ║ 38.0517465 ║ 15.660851 ║ ║ 691.402344 ║
║ 1 ║ ║ ║ 38.0517465 ║ 15.6608509 ║ ║ 691.413818 ║
║ 1 ║ ║ ║ 38.0517465 ║ 15.6608508 ║ ║ 691.425659 ║
╚═══════════════╩════════════╩══════════════╩════════════╩═════════════╩════════════════╩════════════════╝
In the example table I want to return
receiver_id, latitude, longitude, altitude_hae

What CTE would generate listed results or is there a better way to aggregate the names in the tables provided

Is there a CTE that can be written to query these tables and to generate the listed results? If so what would it look like?
This is SQL Serve 2012 SP2.
create table d ( id int, calldate date)
insert into d
values (3, '2016-08-03'), (4, '2016-08-04'), (5, '2016-08-05'),
(6, '2016-08-06'), (7, '2016-08-07'), (8, '2016-08-08'),
(9, '2016-08-09'), (10, '2016-08-10'), (11, '2016-08-11'),
(12, '2016-08-12'), (13, '2016-08-13'), (14, '2016-08-14'),
(15, '2016-08-15'), (16, '2016-08-16'), (17, '2016-08-17'),
(18, '2016-08-18'), (19 , '2016-08-19' ), (20 , '2016-08-20' ), (21 , '2016-08-21' ), (22 , '2016-08-22' ), (23 , '2016-08-23' ), (24 , '2016-08-24' ), (25 , '2016-08-25' ), (26 , '2016-08-26' ), (27 , '2016-08-27' ), (28 , '2016-08-28' ), (29 , '2016-08-29' ), (30 , '2016-08-30');
create table w ( id int, startdate date, enddate date, holiday varchar(60))
insert into w
values (1, '2016-05-02', '2016-05-08', ''),
(2, '2016-05-09', '2016-05-15', 'Company holiday'),
(3, '2016-05-16', '2016-05-22', 'Company holiday'),
(4, '2016-05-23', '2016-05-29', ''),
(5, '2016-05-30', '2016-06-05', ''), (6, '2016-06-06', '2016-06-12', 'Company holiday'), (7, '2016-06-13', '2016-06-19', ''), (8, '2016-06-20', '2016-06-26', ''), (9, '2016-06-27', '2016-07-03', ''), (10, '2016-07-04', '2016-07-10', 'Company holiday'), (11, '2016-07-11', '2016-07-17', ''), (12, '2016-07-18', '2016-07-24', ''), (13, '2016-07-25', '2016-07-31', ''), (14, '2016-08-01', '2016-08-07', ''), (15, '2016-08-08', '2016-08-14', ''), (16, '2016-08-15', '2016-08-21', ''), (17, '2016-08-22', '2016-08-28', ''), (18, '2016-08-29', '2016-09-04', ''), (19, '2016-09-05', '2016-09-11', '')
create table e (id int, name varchar(100), init varchar(10), IsAManager bit)
insert into e values (2 , 'Alice' , 'AA', 0), (8 , 'Jack ' , 'JM', 1), (9 , 'Ace ' , 'AQ', 0), (10 , 'Mike ' , 'MM', 0), (16 , 'George' ,'GH', 0), (21 , 'Jenny' , 'JL', 1), (22 , 'Bill ' , 'BG', 1), (30 , 'Blank' , ' ', 0)
CREATE TABLE r ( id INT, NAME VARCHAR(50) );
INSERT INTO r VALUES (13, 'Primary'), (22, 'Secondary'), (33, 'Prim Trd'), (44, 'Sec Trd');
create table ca (e_id int, r_id int, d_id int, s_id int)
insert into ca values (8 ,13 ,8 ,13), (8 ,13 ,9 ,13), (8 ,13 ,10 ,13), (8 ,13 ,11 ,13), (8 ,13 ,12 ,13), (8 ,13 ,13 ,13), (8 ,13 ,14 ,13), (10 ,13 ,15 ,13), (10 ,13 ,16 ,13), (10 ,13 ,17 ,13), (10 ,13 ,18 ,13), (10 ,13 ,19 ,13),
(10 ,13 ,20 ,13), (10 ,13 ,21 ,13), (16 ,13 ,22 ,13), (16 ,13 ,23 ,13), (16 ,13 ,24 ,13), (16 ,13 ,25 ,13), (16 ,13 ,26 ,13), (16 ,13 ,27 ,13), (16 ,13 ,28 ,13), (10 ,22 ,8 ,13), (10 ,22 ,9 ,13), (10 ,22 ,10 ,13),
(10 ,22 ,11 ,13), (10 ,22 ,12 ,13), (10 ,22 ,13 ,13), (10 ,22 ,14 ,13), (16 ,22 ,15 ,13), (16 ,22 ,16 ,13), (16 ,22 ,17 ,13), (16 ,22 ,18 ,13), (16 ,22 ,19 ,13), (16 ,22 ,20 ,13), (16 ,22 ,21 ,13), (2 ,22 ,22 ,13),
(2 ,22 ,23 ,13), (2 ,22 ,24 ,13), (2 ,22 ,25 ,13), (2 ,22 ,26 ,13), (2 ,22 ,27 ,13), (2 ,22 ,28 ,13), (30 ,33 ,8 ,13), (30 ,33 ,9 ,13), (30 ,33 ,10 ,13), (30 ,33 ,11 ,13), (30 ,33 ,12 ,13), (30 ,33 ,13 ,13),
(30 ,33 ,14 ,13), (30 ,33 ,15 ,13), (21 ,33 ,16 ,13), (22 ,33 ,17 ,13), (30 ,33 ,18 ,13), (30 ,33 ,19 ,13), (30 ,33 ,20 ,13), (30 ,33 ,21 ,13), (2 ,33 ,22 ,13), (30 ,33 ,23 ,13), (30 ,33 ,24 ,13), (30 ,33 ,25 ,13),
(30 ,33 ,26 ,13), (30 ,33 ,27 ,13), (30 ,33 ,28 ,13), (30 ,44 ,8 ,13), (30 ,44 ,9 ,13), (30 ,44 ,10 ,13), (30 ,44 ,11 ,13), (30 ,44 ,12 ,13), (30 ,44 ,13 ,13), (30 ,44 ,14 ,13), (30 ,44 ,15 ,13), (30 ,44 ,16 ,13),
(30 ,44 ,17 ,13), (30 ,44 ,18 ,13), (30 ,44 ,19 ,13), (30 ,44 ,20 ,13), (30 ,44 ,21 ,13), (2 ,44 ,22 ,13), (2 ,44 ,23 ,13), (21 ,44 ,24 ,13), (21 ,44 ,25 ,13), (21 ,44 ,26 ,13), (30 ,44 ,27 ,13), (30 ,44 ,28 ,13)
CREATE TABLE s ( id INT, specialty VARCHAR(50) );
insert into s values (13 , 'Pediatric') , (28 , 'EMT'), (55 , 'ER'), (62 , 'ICU'), (74 , 'GIU'), (88 , 'BBB')
Table W
║ id │ startdate │ enddate ║
╠════╪════════════╪════════════╣
║ 11 │ 2016-07-11 │ 2016-07-17 ║
║ 12 │ 2016-07-18 │ 2016-07-24 ║
║ 13 │ 2016-07-25 │ 2016-07-31 ║
║ 14 │ 2016-08-01 │ 2016-08-07 ║
║ 15 │ 2016-08-08 │ 2016-08-14 ║
║ 16 │ 2016-08-15 │ 2016-08-21 ║
║ 17 │ 2016-08-22 │ 2016-08-28 ║
║ 18 │ 2016-08-29 │ 2016-09-04 ║
║ 19 │ 2016-09-05 │ 2016-09-11 ║
Table D
║ id │ calldate ║
╠════╪════════════╣
║ 3 │ 2016-08-03 ║
║ 4 │ 2016-08-04 ║
║ 5 │ 2016-08-05 ║
║ 6 │ 2016-08-06 ║
║ 7 │ 2016-08-07 ║
║ 8 │ 2016-08-08 ║
║ 9 │ 2016-08-09 ║
║ 10 │ 2016-08-10 ║
║ 11 │ 2016-08-11 ║
║ 12 │ 2016-08-12 ║
║ 13 │ 2016-08-13 ║
║ 14 │ 2016-08-14 ║
║ 15 │ 2016-08-15 ║
║ 16 │ 2016-08-16 ║
║ 17 │ 2016-08-17 ║
║ 18 │ 2016-08-18 ║
║ 19 │ 2016-08-19 ║
║ 20 │ 2016-08-20 ║
║ 21 │ 2016-08-21 ║
║ 22 │ 2016-08-22 ║
║ 23 │ 2016-08-23 ║
║ 24 │ 2016-08-24 ║
║ 25 │ 2016-08-25 ║
║ 26 │ 2016-08-26 ║
║ 27 │ 2016-08-27 ║
║ 28 │ 2016-08-28 ║
║ 29 │ 2016-08-29 ║
║ 30 │ 2016-08-30 ║
Table E
╔════╤════════╤═════════╤════════════╗
║ id │ name │ initial │ IsAManager ║
╠════╪════════╪═════════╪════════════╣
║ 2 │ Alice │ AA │ 0 ║
║ 8 │ Jack │ JM │ 1 ║
║ 9 │ Ace │ AQ │ 0 ║
║ 10 │ Mike │ MM │ 0 ║
║ 16 │ George │ GH │ 0 ║
║ 21 │ Jenny │ JL │ 1 ║
║ 22 │ Bill │ BG │ 1 ║
║ 30 │ Blank │ │ 0 ║
╚════╧════════╧═════════╧════════════╝
Table R
╔════╤═══════════╗
║ id │ name ║
╠════╪═══════════╣
║ 13 │ Primary ║
║ 22 │ Secondary ║
║ 33 │ Prim Trd ║
║ 44 │ Sec Trd ║
╚════╧═══════════╝
Table S
|ID | Specialty |
|---+-----------|
|13 | Pediatric |
|28 | EMT |
|55 | ER |
|62 | ICU |
|74 | GIU |
|88 | BBB |
Here is the linking table that links these tables together.
Table CA
║ e_id │ r_id │ d_id │ s_id ║
╠══════╪══════╪══════╪══════╣
║ 8 │ 13 │ 8 │ 13 ║
║ 8 │ 13 │ 9 │ 13 ║
║ 8 │ 13 │ 10 │ 13 ║
║ 8 │ 13 │ 11 │ 13 ║
║ 8 │ 13 │ 12 │ 13 ║
║ 8 │ 13 │ 13 │ 13 ║
║ 8 │ 13 │ 14 │ 13 ║
║ 10 │ 13 │ 15 │ 13 ║
║ 10 │ 13 │ 16 │ 13 ║
║ 10 │ 13 │ 17 │ 13 ║
║ 10 │ 13 │ 18 │ 13 ║
║ 10 │ 13 │ 19 │ 13 ║
║ 10 │ 13 │ 20 │ 13 ║
║ 10 │ 13 │ 21 │ 13 ║
║ 16 │ 13 │ 22 │ 13 ║
║ 16 │ 13 │ 23 │ 13 ║
║ 16 │ 13 │ 24 │ 13 ║
║ 16 │ 13 │ 25 │ 13 ║
║ 16 │ 13 │ 26 │ 13 ║
║ 16 │ 13 │ 27 │ 13 ║
║ 16 │ 13 │ 28 │ 13 ║
║ 10 │ 22 │ 8 │ 13 ║
║ 10 │ 22 │ 9 │ 13 ║
║ 10 │ 22 │ 10 │ 13 ║
║ 10 │ 22 │ 11 │ 13 ║
║ 10 │ 22 │ 12 │ 13 ║
║ 10 │ 22 │ 13 │ 13 ║
║ 10 │ 22 │ 14 │ 13 ║
║ 16 │ 22 │ 15 │ 13 ║
║ 16 │ 22 │ 16 │ 13 ║
║ 16 │ 22 │ 17 │ 13 ║
║ 16 │ 22 │ 18 │ 13 ║
║ 16 │ 22 │ 19 │ 13 ║
║ 16 │ 22 │ 20 │ 13 ║
║ 16 │ 22 │ 21 │ 13 ║
║ 2 │ 22 │ 22 │ 13 ║
║ 2 │ 22 │ 23 │ 13 ║
║ 2 │ 22 │ 24 │ 13 ║
║ 2 │ 22 │ 25 │ 13 ║
║ 2 │ 22 │ 26 │ 13 ║
║ 2 │ 22 │ 27 │ 13 ║
║ 2 │ 22 │ 28 │ 13 ║
║ 30 │ 33 │ 8 │ 13 ║
║ 30 │ 33 │ 9 │ 13 ║
║ 30 │ 33 │ 10 │ 13 ║
║ 30 │ 33 │ 11 │ 13 ║
║ 30 │ 33 │ 12 │ 13 ║
║ 30 │ 33 │ 13 │ 13 ║
║ 30 │ 33 │ 14 │ 13 ║
║ 30 │ 33 │ 15 │ 13 ║
║ 21 │ 33 │ 16 │ 13 ║
║ 22 │ 33 │ 17 │ 13 ║
║ 30 │ 33 │ 18 │ 13 ║
║ 30 │ 33 │ 19 │ 13 ║
║ 30 │ 33 │ 20 │ 13 ║
║ 30 │ 33 │ 21 │ 13 ║
║ 2 │ 33 │ 22 │ 13 ║
║ 30 │ 33 │ 23 │ 13 ║
║ 30 │ 33 │ 24 │ 13 ║
║ 30 │ 33 │ 25 │ 13 ║
║ 30 │ 33 │ 26 │ 13 ║
║ 30 │ 33 │ 27 │ 13 ║
║ 30 │ 33 │ 28 │ 13 ║
║ 30 │ 44 │ 8 │ 13 ║
║ 30 │ 44 │ 9 │ 13 ║
║ 30 │ 44 │ 10 │ 13 ║
║ 30 │ 44 │ 11 │ 13 ║
║ 30 │ 44 │ 12 │ 13 ║
║ 30 │ 44 │ 13 │ 13 ║
║ 30 │ 44 │ 14 │ 13 ║
║ 30 │ 44 │ 15 │ 13 ║
║ 30 │ 44 │ 16 │ 13 ║
║ 30 │ 44 │ 17 │ 13 ║
║ 30 │ 44 │ 18 │ 13 ║
║ 30 │ 44 │ 19 │ 13 ║
║ 30 │ 44 │ 20 │ 13 ║
║ 30 │ 44 │ 21 │ 13 ║
║ 2 │ 44 │ 22 │ 13 ║
║ 2 │ 44 │ 23 │ 13 ║
║ 21 │ 44 │ 24 │ 13 ║
║ 21 │ 44 │ 25 │ 13 ║
║ 21 │ 44 │ 26 │ 13 ║
║ 30 │ 44 │ 27 │ 13 ║
║ 30 │ 44 │ 28 │ 13 ║
Note: The e.id is the id of the last person who performed the trade either "Prim Trd" or "Sec Trd" for that week with disregard to id 30 blank. But if no trades happened for that week then e.id= 30 blank. Where initials are a list of initials that did a trade for that week. Same for names
Desired Result Set:
║ startdate │ enddate │ holiday │ specialty │ initials │ rolename │ s.id │ e.id │ name │ WasThisTraded ║
║ 2016-08-08 │ 2016-08-14 │ │ Pediatric │ JM │ Primary │ 13 │ 8 │ Jack │ N ║
║ 2016-08-08 │ 2016-08-14 │ │ Pediatric │ MM │ Secondary │ 13 │ 10 │ Mike │ N ║
║ 2016-08-08 │ 2016-08-14 │ │ Pediatric │ │ Prim Trd │ 13 │ 30 │ Blank │ N ║
║ 2016-08-08 │ 2016-08-14 │ │ Pediatric │ │ Sec Trd │ 13 │ 30 │ Blank │ N ║
║ 2016-08-15 │ 2016-08-21 │ │ Pediatric │ MM │ Primary │ 13 │ 10 │ Mike │ Y ║
║ 2016-08-15 │ 2016-08-21 │ │ Pediatric │ GH │ Secondary │ 13 │ 16 │ George │ N ║
║ 2016-08-15 │ 2016-08-21 │ │ Pediatric │ JL, BG │ Prim Trd │ 13 │ 22 │ Jenny, Bill │ N ║
║ 2016-08-15 │ 2016-08-21 │ │ Pediatric │ JL │ Sec Trd │ 13 │ 30 │ Jenny │ N ║
║ 2016-08-22 │ 2016-08-28 │ │ Pediatric │ GH │ Primary │ 13 │ 16 │ George │ Y ║
║ 2016-08-22 │ 2016-08-28 │ │ Pediatric │ AA │ Secondary │ 13 │ 2 │ Alice │ Y ║
║ 2016-08-22 │ 2016-08-28 │ │ Pediatric │ AA │ Prim Trd │ 13 │ 2 │ Alice │ N ║
║ 2016-08-22 │ 2016-08-28 │ │ Pediatric │ AA, JL │ Sec Trd │ 13 │ 21 │ Alice, Jenny │ N ║

Select multiple data from other table for each id separated by comma

Table one - workorder
╔══════════╦══════════════╦══════════════╗
║ id ║ wpeople ║ start_date ║
╠══════════╬══════════════╬══════════════╣
║ 1 ║ 1,2,4 ║ 02.08.2016 ║
║ 2 ║ 4,5 ║ 28.09.2016 ║
╚══════════╩══════════════╩══════════════╝
Table two - employees
╔══════════╦═════════════════╗
║ id ║ name ║
╠══════════╬═════════════════╣
║ 1 ║ John ║
║ 2 ║ Ben ║
║ 3 ║ Ian ║
║ 4 ║ Hank ║
║ 5 ║ George ║
╚══════════╩═════════════════╝
Output selection for who need to work at the project
╔══════════╦════════════════╦════════════╗
║ 1 ║ John,Ben,Hank ║ 02.08.2016 ║
║ 2 ║ Hank,George ║ 28.09.2016 ║
╚══════════╩════════════════╩════════════╝
I have tried with GROUP_CONCAT and FIND_IN_SET
SELECT w.id,
GROUP_CONCAT(e.name ORDER BY e.id) workorder
FROM workorder w
INNER JOIN employees e
ON FIND_IN_SET(e.id, a.wpeople) > 0
GROUP BY w.id
But the output it's
╔══════════╦════════════════╦════════════╗
║ 1 ║ John ║ 02.08.2016 ║
║ 1 ║ Ben ║ 02.08.2016 ║
║ 1 ║ Hank ║ 02.08.2016 ║
║ 2 ║ Hank ║ 28.09.2016 ║
║ 2 ║ George ║ 28.09.2016 ║
╚══════════╩════════════════╩════════════╝
I search on google for this and the solution it's GROUP_CONCAT - FIND_IN_SET. Can be that I didn't understand very well this function.
Thanks for you time!
Stefan
For anyone who will need this:
I added a new table werkbon_employee
╔══════════╦═══════════════════╦═══════════════╗
║ id ║ workorder_id ║ employee_id ║
╠══════════╬═══════════════════╬═══════════════╣
║ 1 ║ 1 ║ 1 ║
║ 2 ║ 1 ║ 2 ║
║ 3 ║ 1 ║ 4 ║
║ 4 ║ 2 ║ 4 ║
║ 5 ║ 2 ║ 5 ║
╚══════════╩═══════════════════╩═══════════════╝
I used to select
SELECT *,
GROUP_CONCAT(e.name ORDER BY e.id) ename
FROM werkbon
LEFT JOIN werkbon_employee we ON werkbon.id = we.werkbon_id
INNER JOIN employees e ON FIND_IN_SET(e.id, we.employee_id) > 0
GROUP BY werkbon.id DESC LIMIT 1
Now the result it's
Werk mensen
John,Ben,Hank
Datum
02.08.2016
Thanks to #MarcB for help

Google Sheets: How do I IMPORTRANGE only if a corresponding cell in the same row is populated?

I have two Google spreadsheets. Three columns on the second spreadsheet are being imported through the IMPORTRANGE() formula. It looks like this:
Spreadsheet 1
╔════════╦════════╦════════╦════════╗
║ title1 ║ title2 ║ title3 ║ title4 ║
╠════════╬════════╬════════╬════════╣
║ input1 ║ input4 ║ input7 ║ ║
║ input2 ║ input5 ║ input8 ║ ║
║ input3 ║ input6 ║ input9 ║ ║
╚════════╩════════╩════════╩════════╝
Spreadsheet 2
╔════════╦════════╦════════╗
║ title1 ║ title2 ║ title3 ║
╠════════╬════════╬════════╣
║ input1 ║ input4 ║ input7 ║
║ input2 ║ input5 ║ input8 ║
║ input3 ║ input6 ║ input9 ║
╚════════╩════════╩════════╝
The thing is, I only want the data to be imported if the corresponding cell in the title4 column is populated. Like this:
If Spreadsheet 1 looks like this
╔════════╦════════╦════════╦═════════╗
║ title1 ║ title2 ║ title3 ║ title4 ║
╠════════╬════════╬════════╬═════════╣
║ input1 ║ input4 ║ input7 ║ ║
║ input2 ║ input5 ║ input8 ║ input11 ║
║ input3 ║ input6 ║ input9 ║ ║
╚════════╩════════╩════════╩═════════╝
then Spreadsheet 2 should look like this
╔════════╦════════╦════════╗
║ title1 ║ title2 ║ title3 ║
╠════════╬════════╬════════╣
║ input2 ║ input5 ║ input8 ║
╚════════╩════════╩════════╝
I figured this out.
I used the argument:
=IF("Sheet Title 1!F1:F1000"<>"", IMPORTRANGE("Spreadsheet Key","Sheet Title 1!E1:E1000"), "")
So the contents of Column E in the first sheet are only copied to the second sheet if the cell in the same row in Column F is not empty.

Selecting a row with specific value from a group?

From the table below, if Project.Date group has a Fail and Success, I'd like to keep the Fail row, but if single row (like the rest,) then keep that row regardless of Status. For example I'd like to keep the first row, discard the second and keep the rest on the table below.
╔═════════╦══════════╦═════════╗
║ PROJECT ║ DATE ║ STATUS ║
╠═════════╬══════════╬═════════╣
║ HLM ║ 20130422 ║ Fail ║
║ HLM ║ 20130422 ║ Success ║
║ HLM ║ 20130423 ║ Fail ║
║ HLM ║ 20130424 ║ Success ║
║ HLM ║ 20130425 ║ Fail ║
║ HLM ║ 20130426 ║ Success ║
╚═════════╩══════════╩═════════╝
WITH records
AS
(
SELECT [Project], [Date], [Status],
ROW_NUMBER() OVER (PARTITION BY [Project], [Date]
ORDER BY [Status]) rn
FROM TableName
)
SELECT [Project], [Date], [Status]
FROM records
WHERE rn = 1
SQLFiddle Demo
OUTPUT
╔═════════╦══════════╦═════════╗
║ PROJECT ║ DATE ║ STATUS ║
╠═════════╬══════════╬═════════╣
║ HLM ║ 20130422 ║ Fail ║
║ HLM ║ 20130423 ║ Fail ║
║ HLM ║ 20130424 ║ Success ║
║ HLM ║ 20130425 ║ Fail ║
║ HLM ║ 20130426 ║ Success ║
╚═════════╩══════════╩═════════╝