codeigniter - gantt report from mysql table - mysql

i'd like to generate some gantt-report (inside html table) with data from mysql table to my ci-view..
the view should be like this :
| name | week#1 | week#2 | week#3 | week#4 |
| wawan | 200 | 340 | 0 | 100 |
| wewe | 210 | 340 | 120 | 100 |
| elias | 200 | 360 | 0 | 170 |
weeks are distinct from field 'week'.
names are distinct from field 'user'.
numbers are sum(qtyfield) from table where user is 'name' during x week
i've tried this method, but the numbers (sum of 'qty' field) won't show :
fetch the weeks row to a table-header block inside a foreach loops -> done.
fetch the names row to a table-row block inside a foreach loops -> done.
the sum(qty) is query base on name in table-row and week in table-header -> ?
any idea how to do this? syntax help or just logic thought are appreciated..
thanks in advance..

Related

Laravel get "unique" row from mysql

I have a special scenario to fetch "unique" row.
Let's say the database is like below
| id | userid | value | others |
|----|--------|-------|---------|
| 1 | 111 | 10 | string1 |
| 2 | 112 | 30 | string2 |
| 3 | 112 | 30 | string3 |
| 4 | 113 | 50 | string4 |
what I want to achieve is to fetch the unique rows based on the "userid" so I'am able to sum all values.
the expect output row can be either id: 1 2 4 or 1 3 4 (both is acceptable for this special case because same id guarantees same value, or in general, get just one row from those row with same userid. ), so the sum will be 90.
Note: DB is extended from Eloquent\model
My old approach is to get DB::unique('userid'); then for each userid DB::where('userid', $id)->value('value'), add the result to sum; I just believe there might be a better approach.
There is Illuminate\Support\Facades\DB in Laravel, it can return the Query Builder. Not recommend to use a model that is named DB.
So just change another name.
By the way, for Eloquent\Model, you can use groupBy and sum too:
Model::groupBy('user_id')->sum('value');

Mysql concatenate only specific rows

In Mysql, I have the following table:
id | paramname | paramcategory | value |
---+-------------+-------------------+-----------------+
1 | width | dimensions | 240 |
2 | height | dimensions | 400 |
3 | param1 | category1 | some value 1 |
4 | param2 | category1 | some value 2 |
5 | param3 | category10 | some value 100 |
...
I'd like to have a query that will return a table with only several rows concatenated, and all other rows should remain intact, something like this:
paramname | value |
--------------+--------------+
width, height | 240 x 400 |
param1 | some value 1 |
...
I'm thinking about concatenating based on the needed paramcategory, but if possible/needed, concatenation can happen for specific paramnames as well. Whatever is easier/simpler.
Any help please?
Looking at this problem from above, you are going to have to 'UNION' 2 queries together. The first part of the union is your concat'd results, the second your original rows. For the first part you are going to need to do a self join on this table, along the lines of
select concat(a.paramname, b.paramname), concat(a.value, b.value) from table a, table b where a.paramcategory = b.paramcategory
along those lines....
Actually if you swap the 2 parts of the union around, you'll keep the original column names too.

Select statement inside SSRS textbox

I am trying to assign values to textboxes in SSRS but am running into a bit of trouble.
My dataset "dsStepInfo" groups and sums a number of rows together based on "ID", query below.
SELECT ID, SUM(Target) AS Target, SUM(Actual) AS Actual
FROM tblStepComplete
WHERE (CompleteID = #ParamID)
GROUP BY ID
And returns:
ID Target Actual
-------------------------------
| 10 | 44418 | 44418 |
-------------------------------
| 12 | 13193 | 13123 |
-------------------------------
| 22 | 1411 | 1411 |
-------------------------------
| 50 | 160 | 80 |
-------------------------------
| 52 | 68 | 34 |
-------------------------------
| 101 | 12120 | 12119 |
-------------------------------
| 105 | 875 | 868 |
-------------------------------
| 140 | 40 | 40 |
-------------------------------
| 560 | 2985 | 3418 |
-------------------------------
I want to assign cells in the grid in the picture to a certain ID. The Cell for tank 110 would always be the Target of ID=50 and Actual of ID=50 for example. Many of the IDs returned aren't going to be populated in the table, just the 10 displayed in this table. Is there any way to perform a SELECT, or equivalent in the textbox as an expression to get these specific values from the dataset?
You can use the Lookup function.
This should get the ID #50 and output its Target:
=Lookup(50, Fields!ID.Value, Fields!Target.Value, "DSStepInfo")
I think if it were me I'd just use a Case statement, something like
SELECT
Case [ID]
WHEN 10 Then 'Tank 50'
WHEN 12 Then 'Tank 120'
When 22 Then 'Tank 130'
WHEN 50 Then 'Tank 140'
When 52 Then 'Tank 150'
End As TankNo
,SUM([Target]) As Target
,SUM([Actual]) As Actual
FROM tblStepComplete
WHERE (CompleteID = #ParamID)
Group By [ID]
And then build your report using the values presented. Better would be an additional table that had the Tank names linked to the IDs you are pulling, but if you don't have that then the Case statement is fairly easy to maintain.

MS Access transform statement calculated fields

The Transform Statement:
TRANSFORM Sum(January2015.txnamount) AS SumOftxnamount
SELECT January2015.txndate2, January2015.cbsaccno
FROM January2015
GROUP BY January2015.txndate2, January2015.cbsaccno
PIVOT January2015.txntypes;
gives output as below:
+------------+---------------+-----+-----+
| txndate2 | cbsaccno | A1 | A2 |
+------------+---------------+-----+-----+
| 01/01/2015 | 0021010392747 | 50 | 300 |
| 01/01/2015 | 0021010392891 | 100 | 200 |
+------------+---------------+-----+-----+
I want a fifth field (calculated field A1-A2)
which have values -250 and -100 for the sample data above.
Anyway to do that?
Thanks in advance.
Save the query you built (for example, PivotQuery). Create a new query that calls PivotQuery with the following SQL:
SELECT PivotQuery.*, [a1]-[a2] AS CalcField
FROM PivotQuery;

How to replace substring in mysql where string is based on other table-column values

I have two mysql tables as
Component
+----+-------------------------+--------+
| OldComponentId | NewComponentId |
+----+-------------------------+--------+
| 15 | 85 |
| 16 | 86 |
| 17 | 87 |
+----+-------------------------+--------+
Formulae
+----+-------------------------+--------+
| id | formula_string |
+----+-------------------------+--------+
| 1 | A+15-16+17 |
| 2 | 16+15-17 |
+----+-------------------------+--------+
I want to replace value of formula_string on the basis of NewComponentId as
Formulae
+----+-------------------------+--------+
| id | formula_string |
+----+-------------------------+--------+
| 1 | A+85-86+87 |
| 2 | 86+85-87 |
+----+-------------------------+--------+
I have tried with following mysql query but its not working
update Formulae fr, Component comp set formula_string=REPLACE(fr.formula_string,comp.OldComponentId,comp.NewComponentId).
Please suggest the solutions
thanks.
There is no easy way to do this. As you observed in your update statement, the replacements don't nest. They just replace one at a time.
One thing that you can do is:
update Formulae fr cross join
Component comp
set formula_string = REPLACE(fr.formula_string, comp.OldComponentId, comp.NewComponentId)
where formula_string like concat('%', comp.OldComponentId, '%')
Then continue running this until row_count() returns 0.
Do note that your structure could result in infinite loops (if A --> B and B --> A). You also have a problem of "confusion" so 10 would be replaced in 100. This suggests that your overall data structure may not be correct. Perhaps you should break up the formula into separate pieces. If they are just numbers and + and -, you can have a junction table with the value and the sign for each component. Then your query would be much easier.