Laravel Query Builder Join 3 table and count result - laravel

I'm hanging in this, and , hope someone can bring me out this task. i have 4 table called "LOCATIONS","COUNTRIES","REGIONS"AND "DISTRICT".
LOCATIONS
id
name
1
country
2
region
3
district
id
name
COUNTRIES
id
location_id
country
1
1
India
2
1
Italy
3
1
Philippines
4
1
Japan
5
1
Taiwan
6
1
Canada
7
1
USA
8
1
Russia
REGIONS
id
location_id
country_id
region
1
2
2
Region1
2
2
2
Region3
3
2
3
Region8
DISTRICT
id
location_id
region_id
district
1
3
1
district 1
2
3
1
district 3
3
3
1
district 8
Desired Output
COUNTRY
REGION
DISTRICT
India
0
0
Italy
2
0
Philippines
1
3
Japan
0
0
Taiwan
0
0
Canada
0
0
USA
0
0
Russia
0
0
MY QUERY is working but groupby is not working when i apply and only 2 country will appear.
$getcountry = LocationModel::join('location_country as country','country.location_id','locations.id')
->join('locations_region','locations_region.country_id','=','country.id')
->select([
'locations.*',
'country.*',
'locations_region.*'
])
->orderBy('country.country')
->paginate(10);

Related

Making a scatterplot with PCA and how to read results

Im a little newbie with R and not familiar with PCA. My problem is, from a survey I have a list with observations from nine variables, first one is the gender of the respondents, the next five (Q51_1_c,Q51_2_c,Q51_4_c,Q51_6_c,Q51_7_c) ask about entrepreneurial issues and the others ask about future expectations (Q56_1_c, Q56_2_c, Q56_3_c). Except gender, all this variables takes values between 1 and 5. I want to make a scatter plot with two axis. First one with "entrepreneurial variables" and second axis with "future expectations variables" and then define as points in the scatter plot the position of Male and Female. My data look like this:
x <- "Q1b Q51_1_c Q51_2_c Q51_4_c Q51_6_c Q51_7_c Q56_1_c Q56_2_c Q56_3_c
3 Male 5 4 4 4 4 5 4 4
4 Female 4 3 4 4 3 3 4 3
5 Female 1 1 1 1 1 3 1 1
7 Female 2 1 1 1 1 5 1 4
8 Female 4 4 5 4 4 5 4 4
9 Female 3 3 4 4 3 3 4 4
13 Male 4 4 4 4 5 3 3 3
15 Female 3 4 4 4 4 1 1 5
16 Female 4 1 4 4 4 3 3 3
19 Female 3 2 3 3 3 3 3 3
20 Male 1 1 1 1 1 3 1 5
21 Female 3 1 1 2 1 3 3 3
26 Female 5 5 1 2 1 4 4 3
27 Female 2 1 1 1 1 1 1 1
29 Male 2 2 2 2 1 4 4 4
31 Female 3 1 1 1 1 5 2 3
34 Female 4 1 1 4 3 3 1 4
36 Female 5 1 1 4 4 5 1 2
37 Male 5 1 2 4 4 5 4 5
38 Female 3 1 1 1 1 1 1 1"
To run PCA this is my code:
x <- na.omit(x) #Jus to simplyfy
resul <- prcomp(x[,-1], scale = TRUE)
x$PC1 <- resul$x[,1] #Saving Scores PC1
x$PC2 <- resul$x[,2] #Saving Scores PC2
The result axis are like this:
biplot(resul, scale = 0)
Finally, to make the scatter plot:
x %>%
group_by(Q1b) %>%
summarise(mean_PC1 = mean(PC1),
mean_PC2 = mean(PC2)) %>%
ggplot(aes(x=mean_PC1, y=mean_PC2, colour=Q1b)) +
geom_point() +
theme_bw()
Which gives me this:
I'm not sure how about read the results... Should I accept that Females in general get higher values in the dimension of future expectations than Males. And Males get higher values in the entrepreneurial dimension?
Thanks in advance!!
Your interpretation of the axes looks correct, i.e., PC1 is a gradient which from left to right represents decreasing "entrepreneurialness", while PC2 is a gradient which from bottom to top represents increasing future expectations (assuming that "5" in the original data means highest entrepreneurialness/expectations).
In terms of whether males and females are different, you probably need to plot more than the just the means for each group: even if males and females are truly identical in their entrepreneurialness/expectations, you'd never expect the means from two samples to sit right on top of each other on a scatter plot. To address this, you could plot the actual observations rather than their means (i.e., one point per row, coloured by gender) and see if they intermingle vs. separate in the plot space. Or, regress gender against the principal components.
Another issue is whether it's appropriate to use PCA on ordinal data - see here for discussion.

aggregate ordered rows in hive table

i have a table in hive with 4 columns like this:
row_id| user_id|product_id| duration
1 1 product1 3
2 1 product1 1
3 1 product2 6
4 1 product3 2
5 1 product1 4
6 1 product4 3
7 1 product4 5
8 1 product4 7
9 2 product4 3
10 2 product4 6
i want to aggregate rows of the same product for each user, sum the duration and count the clicks only if they are consequent in order
row_id| user_id|product_id |duration_per_product |clicks_per_product
1 1 product1 4 2
2 1 product2 6 1
3 1 product3 2 1
4 1 product1 4 1
5 1 product4 15 3
6 2 product4 9 2
any ideas how to do that in hive 1.1.0?
group by obviously doesn't work because i don't want to group products if they are consequent , i have tried case,lag and lead but didn't work!
thanks!
First off, this is something you would want to do in a loop, hive is not very suitable for these kind of problems.
That being said, here is an approach that should work:
Suppose this is our dataset
1 1 product1 3
2 1 product1 1
3 1 product2 6
4 1 product1 4
Identify starter rows: 1,3,4
This can be done by doing a left join on id=id+1 and seeing whether user and product match.
Join everything onto these starters by user and product:
1 1
1 2
1 4
3 3
4 1
4 2
4 4
Filter out things that are in the wrong order (starter after row), remaining are:
1 1
1 2
1 4
3 3
4 4
Group to find the maximum valid starter for each row, remaining will be:
1 1
1 2
3 3
4 4
Now join to reattach the relevant dimensions
1 1 3
1 2 1
3 3 6
4 4 4
Now you can get the results by grouping on the starter id.
1 4
3 6
4 4
Of course you can now choose to use another join to attach the name of the product.
1 product1 4
3 product2 6
4 product1 4
And that is all!

PL/SQL Oracle 11g Looping

I am having trouble solve. I am suppose to be getting a record every time there is a change to an account in our data warehouse, but I am only receiving one. The table below is a sample of what I am working with.
Row Acct1 Acct2 Date Total_Reissued Reissue_Per_Day
1 A 1 1/1/2016 2 2
2 A 1 1/2/2016 3 1
3 A 1 1/3/2016 5 2
4 A 1 1/4/2016 6 1
1 B 3 1/1/2016 1 1
2 B 3 1/2/2016 2 1
1 B 4 1/1/2016 1 1
2 B 4 1/2/2016 2 1
The Reissued Column is a running total. For Acct A on 1/1/2016 there were 2 reissues, then On 1/2/2016 there was 1 more making a total of 3. My problem is calculating the actual number of reissues per day.
You can use the lag() function to peek back at the previous row; assuming that 'previous' is the last date you saw for the acct1/acct2 combination you can do:
select row_number() over (partition by acct1, acct2 order by dt) as row_num,
acct1, acct2, dt, total_reissued,
total_reissued - nvl(lag(total_reissued)
over (partition by acct1, acct2 order by dt), 0) as reissue_per_day
from your_table;
ROW_NUM A ACCT2 DT TOTAL_REISSUED REISSUE_PER_DAY
---------- - ---------- ---------- -------------- ---------------
1 A 1 2016-01-01 2 2
2 A 1 2016-01-02 3 1
3 A 1 2016-01-03 5 2
4 A 1 2016-01-04 6 1
1 B 3 2016-01-01 1 1
2 B 3 2016-01-02 2 1
1 B 4 2016-01-01 1 1
2 B 4 2016-01-02 2 1
I'm not sure if your 'row' column actually exists, or is required, or was just to illustrate your data. I've generated it anyway, in case you need it.
The main bit of interest is:
lag(total_reissued) over (partition by acct1, acct2 order by dt)
which finds the previous date's value (using dt as a column name, since date isn't a valid name). That then has an nvl() wrapper so the first row sees a dummy value of zero instead of null. And then that is subtracted from the current row's value to get the difference.

ORACLE calculate Sales، returns and the rest for a customer in the same table for the sam product

ORACLE select
calculate Sales، returns and the rest for a customer in the same table for the same product according to trans type
i need to calculate total sales and total returns and the rest for the customer and items.
and group by customer
Trans_Type:
1= Sales
2= Return
ID Trans_Type DATE Items_ID Quantity Clint_ID
--- ---------- -------- ---------- ---------- ----------
1 1 16-OCT-09 701555 3 1
2 2 12-DEC-09 701555 1 1
3 1 30-JUL-10 701511 63 2
4 2 30-JUL-10 701555 1 1
5 1 30-JUL-10 701234 2 3
6 1 30-JUL-10 701234 5 3
7 2 30-JUL-10 701511 1 2
8 1 30-JUL-10 701522 3 2
9 1 30-JUL-10 701555 2 3
10 1 30-JUL-10 701555 4 2
11 2 30-JUL-10 701555 2 2
If I understood everything correct you need to use case when ... and group by ... clauses, like here:
select clint_id, items_id, qty, ret, nvl(qty,0) - nvl(ret,0) rest
from (
select clint_id, items_id,
sum(case when trans_type = 1 then quantity end) qty,
sum(case when trans_type = 2 then quantity end) ret
from data group by clint_id, items_id )
order by clint_id, items_id
SQLFiddle demo

Reporting Services: how to apply interactive sort for matrix columns?

I need to apply interactive soring for matrix columns, which contain aggregated data.
The report is counting products sold in different places:
Product A Product B Product C
---------------------------------------------------------------
Country 1 5 10 4
City A 3 0 3
City B 2 10 1
---------------------------------------------------------------
Country 2 10 5 5
City C 2 4 2
City D 8 1 3
After descending sorting on "Product A" the table rows should be sorted by "Product A" sales in country, and also by sales in city:
Product A Product B Product C
---------------------------------------------------------------
Country 2 10 5 5
City D 8 1 3
City C 2 4 2
---------------------------------------------------------------
Country 1 5 10 4
City A 3 0 3
City B 2 10 1
The matrix scheme looks like this:
| [Product]
[Country] | [City] | [Count(Product)]
Interactive sort is not supported in matrix.
A workaround could be the one below:
Create a sort by parameter with the values:
Label Value
Country ASC, City ASC 1
Country DESC, City ASC 2
Country ASC, City DESC 3
Country DESC, City DESC 4
Then in the country create two sorting expressions:
=Iif(Parameters!SortBy.Value = 1 OR Parameters!SortBy.Value = 3,Fields!country.Value,"")
ASCENDING sort
=Iif(Parameters!SortBy.Value = 2 OR Parameters!SortBy.Value = 4,Fields!country.Value,"")
DESCENDING sort
Do the same for city:
=Iif(Parameters!SortBy.Value = 1 OR Parameters!SortBy.Value = 2,Fields!city.Value,"")
ASCENDING sort
=Iif(Parameters!SortBy.Value = 3 OR Parameters!SortBy.Value = 4,Fields!city.Value,"")
DESCENDING sort

Resources