How to filter a pivot table using Eloquent?
Laravel 4.1 brings native wherePivot and orWherePivot methods, which is directly a solution to my problem.
Laravel 4.1 brings native wherePivot and orWherePivot methods, which is directly a solution to my problem.
I wrote a simple general custom function, which is 100% reusable you can unpivot / reverse pivot a table of any size. In your case you could use it like this: =unpivot(A1:D4,1,1,”customer”,”sales”) So you can use it just like any built-in array function in spreadsheet. Please see here 2 examples: https://docs.google.com/spreadsheets/d/12TBoX2UI_Yu2MA2ZN3p9f-cZsySE4et1slwpgjZbSzw/edit#gid=422214765 The following is the … Read more
SELECT unnest(array[‘Sl.no’, ‘username’, ‘Designation’,’salary’]) AS “Columns”, unnest(array[Sl.no, username, value3Count,salary]) AS “Values” FROM view_name ORDER BY “Columns” Reference : convertingColumnsToRows
To “reverse pivot”, “unpivot” or “flatten”: For Excel 2003: Activate any cell in your summary table and choose Data – PivotTable and PivotChart Report: For later versions access the Wizard with Alt+D, P. For Excel for Mac 2011, it’s ⌘+Alt+P (See here). Select Multiple consolidation ranges and click Next. In “Step 2a of 3”, choose … Read more
You can. From this example in Docs (4.2, 5.0): $user->roles()->sync(array(1 => array(‘expires’ => true))); Hardcoded version for the first two rows: $food = Food::find(1); $food->allergies()->sync([1 => [‘severity’ => 3], 4 => [‘severity’ => 1]]); Dynamically, with your arrays $allergy_ids and $severities in a compatible state (size and sort), you shall prepare your sync data before. … Read more
SELECT unnest(array[‘Sl.no’, ‘username’, ‘Designation’,’salary’]) AS “Columns”, unnest(array[Sl.no, username, value3Count,salary]) AS “Values” FROM view_name ORDER BY “Columns” Reference : convertingColumnsToRows
Here are couple of ways to reshape your data df In [27]: df Out[27]: Col X Col Y 0 class 1 cat 1 1 class 2 cat 1 2 class 3 cat 2 3 class 2 cat 3 1) Using pd.crosstab() In [28]: pd.crosstab(df[‘Col X’], df[‘Col Y’]) Out[28]: Col Y cat 1 cat 2 cat … Read more
You need remove only index name, use rename_axis (new in pandas 0.18.0): print (reshaped_df) sale_product_id 1 8 52 312 315 sale_user_id 1 1 1 1 5 1 print (reshaped_df.index.name) sale_user_id print (reshaped_df.rename_axis(None)) sale_product_id 1 8 52 312 315 1 1 1 1 5 1 Another solution working in pandas below 0.18.0: reshaped_df.index.name = None print … Read more
If you put State and City not both in the rows, you’ll get separate margins. Reshape and you get the table you’re after: In [10]: table = pivot_table(df, values=[‘SalesToday’, ‘SalesMTD’,’SalesYTD’],\ rows=[‘State’], cols=[‘City’], aggfunc=np.sum, margins=True) In [11]: table.stack(‘City’) Out[11]: SalesMTD SalesToday SalesYTD State City stA All 900 50 2100 ctA 400 20 1000 ctB 500 30 … Read more
You could group by both the bins and username, compute the group sizes and then use unstack(): >>> groups = df.groupby([‘username’, pd.cut(df.views, bins)]) >>> groups.size().unstack() views (1, 10] (10, 25] (25, 50] (50, 100] username jane 1 1 1 1 john 1 1 1 1