Copying Information based on conditions in an Excel Macro - vba

I have a list of open invoices listed in Excel per office. How can I use a macro to split these open invoices per office into different Excel sheets, and then save each sheet as the office number? Below is a sample list where the first column is the office number, the entire line associated would have to be copied over to the new sheet.
1 180 JOHN 30073 COMPANY X 15,101 173,758 255,713 8/1/2011 8,101 8/24/11 Adair has approved the rates!
1 278 ADAM 159334 COMPANY A 28,606 116,174 158,925 5/9/2011 167,631 7/18/11 Julie Levinsohn still needs to look at reduced entries to see if we can resubmit.
2 600 ROSE 113724 COMPANY 123 0 5,918 20,446 8/22/2011 6,713 8/26/11 Em M Belcher Jul invs per her request.
2 289 SUE 149232 COMPANY BC 389 5,575 12,098 4/22/2011 328 8/23/11 Em w/jun inv to R. Kos
2 169 MIKE 120126 COMPANY 98 41,907 5,218 202,756 8/18/2011 33,635 8/24/11 Jun invs to be pd mid sept per Patrice.
2 849 BOB 63068 COMPANY CB 2,862 4,889 9,271 4/25/2011 4,279 8/23/11 Called Choi re when and how much she will send.
2 849 LANEY 170318 COMPANY 34 0 4,123 6,283 6/30/2011 270 8/17/11 Robert em me re working w/cj re problem w/retainer hrs and bills.
2 707 BOB 153213 COMPANY CI 0 3,127 3,127 5/6/2011 257 4/27/11 Appeals for some of the shortpays by insurance co are pending per
2 141 SUE 65267 COMPANY Z 9,652 2,313 12,546 7/20/2011 8,380 8/16/11 Stmt em to Pat for pmt of os invs.
2 705 MIKE 173993 COMPANY X 5,020 2,240 7,294 7/19/2011 1,120 8/24/11 Pmt is processing.
2 763 JOHN 85919 COMPANY LK 3,500 0 4,500 8/22/2011 1,014 8/19/11 Inv 5637061 in a/p for pmt per cl.
2 400 MIKE 41218 COMPANY 90 3,433 0 3,433 8/24/2011 2,270 5/27/09 Per Hall, ck has been signed and mailed for Feb inv.
3 164 MIKE 133625 COMPANY LO 500 19,351 21,795 7/25/2011 636 8/16/11 Em to B. Kampas re my calling for pmt
3 178 BOB 168512 COMPANY GH 889 15,749 17,030 6/9/2011 2,322 8/24/11 L/m for M Cornejo to call re osbal due.
3 1005 SUE 164680 COMPANY TH 0 13,862 14,459 8/24/2010 5,000 07/06/11 snt ar statement.dtelles
3 164 LANEY 61383 COMPANY RT 0 11,077 65,316 7/29/2011 31,750 8/30/11 Inv 5567542 being revised per
3 171 SUE 78029 COMPANY 345 0 10,507 20,385 8/15/2011 10,165 8/26/11 May invs em to susan
3 164 JOHN 62161 COMPANY 383 14,000 10,500 73,376 8/22/2011 3,500 8/26/11 Invs 5655722 and 5629996 are flat fee bills
3 1139 MIKE 169932 COMPANY 282 145 10,401 10,546 8/24/2011 800 8/26/11 $800 recd.
3 171 CHRIS 134278 COMPANY 202 0 9,603 9,603 8/15/2011 38,300 8/11/11 JP em from Myriam that ck cut today
3 1363 CHRIS 166031 COMPANY CW 0 8,987 8,987 9/17/2010 4,104 8/3/11 em to Brad K about speaking to Charles
3 171 JOHN 139383 COMPANY WE 3,872 8,712 23,575 8/19/2011 5,608 07/06/11 snt ar stmnt.
3 198 MIKE 118294 COMPANY LC 0 3,262 3,262 3/9/2011 1,000 8/15/11 Em Cl for a $500 pmt.
3 1139 BOB 176647 COMPANY XC 0 2,673 11,648 7/26/2011 12,152 8/24/11 Em w/may inv sent to for pmt.
3 1223 BOB 163879 COMPANY NC 4,550 185 4,735 8/15/2011 32,815 8/4/11 Mar, Apr May invs revised and sent to Cl
3 1139 BOB 169094 COMPANY 321 5,000 173 12,728 7/20/2011 4,730 8/30/11 CP em Gayle W9 and inv 5625894
3 178 SUE 5416 COMPANY DW 2,670 0 9,596 8/8/2011 2,496 8/24/11 Sent stmt to V. Wu.
3 762 CHRIS 112507 COMPANY IC 6,000 0 12,293 8/8/2011 4,013 07/06/11 snt ar stmnt.
3 631 JOHH 108718 COMPANY IF 15,842 0 43,215 8/26/2011 5,515 07/06/11 snt ar stmnt
4 133 LANEY 157042 COMPANY IR 0 92,879 114,157 5/6/2011 116,483 08/18/2011,jw, emailed Carolyn
4 502 LANEY 66422 COMPANY IG 30,291 58,792 160,301 8/23/2011 24,512 9/17/10 JP sent f/u em to Robin re past due inv
4 155 CHRIS 72564 COMPANY 853 3,283 55,918 62,367 8/4/2011 500 10/19/05 lg recd call from karen,invs will be pd as of next week
4 500 BOB 128230 COMPANY URE 850 49,217 51,006 12/3/2010 2,353 06/29/2011,jw, asked Dk for collection ypdate
4 751 MIKE 174393 COMPANY KRIG 0 42,753 60,057 7/5/2011 3,658
4 1384 JOHN 143392 COMPANY IR 0 42,468 42,468 10/27/2010 -2,500 7/20/11 Account turned over to collections.
4 1135 MIKE 169399 COMPANY IGD 1,517 38,857 44,108 7/3/2011 1,539 07/07/2011,jw, emailed Jake for collecti on update
4 1135 CHRIS 151511 COMPANY IGDS 608 37,458 42,010 7/29/2011 5,691 07/07/2011,jw, emailed Jake for collection update
5 101 BOB 140464 COMPANY IDGS 0 9,226 16,185 7/20/2011 1,120 10/13/10 JP call from Sun req copy of os inv be em
5 281 JOHN 155780 COMPANY IERE 0 9,214 14,557 8/22/2011 13,097 5/3/11Inv 5440713 to clnt mi
5 288 JOHN 86325 COMPANY 832 1,140 9,178 11,458 8/22/2011 2,824 12/10/09 am received em advising they r moving

Try this; it should work, assuming the office number is column A and there are row headings:
Dim Source As Range
Dim OfficeNumber As String
Dim PrevOfficeNumber As String
Dim CurrentSheet As Worksheet
Dim NewSheet As Worksheet
Set Source = Cells(1, 1).CurrentRegion
Set CurrentSheet = ActiveSheet
For Row = 1 To Source.Rows.Count
OfficeNumber = CurrentSheet.Cells(Row, 1)
If OfficeNumber <> PrevOfficeNumber Then
' create a new sheet
newrow = 1
Set NewSheet = Application.Sheets.Add
NewSheet.Name = OfficeNumber
PrevOfficeNumber = OfficeNumber
End If
' copy row to new sheet
CurrentSheet.Cells(Row, 1).EntireRow.Copy (NewSheet.Cells(newrow, 1))
newrow = newrow + 1
Next

Related

SPARK SQL query for match output

I have 2 ds as below
ds1:
CustId Name Street1 City
=================================
1 Ron 1 Mn strt Hyd
2 Ashok westend av Delhi
3 Rajesh 5th Cross Mumbai
4 Venki 2nd Main NY
ds2:
Id CustName CustAddr1 City
=========================================
11 Ron 1 Mn Street Hyd
12 Ron eastend avn Patna
13 Rajesh 2nd Main Mumbai
14 Girish 100ft rd BLR
15 Dinesh 60ft Mum
16 Rajesh 1st Cross Mumbai
I am trying to find an exact match like ds1.Name --> ds2.CustName, ds1.city --> ds2.city
Output:
GrpID Rec_Id Count ds1.cond Rec_Id Count ds2.cond
======================================================================
1 1 1 Ron + Hyd 1001 1 Ron + Hyd
2 2 1 Rajesh + Mumbai 1002 2 Rajesh + Mumbai
How to write (SPARK) SQL query for it?
I tried
final Dataset<Row> rslt = spark.sql("select * from ds1 JOIN ds2 ON ds1.Name==ds2.CustName");
(using only name)
but it gives output of mXn for m matching rows in ds1 with n matching rows in ds2.
My first work on this. Any suggestion?

SQL join to identify group members

I have got a client table which pretty much looks like below:
Client List
customer no. Customer name
123 Kristen Smith
128 Jeremy Church
127 Alan Li
132 Ryan Nelson
I need to map it to a Customer_Dim table
Customer_Dim
customer no. Customer name Group no. Group Name Cust_Active Flag
123 Kristen Smith 5491 Zealong Tea Estate Y
167 Anna Hathaway 5823 AA Insurance Y
146 Simon Joe 5671 Direct Automobile Y
148 Henry Wilson 5823 AA Insurance Y
195 Graham Brown 5491 Zealong Tea Estate Y
172 Daria Smith 5671 Direct Automobile N
122 Dyana Smith 5823 AA Insurance N
132 Ryan Nelson 5671 Direct Automobile N
128 Jeremy Church 5823 AA Insurance Y
127 Alan Li 5671 Direct Automobile Y
to get their group numbers from below table (which I am able to do by a simple left join)
to list all the remaining customers (who are active) from the group numbers of the client customer [I AM UNABLE TO DO THIS 2nd PART] :
Required Results :
Customer No. Customer name Group No. Group Name
123 Kristen Smith 5491 Zealong Tea Estate
128 Jeremy Church 5823 AA Insurance
127 Alan Li 5671 Direct Automobile
195 Graham Brown 5491 Zealong Tea Estate
167 Anna Hathaway 5823 AA Insurance
148 Henry Wilson 5823 AA Insurance
146 Simon Joe 5671 Direct Automobile
Please let me know if any other information is needed.
Sorry, if a similar question has been asked earlier - did several searches but was unable to find anything.
Thanks
join the tables to get all the group numbers of the clients in the client list and then select from customer_dim only the clients of these group numbers who are active:
select * from customer_dim
where
cust_active_flag = 'Y'
and
groupno in (
select groupno
from client_list l inner join customer_dim d
on d.customerno = l.customerno
)
See the demo.
Results:
> customerno | customername | groupno | groupname | cust_active_flag
> ---------: | :------------ | ------: | :----------------- | :---------------
> 123 | Kristen Smith | 5491 | Zealong Tea Estate | Y
> 167 | Anna Hathaway | 5823 | AA Insurance | Y
> 146 | Simon Joe | 5671 | Direct Automobile | Y
> 148 | Henry Wilson | 5823 | AA Insurance | Y
> 195 | Graham Brown | 5491 | Zealong Tea Estate | Y
> 128 | Jeremy Church | 5823 | AA Insurance | Y
> 127 | Alan Li | 5671 | Direct Automobile | Y
for get required results you need a condition in your join
SELECT *
FROM Client c
JOIN Customer_Dim cd on c.CustomerNo = cd.CustomerNo
and cd.Cust_ActiveFlag ='Y'
or
SELECT *
FROM Client c
JOIN Customer_Dim cd on c.CustomerNo = cd.CustomerNo
WHERE cd.Cust_ActiveFlag ='Y'
I think it is pretty simple to get your posted result from Customer_Dim table.
if you don't want Group No. of ClientList
select * from Customer_Dim
where [Cust_Active Flag] = 'Y'
and [Group No.] not in (
select CD.[Group No.] from [Client List] as CL inner join Customer_Dim as CD where CL.[customer no.] = CD.[customer no.] )
And
if you only want Group No. of ClientList
select * from Customer_Dim
where [Cust_Active Flag] = 'Y'
and [Group No.] in (
select CD.[Group No.] from [Client List] as CL inner join Customer_Dim as CD where CL.[customer no.] = CD.[customer no.] )
For the case of a client being inactive and wanting to identify the client in the result set you can do a LEFT JOIN with a GROUP BY and leverage Proc SQL automatic remerging in a HAVING clause for selection criteria.
data client_list; input
custno custname:& $30.; datalines;
123 Kristen Smith
128 Jeremy Church
127 Alan Li
132 Ryan Nelson
899 Julius Caesar
run;
data customer_dim; input
custno custname:& $30. groupnum groupname:& $30. Cust_Active_Flag: $1.; datalines;
123 Kristen Smith 5491 Zealong Tea Estate Y
167 Anna Hathaway 5823 AA Insurance Y
146 Simon Joe 5671 Direct Automobile Y
148 Henry Wilson 5823 AA Insurance Y
195 Graham Brown 5491 Zealong Tea Estate Y
172 Daria Smith 5671 Direct Automobile N
122 Dyana Smith 5823 AA Insurance N
132 Ryan Nelson 5671 Direct Automobile N
128 Jeremy Church 5823 AA Insurance Y
127 Alan Li 5671 Direct Automobile Y
231 Donald Duck 7434 Orange Insurance Y
899 Julius Caesar 4999 Emperors N
900 Joshua Norton 4999 Emperors N
925 Joaquin Guzman 4999 Emperors Y
925 Naruhito 4999 Emperors Y
run;
proc sql;
create table want(label="Active customers of clients groups") as
select
LIST.custno as client,
DIM.*
from
customer_dim DIM
left join
client_list LIST
on
DIM.custno = LIST.custno
group by
groupnum
having
N(LIST.custno) > 0
and
(
cust_active_flag = 'Y'
or LIST.custno is not NULL
)
order by
groupnum, custno
;

If last names are similar in [Name] column, fill in missing values of another column

Below is a sample of a much larger dataframe.
Fare Cabin Pclass Ticket Name
257 86.5000 B77 1 110152 Cherry, Miss. Gladys
759 86.5000 B77 1 110152 Rothes, the Countess. of (Lucy Noel Martha Dye...
504 86.5000 B79 1 110152 Maioni, Miss. Roberta
262 79.6500 E67 1 110413 Taussig, Mr. Emil
558 79.6500 E67 1 110413 Taussig, Mrs. Emil (Tillie Mandelbaum)
585 79.6500 NaN 1 110413 Taussig, Miss. Ruth
475 52.0000 A14 1 110465 Clifford, Mr. George Quincy
110 52.0000 C110 1 110465 Porter, Mr. Walter Chamberlain
335 26.0000 C106 1 110469 Maguire, Mr. John Edward
158 26.5500 D22 1 110489 Borebank, Mr. John James
430 26.5500 C52 1 110564 Bjornstrom-Steffansson, Mr. Mauritz Hakan
236 75.2500 D37 1 110813 Warren, Mr. Frank Manley
366 75.2500 D37 1 110813 Warren, Mrs. Frank Manley (Anna Sophia Atkinson)
191 26.0000 NaN 1 111163 Salomon, Mr. Abraham L
170 33.5000 B19 1 111240 Van der hoef, Mr. Wyckoff
462 38.5000 E63 1 111320 Gee, Mr. Arthur H
329 57.9792 Nan 1 111361 Hippach, Miss. Jean Gertrude
523 57.9792 B18 1 111361 Hippach, Mrs. Louis Albert (Ida Sophia Fischer)
If I want to iterate the filling of missing values of "Cabin" for people who are missing "Cabin" values, with someone else's "Cabin" values, only if
the someone else (the one who has a cabin value) has the same last name and also are in the vicinity of oneself( as in one above or one below them) .
So in the dataframe above, [Tassuig, Miss.Ruth]'s Cabin value of "Nan" would be replaced with that of [Tassuig, Mrs.Emil]'s cabin value [E67] who is one above herself because both conditions are met. (Same last name and in the vicinity)
And [Hippach, Miss. Jean Gertrude]'s missing cabin value would be replaced with
[ Hippach, Mrs. Louis Albert (Ida Sophia Fischer)]'s Cabin value of [B18].
I tried to think of iteration but this is as far as I got
for x in df.Name.str.split(',')[x][0] ==df.Name.str.split(',')[x+1][0]:
if df.Cabin[x] or df.Cabin[x+1] == np.nan:
df.Cabin.replace(np.nan,
I want to make sure the np.nan value is replaced with a True value and not np.nan. Couldn't figure out how to do that.
Thanks.
Starting with your DataFrame
print(df)
Fare Cabin Pclass Ticket \
0 86.5000 B77 1 110152
1 86.5000 B77 1 110152
2 86.5000 B79 1 110152
3 79.6500 E67 1 110413
4 79.6500 E67 1 110413
5 79.6500 NaN 1 110413
6 52.0000 A14 1 110465
7 52.0000 C110 1 110465
8 26.0000 C106 1 110469
9 26.5500 D22 1 110489
10 26.5500 C52 1 110564
11 75.2500 D37 1 110813
12 75.2500 D37 1 110813
13 26.0000 NaN 1 111163
14 33.5000 B19 1 111240
15 38.5000 E63 1 111320
16 57.9792 NaN 1 111361
17 57.9792 B18 1 111361
Name
0 Cherry, Miss. Gladys
1 Rothes, the Countess. of (Lucy Noel Martha Dye...
2 Maioni, Miss. Roberta
3 Taussig, Mr. Emil
4 Taussig, Mrs. Emil (Tillie Mandelbaum)
5 Taussig, Miss. Ruth
6 Clifford, Mr. George Quincy
7 Porter, Mr. Walter Chamberlain
8 Maguire, Mr. John Edward
9 Borebank, Mr. John James
10 Bjornstrom-Steffansson, Mr. Mauritz Hakan
11 Warren, Mr. Frank Manley
12 Warren, Mrs. Frank Manley (Anna Sophia Atkinson)
13 Salomon, Mr. Abraham L
14 Van der hoef, Mr. Wyckoff
15 Gee, Mr. Arthur H
16 Hippach, Miss. Jean Gertrude
17 Hippach, Mrs. Louis Albert (Ida Sophia Fischer)
Creating a new column/series with just the LastName. Note, might be a better way to do this with pandas str methods, but I couldn't get anything to work
df['LastName'] = df['Name'].map(lambda x : x[:x.find(',')])
Then we leverage Pandas' shift and boolean indexing to see if the passenger above has the same last name (ie the Taussig case)
filter = (df['Cabin'].isnull()) & (df['LastName'] == df['LastName'].shift())
df.loc[filter,'Cabin'] = df['Cabin'].shift()
and then the passenger below by passing a -1 to shift() (ie the Hippach case)
filter = (df['Cabin'].isnull()) & (df['LastName'] == df['LastName'].shift(-1))
df.loc[filter,'Cabin'] = df['Cabin'].shift(-1)
print(df)
Fare Cabin Pclass Ticket \
0 86.5000 B77 1 110152
1 86.5000 B77 1 110152
2 86.5000 B79 1 110152
3 79.6500 E67 1 110413
4 79.6500 E67 1 110413
5 79.6500 E67 1 110413
6 52.0000 A14 1 110465
7 52.0000 C110 1 110465
8 26.0000 C106 1 110469
9 26.5500 D22 1 110489
10 26.5500 C52 1 110564
11 75.2500 D37 1 110813
12 75.2500 D37 1 110813
13 26.0000 NaN 1 111163
14 33.5000 B19 1 111240
15 38.5000 E63 1 111320
16 57.9792 B18 1 111361
17 57.9792 B18 1 111361
Name LastName
0 Cherry, Miss. Gladys Cherry
1 Rothes, the Countess. of (Lucy Noel Martha Dye... Rothes
2 Maioni, Miss. Roberta Maioni
3 Taussig, Mr. Emil Taussig
4 Taussig, Mrs. Emil (Tillie Mandelbaum) Taussig
5 Taussig, Miss. Ruth Taussig
6 Clifford, Mr. George Quincy Clifford
7 Porter, Mr. Walter Chamberlain Porter
8 Maguire, Mr. John Edward Maguire
9 Borebank, Mr. John James Borebank
10 Bjornstrom-Steffansson, Mr. Mauritz Hakan Bjornstrom-Steffansson
11 Warren, Mr. Frank Manley Warren
12 Warren, Mrs. Frank Manley (Anna Sophia Atkinson) Warren
13 Salomon, Mr. Abraham L Salomon
14 Van der hoef, Mr. Wyckoff Van der hoef
15 Gee, Mr. Arthur H Gee
16 Hippach, Miss. Jean Gertrude Hippach
17 Hippach, Mrs. Louis Albert (Ida Sophia Fischer) Hippach
groupby + fillna
# back fills, then forward fills
def bffill(x):
return x.bfill().ffill()
# group by last name
df['Cabin'] = df.groupby(df.Name.str.split(',').str[0]).Cabin.apply(bffill)
df

3 Tables, JOIN query and alphabetic order

I am currently working with three tables where I am trying to figure out how to use a join to display once the title_id of any book with Dennis McCann as an editor. The tables have in common title_id and editor_id. Cant find a way to piece it all together. How to display once the title_id of any book with Dennis McCann as an editor?
SELECT * FROM title_editors;
EDITOR_ID TITLE_ EDITOR_ORDER
----------- ------ ------------
826-11-9034 Bu2075 2
826-11-9034 PS2091 2
826-11-9034 Ps2106 2
826-11-9034 PS3333 2
826-11-9034 PS7777 2
826-11-9034 pS1372 2
885-23-9140 MC2222 2
885-23-9140 MC3021 2
885-23-9140 Tc3281 2
885-23-9140 TC4203 2
885-23-9140 TC7777 2
321-55-8906 bU1032 2
321-55-8906 BU1111 2
321-55-8906 BU7832 2
321-55-8906 PC1035 2
321-55-8906 PC8888 2
321-55-8906 BU2075 3
777-02-9831 pc1035 3
777-02-9831 PC8888 3
943-88-7920 BU1032 1
943-88-7920 bu1111 1
943-88-7920 BU2075 1
943-88-7920 BU7832 1
943-88-7920 PC1035 1
943-88-7920 pc8888 1
993-86-0420 PS1372 1
993-86-0420 PS2091 1
993-86-0420 PS2106 1
993-86-0420 PS3333 1
993-86-0420 pS7777 1
993-86-0420 MC2222 1
993-86-0420 MC3021 1
993-86-0420 Tc3218 1
993-86-0420 TC4203 1
993-86-0420 TC7777 1
35 rows selected.
SQL> SELECT * FROM title_authors;
AUTHOR_ID TITLE_ AUTHOR_ORDER ROYALTY_SHARE
----------- ------ ------------ -------------
409-56-7008 Bu1032 1 .6
486-29-1786 PS7777 1 1
486-29-1786 pC9999 1 1
712-45-1867 MC2222 1 1
172-32-1176 Ps3333 1 1
213-46-8915 BU1032 2 .4
238-95-7766 PC1035 1 1
213-46-8915 Bu2075 1 1
998-72-3567 pS2091 1 .5
899-46-2035 PS2091 2 .5
998-72-3567 PS2106 1 1
722-51-5454 mc3021 1 .75
899-46-2035 MC3021 2 .25
807-91-6654 tC3218 1 1
274-80-9391 BU7832 1 1
427-17-2319 pC8888 1 .5
846-92-7186 PC8888 2 .5
756-30-7391 PS1372 1 .75
724-80-9391 PS1372 2 .25
724-80-9391 bu1111 1 .6
267-41-2394 bU1111 2 .4
672-71-3249 TC7777 1 .4
267-41-2394 TC7777 2 .3
472-27-2349 Tc7777 3 .3
648-92-1872 TC4203 1 1
25 rows selected.
SQL> SELECT * FROM editors;
EDITOR_ID EDITOR_LNAME EDITOR_FNAME EDITOR_POSITION PHONE ADDRESS CITY ST ZIP
----------- ----------------- ------------- --------------- ------------ -------------------- ------------ -- ------
321-55-8906 DeLongue Martinella Project 415 843-2222 3000 6th St. BERKELEY Ca 94710
723-48-9010 Sparks MANfred cOPY 303 721-3388 15 Sail DENVER Co 80237
777-02-9831 Samuelson Bernard proJect 415 843-6990 27 Yosemite OAKLAND Ca 94609
777-66-9902 Almond Alfred copy 312 699-4177 1010 E. DeVON CHICAGO Il 60018
826-11-9034 Himmel Eleanore pRoject 617 423-0552 97 Bleaker BOSTON Ma 02210
885-23-9140 Rutherford-Hayes Hannah PROJECT 301 468-3909 32 Rockbill Pike ROCKBILL MD 20852
993-86-0420 McCann Dennis acQuisition 301 468-3909 32 Rockbill Pike ROCKBill MD 20852
943-88-7920 Kaspchek Christof acquisitiOn 415 549-3909 18 Severe Rd. BERKELEY CA 94710
234-88-9720 Hunter Amanda acquisition 617 432-5586 18 Dowdy Ln. BOSTON MA 02210
You can try join on the table Editors and Ttile_Editors using the Editor_ID that will give you the matching records and you can filter out Only for the ' Dennis McCann ' using either multiple conditions in join or the where clause as,
WITHOUT WHERE
SELECT DISTINCT te.title_id,ed.EDITOR_ID,ed.EDITOR_LNAME,ed.EDITOR_FNAME
FROM
title_editors te JOIN editors ed
ON te.EDITOR_ID = ed.EDITOR_ID
AND ed.EDITOR_LNAME = 'McCann'
AND ed.EDITOR_FNAME = 'Dennis'
ORDER BY te.title_id
USing WHERE
SELECT DISTINCT te.title_id,ed.EDITOR_ID,ed.EDITOR_LNAME,ed.EDITOR_FNAME
FROM
title_editors te JOIN editors ed
ON te.EDITOR_ID = ed.EDITOR_ID
WHERE
ed.EDITOR_LNAME = 'McCann'
AND ed.EDITOR_FNAME = 'Dennis'
ORDER BY te.title_id
It would be easier with the in operator:
SELECT DISTINCT title_id
FROM title_editors
WHERE editor_id IN (SELECT editor_id
FROM editors
WHERE editor_fname = 'Dennis' AND
editor_lname = 'McCann')
ORDER BY title_id ASC

Copy rows of data in SQL Server

Please help me come up with a solution for the situation being explained below:
ID name address age hobby GPA
---------------------------------------------------------
101 James 100 Garfield St 21 reading 3.13
101 James 100 Garfield St 21 writing 2.63
101 James 100 Garfield St 21 running 3.81
109 Tom 19 Lily Ave 19 dating 3.54
109 Tom 20 Lily Ave 19 climbing 2.76
109 Tom 21 Lily Ave 19 watching 3.91
I want to copy the set of rows with the same ID (eg. 101) and assign each set a State abbreviation(s) by running a single sql query. For instance: adding states CA, NJ, and DE to rows with an ID of 101, the result set is expected to look like this:
ID name address age hobby GPA state
-----------------------------------------------------------------------
101 James 100 Garfield St 21 reading 3.13 CA
101 James 100 Garfield St 21 writing 2.63 CA
101 James 100 Garfield St 21 running 3.81 CA
101 James 100 Garfield St 21 reading 3.13 NJ
101 James 100 Garfield St 21 writing 2.63 NJ
101 James 100 Garfield St 21 running 3.81 NJ
101 James 100 Garfield St 21 reading 3.13 DE
101 James 100 Garfield St 21 writing 2.63 DE
101 James 100 Garfield St 21 running 3.81 DE
Please keep in mind that everything else remains the same way as they were before the addition of the state abbreviations. Also assume I have more than three states to add and integrate to the query, say, I have all 50 states. Thank you for your time and effort in advance!
This should produce that result set:
select x.*, y.st
from tbl x
join
(select 'CA' as st union all
select 'NJ' union all
select 'DE') y
where x.id = 101
Create a new table with IDs and States
ID ST
101 CA
101 NJ
101 DE
109 ..
then join that on your table
SELECT t.*, s.st
FROM tbl t
JOIN states s ON t.id = s.id