I have query data that should be inserted in the structure. While looping over data each value should be assigned to matching column. Here is example of query data:
REC_ID NAME STATUS TYPE YEAR
1 01 PARIS Approved 1 2019
2 06 AUSTIN Inactive 3 2017
3 48 LONDON Approved 2 2018
4 43 ROME Inactive 5 2019
5 61 DUBLIN Inactive 4 2019
This data should be organized to look like this:
1
REC_ID 01
NAME PARIS
STATUS Approved
TYPE 1
YEAR 2019
2
REC_ID 06
NAME AUSTIN
STATUS Inactive
TYPE 3
YEAR 2017
3
REC_ID 48
NAME LONDON
STATUS Approved
TYPE 2
YEAR 2018
4
REC_ID 43
NAME ROME
STATUS Inactive
TYPE 5
YEAR 2019
5
REC_ID 61
NAME DUBLIN
STATUS Inactive
TYPE 4
YEAR 2019
I tried to get desired data format with this function:
function formatData(qryData) {
local.fnResult = structNew();
local.fnData = structNew();
if(qryData.recordcount){
for(row in qryData) {
for(column in qryData.columnList) {
local.strRec = structNew();
structInsert(strRec, column, row[column]);
local.fnData[qryData.currentrow] = strRec;
}
}
}
writeDump(fnData);
}
Here is how my result looks:
1
REC_ID 01
2
REC_ID 06
3
REC_ID 48
4
REC_ID 43
5
REC_ID 61
I use ColdFusion 11 cfscript syntax. If anyone can help me solve this problem please let me know. Thank you.
There is an issue with your column list loop, you are creating a new structure for each column and that get reset in the next loop.
This loop should be changed
for(column in qryData.columnList) {
local.strRec = structNew();
structInsert(strRec, column, row[column]);
local.fnData[qryData.currentrow] = strRec;
}
to look like
local.strRec = structNew();
for(column in qryData.columnList) {
structInsert(strRec, column, row[column]);
}
local.fnData[qryData.currentrow] = strRec;
#espresso_coffee, Here I've go through your problem. I hope you need to create a structure with key as row count and each key value should be in a structure format. Here I've provide some sample code for with my query. I hope it's useful to you.
<cfquery name="readStudent" datasource="student">
select * from user
</cfquery>
In my user table having 6 records with firstname, lastName & id. Here I've loop over the query and convert it in to structure key values. I've used script syntax because your code having script style. :)
<cfscript>
myStr = {};
for (row in readStudent) {
structInsert(myStr, #readStudent.currentrow#, row);
}
writeDump(myStr);
</cfscript>
Here I've create my first structure name as myStr, and loop over query data and insert a structure value for key with current row that is readStudent.currentRow and value is in row . The row having firstname,lastname & id as structure key & it's value.
FYR : I've attached my sample query and converted to structure value. Correct me if I'm wrong understood your problem
I hope it's helpful to you. Thanks.
If I understand this correctly, it looks like you could just
array function formatData(qryData) {
return DeserializeJSON(SerializeJSON(arguments.qryData, "struct"));
}
Runnable example on trycf.com
Result:
Alternative
array function formatData(qryData) {
var result = [];
for (var row in qryData) {
result.append(row);
}
return result;
}
Related
ID first_name last_name age grade address phone_num
1 John Doe 12 6 444 New york xxxxxxx
I want to automatically add value when the date has reached 31 May in grade column.
Let say if the date is reach 31 May then grade should be 7.
Thank you in advance
I have tried:
I2 = 6
E2 = 31/05/2022
=if(TODAY() = I2,E2+1,E2)
I'm not sure if this is the right way to do it.
Shouldn't that be (to fill in in cell "I3"):
I2 = 6
E2 = 31/05/2022
=IF(TODAY() = E2,I2+1,I2)
I have two sheets in Excel workbook.
The first sheet has
1) Customer ID – unique values for each customer.
2) Question ID – unique id for each question
3) Questions
Customer ID Question ID question
1 34 name
1 45 company
2 34 name
2 45 company
3 34 name
3 45 company
4 34 name
4 45 company
5 34 name
5 45 company
The second sheet has three columns
1) Customer ID – unique values for each customer.
2) Question ID – unqiue id for each question
3) Questions
Customer ID Question ID Answer
1 34 Amy
1 45 GEICO
2 34 Steph
3 34 Anna
3 45 GEICO
4 34 Adam
5 34 Mark
5 45 AAA
In this sheet, not every customer id and Question ID in sheet one will have answers in the sheet 2
Sheet 3 Expected Output
I wanted to do a vba macro to combine both sheet1 and sheet2 and have all the columns. For any customer id, if there is no answer for a question, that field should be left blank.
Expected Output in Sheet3
Customer ID Question ID question Answer
1 34 name Amy
1 45 company GEICO
2 34 name Steph
2 45 company
3 34 name Anna
3 45 company GEICO
4 34 name Adam
4 45 company
5 34 name Mark
5 45 company AAA
There are several ways this can be done without writing code.
Below is one method off the top of my head. Others include the built-in query editor (Get & Transform), or PivotTables and others ways to consolidate data in multiple worksheets.
On Sheet2, first set up a "helper column" since there are multiple columns you want to match. In this example the formula is: =C2&D2 starting in Cell B2.
...then, in Sheet1 (cell E2 in the example), use a formula like:
=IFERROR(VLOOKUP(B2&C2,Sheet2!$B$1:$E$9,4,FALSE),"")
Both formulas get copied or "dragged" down as far as necessary and obviously the formula adjusted to refer to the correct cells.
No third worksheet is necessary but if you want you can start by copying Sheet1 to Sheet3.
More Information:
Microsoft Support : VLOOKUP Function
Microsoft Support : Lookup & Reference Functions
Microsoft Support : IFERROR Function
I have an issue with Pivot table in Python. Let's say that I have below values in list:
team_A_id = [1,5,10]
team_A_result = 0
and below data frame:
id points
3 36
4 0
5 11
7 6
10 23
How could I using (perhaps) "for loop" find by team A id in list points and count them. Output should be:
result_team_A = 34
Thanks for any help
You are looking for isin and sum
team_A_id = [1,5,10]
df.loc[df.id.isin(team_A_id),'points'].sum()
Out[136]: 34
this will return the rows for the team A:
df.iloc[team_A_id]
result team A can be obtained by:
df['points].sum()
TLDR:
df.iloc[team_A_id]['points].sum()
I want some cohort analysis on a userbase. We have 2 tables "signups" and "sessions", where users and sessions both have a "date" field. I'm looking to formulate a query that yields a table of numbers (with some blanks) that shows me: a count of users who created an account on a particular day and ho also have a session created , indicating that he returned on that day, 3rd day, 7th day and 14 day.
created_at d1 d3 d7 d14
05/07/2007 12 * * *
04/07/2007 49 21 1 2
03/07/2007 45 30 * 3
02/07/2007 47 41 18 12
...
In this case, 47 users who created an account on 2/07/2007 returned after 3 days(d3)
Can I perform this in a single MySQL query?
Yes you can:
Select Signups.date as created at,
count (distinct case when datediff(sessions.date, signups.date)=1 then signups.users else null end) as d1,
count (distinct case when datediff(sessions.date, signups.date)=3 then signups.users else null end) as d3,
count (distinct case when datediff(sessions.date, signups.date)=7 then signups.users else null end) as d7,
count (distinct case when datediff(sessions.date, signups.date)=14 then signups.users else null end) as d14 from signups
left join sessions using(users)
group by 1
I have a requirement in which i have to pick up the initial value of each row in a crosstab..
My crosstab looks like this
value 1960 1970 2010 2011
aus 10 5 11 6
eng 5 2
bra 11 4
ind 8 11
i have to add another column which picks up the initial value for every row based on the year..
so the result should look like this.
value 1960 1970 2010 2011 initialValue
aus 10 5 11 6 10
eng 5 2 5
bra 11 4 11
ind 8 11 8
You should be able to use the minimum() function to determine the lowest value for year and then return the value corresponding to that. The expression for the initialValue data item would be something like:
total(
CASE
WHEN [Year] = minimum([Year] for [Language])
THEN [Value]
ELSE 0
END
for [Language])
We get the lowest year for the specific language in the data set using the minimum() function using the for clause to define the aggregation level. If the year of the row matches this number, we output the value, otherwise we output 0. We then total everything up for each language which should give us the value for the lowest year.
This solution assumes that the numbers displayed in your crosstab are totals of lower-level row detail. If the aggregate is something different, such as average or count, the wrapping summary function should be changed accordingly.