mysql connect all fields in two columns - if-statement

I have a view with two columns: a person's ID (a number) and the sector that they below to (given as numbers 1-5).
I want to create a view to show whether people belong to the same sector. I think this would have three columns: ID1, ID2, and SameSector. The first column would list IDs, and for each ID in column 1 the second column would list ALL of the IDs. The third column would be an if statement, 1 if the sector was the same for both IDs, 0 if it wasn't. This is made slightly more complicated because a person can belong to more than one sector.
For example:
I have:
ID Sector
1 1
2 1
2 5
3 1
I want:
ID1 ID2 SameSector
1 1 1
1 2 1
1 2 0
1 3 0
2 1 1
2 1 0
etc.
I'm guessing this involves some sort of self join and if statement but I can't figure out how to get all of the ID fields to be listed in ID1 column and matched to all of the ID fields in ID2 any ideas?

This should be what you want:
SELECT a.ID AS ID1, b.ID AS ID2, IF(a.Sector=b.Sector,1,0) AS SameSector
FROM theTable AS a, theTable AS b
http://sqlfiddle.com/#!2/f2cbc/4
I initially had a much more complicated query, but then realized you wanted a complete cross-join, including the same ID comparing to itself.

Related

Power BI pivot column with no aggregation errors

I have a simple table that I want to pivot by the 'COLUMN_NAME' column:
When I pivot and aggregate by count it works fine:
When I try to pivot without aggregation, it gives this error:
Expression.Error: There were too many elements in the enumeration to
complete the operation. Details:
[List]
Here is what I expected to happen:
thx in adavnce
You need to pivot against a specific column, otherwise the powerbi engine can't determine how to keep the data in rows consistently.
Your input needs to be in a format similar to this:
RecordID
COLUMN_NAME
COLUMN_VALUE
1
PRODUCT_SUB_FAMILY
MYPRODUCT
1
MFG_STEP_NAME
FT1
1
QTY_IN
678
1
QTY_OUT
480
1
AGG_YIELD
0.70796
2
PRODUCT_SUB_FAMILY
MYPRODUCT
2
MFG_STEP_NAME
SLT1
2
QTY_IN
66
2
QTY_OUT
0
2
AGG_YIELD
0
And then when you pivot, you select the RecordID as the column you pivot against.

Merging Tables Correctly in SAS

Hi I am trying to merge two tables the FormA scores table that I made that is now CalculatingScores with the domain number found in DomainsFormA. I need to merge them by QuestionNum. Here is my code.
proc sql;
create table combined as
select *
from CalculatingScores inner join DomainsFormA
on CalculatingScores.Scores=DomainsFormA.QuestionNum;
quit;
proc print data=combined (obs=15);
run;
This table is what I am trying to get my merged tables to look like but for 15 observations.
Form
Student
QuestionNum
Scores
DomainNum
A
1
1
0
5
A
1
2
1
4
A
1
3
0
5
But My tables look more like this
Form
Student
QuestionNum
Scores
DomainNum
A
1
2
1
5
A
1
4
1
5
A
1
5
1
5
My entire Scores column for these 15 observations have a value of 1. Also my DomainNum column only has values of 5. My Student and Form columns are correct but I need to have varied scores and varied domain numbers. Any ideas for how to solve my problem? Maybe I need a order by statement?
You appear to be joining on the incorrect columns
You coded
on CalculatingScores.Scores=DomainsFormA.QuestionNum
which is joining a score to a question number
perhaps you should be coding
on CalculatingScores.QuestionNum=DomainsFormA.QuestionNum
^^^^^^^^^^^ ^^^^^^^^^^^

Concatenating row values in Athena Aws

I've 2 cols lets say id and values. I want to concatenate values grouped by id col.
for eg.
I've
ID Values
1 a
1 b
2 a
2 b
I need the output as
ID Values
1 a,b
2 a,b
You can use an array_agg followed by an array_join
select id, array_join(array_agg(values),',') from table group by 1
The array_agg will give you an array of all values with the same id, and the array_join will concatenate them into a string. See the docs.

data model in Power BI with multiple tables each has an FK

Assume I have the following tables:
Table 1:
Prj_id, name
1 , prj1
2 , prj2
3 , prj3
Table 2
prj_id, cost, year
1 ,100 ,1999
1 ,200 ,2000
1 ,300 ,3000
2 ,150 ,1998
Table 3
Prj_id, manager
1 , xxx
1 , yyy
2 , xxx
3 , zzz
Now my question is:
Shall I connect all tables together in the model (relation tab) of BI, if I have all the attributes as filters on the page
i.e.
Table 1 to 2 ->Both direction
Table 1 to 3 ->Both direction
Table 2 to 3 ->Both direction
or shall I need to only join tables as follows:
Table 1 to 2->Both direction
Table 1 to 3->Both direction
That highly depends on what your final goal is.
However I would create a relationship between this tables. As I looks, these data coming from a database with a good structure, so why joining every together again.

pandas keep rows based on column values for repeated values

I have a pandas data frame and I have a list of values. I want to keep all the rows from my original DF that have a certain column value belonging to my list of values. However my list that I want to choose my rows from have repeated values. Each time I encounter the same values again I want to add the rows with that column values again to my new data frame.
lets say my frames name is: with_prot_choice_df and my list is: with_prot_choices
if I issue the following command:
with_prot_choice_df = with_df[with_df[0].isin(with_prot_choices)]
then this will only keep the rows once (as if for only unique values in the list).
I don't want to do this with for loops since I will repeat the process many times and it will be extremely time consuming.
Any advice will be appreciated. Thanks.
I'm adding an example here:
let's say my data frame is:
col1 col2
a 1
a 6
b 2
c 3
d 4
and my list is:
lst = [a,b,a,a]
I want my new data frame, new_df to be:
new_df
col1 col2
a 1
a 6
b 2
a 1
a 6
a 1
a 6
Seems like you need reindex
df.set_index('col1').reindex(lst).reset_index()
Out[224]:
col1 col2
0 a 1
1 b 2
2 a 1
3 a 1
Updated
df.merge(pd.DataFrame({'col1':lst}).reset_index()).sort_values('index').drop('index',1)
Out[236]:
col1 col2
0 a 1
3 a 6
6 b 2
1 a 1
4 a 6
2 a 1
5 a 6