I am having a problem selecting a buyers name, products they bought, and price of items. I want the results to just show the buyers name once and list every product/price of said product.
Select distinct Buyer_First_Name, Buyer_Last_Name, Appliance_num, Price
from Buyer inner join Appliance
on buyer.Buyer_num = Appliance.Buyer_num;
This winds up listing the buyers name multiple times, as it is selecting all of the items distinct. The results I am seeing with this code is:
Buyer_First_Name Buyer_First_Name Appliance_num Price
John Smith 000001 $19.99
John Smith 000002 $45.99
John Smith 000003 $12.99
John Smith 000004 $17.99
Mike Brown 000001 $19.99
Mike Brown 000005 $33.99
Mike Brown 000006 $29.99
What I want to see:
Buyer_First_Name Buyer_First_Name Appliance_num Price
John Smith 000001 $19.99
000002 $45.99
000003 $12.99
000004 $17.99
Mike Brown 000001 $19.99
000005 $33.99
000006 $29.99
Thanks.
Hiding the duplicates is something you need to do in the presentation, not in the query. Look for an option in your reporting tool or adjust your code to do this.
Related
I am trying to create an SOQL query that looks to an employees report to position id and create a string that will concat the employees position id and the position id of his manager and concat the managers manager and continue this up the organization chart.
Example Data in Position__c:
Employee_Name Position_ID Reports_to_Position_ID
John Doe 123 456
Billy Bob 456 789
Jane Doe 789 321
Harvey Sample 321 654
John Doe's position id is 123, he reports to position 456. position 456 reports to 789 and so on.
Expected Result for John Doe reports_to_Hierarchy --> 123,456,789,321,654
SOQL looks bit object-oriented, you use dots to go "up" the relationship. https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_relationships.htm
The basic idea is
SELECT Name, Manager.Name, Manager.Manager.Name, Manager.Manager.Manager.Name
FROM User
Actual result depends if you have standard or custom object and what are the field names. You can go up to 5 levels (dots) away from your starting point.
Is the list flat just like that? You might need a helper lookup to "self" field on Position__c, call it "Parent__c", "Manager__c" or something. And you'd need to populate it during load (read about upsert and external ids?)
And then it'd be something like
SELECT PositionId__c,
Parent__r.PositionId__c,
Parent__r.Parent__r.PositionId__c,
Parent__r.Parent__r.Parent__r.PositionId__c
FROM Position__c
I am new to coding. Now I have a employee table looked like below:
Name
Hometown
School
Jeff
Illinois
Loyola University Chicago
Alice
California
New York University
William
Michigan
University of Illinois at Chicago
Fiona
California
Loyola University Chicago
Charles
Michigan
New York University
Linda
Indiana
Loyola University Chicago
I am trying to get those employees in pairs where two employees come from different state and different university. Each person can only be in one pair. The expected table should look like
employee1
employee2
Jeff
Alice
William
Fiona
Charles
Linda
The real table is over 3,000 rows. I am trying to do it with SQL or Python, but I don't know where to start.
A straightforward approach is to pick employees one by one and search the table after the one for an appropriate peer; found peers are flagged in order to not be paired repeatedly. Since in your case a peer should be found after a few steps, this iteration will likely be faster than operations which construct whole data sets at once.
from io import StringIO
import pandas as pd
# read example employee table
df = pd.read_table(StringIO("""Name Hometown School
Jeff Illinois Loyola University Chicago
Alice California New York University
William Michigan University of Illinois at Chicago
Fiona California Loyola University Chicago
Charles Michigan New York University
Linda Indiana Loyola University Chicago
"""))
# create expected table; its length is half that of the above
ef = pd.DataFrame(index=pd.RangeIndex(len(df)/2), columns=['employee1', 'employee2'])
k = 0 # number of found pairs, index into expected table
# array of flags for already paired employees
paired = pd.Series(False, pd.RangeIndex(len(df)))
# go through the employee table and collect pairs
for i in range(len(df)):
if paired[i]: continue
for j in range(i+1, len(df)):
if not paired[j] \
and df.iloc[j]['Hometown'] != df.iloc[i]['Hometown'] \
and df.iloc[j]['School'] != df.iloc[i]['School']:
# we found a pair - store it, mark employee j paired
ef.iloc[k] = df.iloc[[i, j]]['Name']
k += 1
paired[j] = True
break
else:
print("no peer for", df.iloc[i]['Name'])
print(ef)
output:
employee1 employee2
0 Jeff Alice
1 William Fiona
2 Charles Linda
Hi I can't figure this out for the life of me! On the second tab 'Combined' I'm trying to get the array to show a combination of unique dates, names and then a total of the amount to pay that person.
https://docs.google.com/spreadsheets/d/1sSuHK0h2OeaEJpraoHXTi01XIwpreM47BovQAV-snDE/edit?usp=sharing
So ideally it should show on the 'Combined' page:
A
11/15/2020 Bill Jones $553.80
11/15/2020 Steve Robinson $320.00
10/7/2019 Grady Johnson $100.12
11/15/2020 Grady Johnson $45.00
11/22/2020 Jim Luke $300.43
11/17/2020 Jim Luke $1,357.63
I've been trying to figure this out for days - please help!
use:
=QUERY(Investors!A4:C,
"select A,B,sum(C)
where A is not null
group by A,B
label sum(C)''")
Suppose I have the following data table in Excel
Company Amount Text
Oracle $3,400 330 Richard ERP
Walmart $750 348 Mary ERP
Amazon $6,880 xxxx Loretta ERP
Rexel $865 0000 Mike ERP
Toyota $11,048 330 Richard ERP
I want to go through each item in the "Text" column, search the item against the following range of names:
Mary
Mike
Janine
Susan
Richard
Jerry
Loretta
and return the name in the "Person" column, if found. For example:
Company Amount Text Person
Oracle $3,400 330 Richard ERP Richard
Walmart $750 348 Mary ERP Mary
Amazon $6,880 xxxx Loretta ERP Loretta
Rexel $865 0000 Mike ERP Mike
Toyota $11,048 330 Richard ERP Richard
I've tried the following in Excel which works:
=IF(N2="","",
IF(ISNUMBER(SEARCH(Sheet2!$A$1,N2)),Sheet2!$A$1,
IF(ISNUMBER(SEARCH(Sheet2!$A$2,N2)),Sheet2!$A$2,
IF(ISNUMBER(SEARCH(Sheet2!$A$3,N2)),Sheet2!$A$3,
....
Where $A$1:$A$133 is my range and N2 is the "Text" column values; however, that is a lot of nested code and apparently Excel has a limit on the number of nested IF statements you can have.
Is there a simpler solution (arrays? VBA?)
Thanks!
Use the following formula:
=IFERROR(INDEX(Sheet2!A:A,AGGREGATE(15,6,ROW(Sheet2!$A$1:INDEX(Sheet2!A:A,MATCH("zzz",Sheet2!A:A)))/(ISNUMBER(SEARCH(Sheet2!$A$1:INDEX(Sheet2!A:A,MATCH("zzz",Sheet2!A:A)),N2))),1)),"")
From the 50 states of US, most of them have counties except Louisiana and Alaska.
My tables would look like this
**State_tbl**
State_id
State_name
**County_tbl**
County_id
State_id ->state_tbl
County_name
**City_tbl**
City_id
County_id ->county_tbl
City_name
However, since the two states Alaska and Louisiana don't have counties, I would have problems implementing them. And I also read that there may be cities within a state that don't have a county, or that belong to two counties (don't know if that is true).
What would be the best approach to design the database?
UPDATE More info:
I have an user which would register to serve into specific cities (within a state). When I retrieve data I want to be able to display both the cities that are served, as well as the counties. There would be a Many-to-Many relationship between the user and the cities served, and a one-many relationship between cities and counties.
i.e:
John K - serving in state_A (all counties and cities below belong to state_A)
-cities served: City_A (county_x), City_B (County_Y), City_C (County_Y)
-counties served: County_X, County_Y
Also, would I be able to retrieve a user's info and all the cities and counties served within one query?
Just treat the boroughs and parishes and counties (or any other naming conventions) as the same thing. The USPS treats them the same and the Census Bureau also treats them the same. Most of the government (and nongovernment organizations) in the US that need to generate any kind of report on counties or statistical areas rely on the MSA or CBSA codes which are based on these units, all of which are referred to as counties - even though they may have other local names.
For Alaska, here are all the boroughs:
ANCHORAGE
BETHEL
ALEUTIANS WEST
LAKE AND PENINSULA
KODIAK ISLAND
ALEUTIANS EAST
WADE HAMPTON
DILLINGHAM
KENAI PENINSULA
YUKON KOYUKUK
VALDEZ CORDOVA
BRISTOL BAY
MATANUSKA SUSITNA
NOME
YAKUTAT
FAIRBANKS NORTH STAR
DENALI
NORTH SLOPE
NORTHWEST ARCTIC
SOUTHEAST FAIRBANKS
JUNEAU
HOONAH ANGOON
HAINES
PETERSBURG
SITKA
SKAGWAY
KETCHIKAN GATEWAY
PRINCE OF WALES HYDER
WRANGELL
For Lousiana, here are all the parishes:
JEFFERSON
SAINT CHARLES
SAINT BERNARD
PLAQUEMINES
ST JOHN THE BAPTIST
SAINT JAMES
ORLEANS
LAFOURCHE
ASSUMPTION
SAINT MARY
TERREBONNE
ASCENSION
TANGIPAHOA
SAINT TAMMANY
WASHINGTON
SAINT HELENA
LIVINGSTON
LAFAYETTE
VERMILION
SAINT LANDRY
IBERIA
EVANGELINE
ACADIA
SAINT MARTIN
JEFFERSON DAVIS
CALCASIEU
CAMERON
BEAUREGARD
ALLEN
VERNON
EAST BATON ROUGE
WEST BATON ROUGE
WEST FELICIANA
POINTE COUPEE
IBERVILLE
EAST FELICIANA
BIENVILLE
NATCHITOCHES
CLAIBORNE
CADDO
BOSSIER
WEBSTER
RED RIVER
DE SOTO
SABINE
OUACHITA
RICHLAND
FRANKLIN
MOREHOUSE
UNION
JACKSON
LINCOLN
MADISON
WEST CARROLL
EAST CARROLL
RAPIDES
CONCORDIA
AVOYELLES
CATAHOULA
LA SALLE
TENSAS
WINN
GRANT
CALDWELL