Views Calc is a handy module that computes aggregate functions on Views results. The way it works is by creating a new SQL statement that adds the aggregate functions to the existing Views SQL query. Unfortunately, this approach means that custom fields cannot be aggregated, since they don't exist in the database.
Attached is a patch for Views Custom Field that allows Views Calc to calculate custom fields. Please apply the patch and try it before I commit the changes to the 6.x-1.x-dev branch. To try it, you must be familiar with Views Calc. Then:
* Clear your views cache
* Instead of the "Views Calc Table" view style that it normally used with Views Calc, use the new "Views Custom Field Calc Table" style
* Using this new style, you can choose custom fields to be aggregated as you would do with normal fields
Comment | File | Size | Author |
---|---|---|---|
#16 | views_customfield-576264.patch | 9.87 KB | abaddon |
#1 | views_calc.patch | 10.65 KB | infojunkie |
Comments
Comment #1
infojunkieAnd here's the patch :-)
Comment #2
obrienmd CreditAttribution: obrienmd commentedDid this patch get committed? I have a bunch of failures when trying to run patch against current (as of today) 6.x-1.x-dev... And after patch was run, tons of errors in any views_calc view. Because I'm an idiot, I didn't attempt to just use dev and see if it was in there...
I'm heading out of office now, but would be happy to test further when I get back this afternoon.
Comment #3
obrienmd CreditAttribution: obrienmd commentedOK - back in office. I'm officially an idiot - everything is functional. However, I'm getting an odd MYSQL warning:
When my computed field is running the php:
This happens everywhere else that $data->cid is called (many different views custom fields), but they all DO produce the expected output, so $data->cid is obviously not returning null, as the SQL warning seems to suggest.
Thoughts?
Comment #4
infojunkieI'll review the patch for latest versions of Views Custom Field + Views Calc.
Concerning your query warnings, can you
dsm($data->cid)
to make sure the values are what you're expecting? And wouldn't it be better to make just one query with a couple of JOINs ?Comment #5
obrienmd CreditAttribution: obrienmd commentedHehe... I know nothing about databases, but if you wanted to help me w/ the joins... :)
The table actually shows the correct data, which makes the warnings even stranger.
Comment #6
infojunkieThe patch applies to the latest dev version of Views Custom Field without warnings. Please confirm.
As for the joined query, something like:
But that, of course, doesn't solve the weird warning that you got :-)
In any case, as this issue is concerned with the Views Calc patch, we can mark this as RTBC unless anyone else encounters a problem.
Comment #7
obrienmd CreditAttribution: obrienmd commentedWill confirm ASAP, probably tomorrow though. Thanks for the help w/ JOIN! Maybe I should actually learn how to make an efficient SQL query some day :)
Comment #8
obrienmd CreditAttribution: obrienmd commentedApologies for delay - this patch works great against current -dev.
Comment #9
Bilmar CreditAttribution: Bilmar commentedsubscribing
Comment #10
robby.smith CreditAttribution: robby.smith commentedsubscribing
Comment #11
Vacilando CreditAttribution: Vacilando commentedSubscribing.
Comment #12
quinnhigurashi CreditAttribution: quinnhigurashi commentedI am trying this patch. However, I get this error on my testing environment. What went wrong?
I see no customfield table on my mysql database.
Comment #13
quinnhigurashi CreditAttribution: quinnhigurashi commentedSorry, my bad. I didn't read the instruction. I selected "Views Custom Field Calc Table", then all is fine now.
Comment #14
YK85 CreditAttribution: YK85 commentedsubscribing
Comment #15
mpaler CreditAttribution: mpaler commentedHi,
Strange behavior here. I successfully applied the patch to the latest dev version. When I go edit the style format, I can see the new "Views Custom Field Calc Table" option. I select it. Update. But it won't stick. It always reverts back to previous selection (eg. Views Calc Table). Any ideas here?
Mike
Comment #16
abaddon CreditAttribution: abaddon commentedi can confirm this works, you need views_calc stable-6.x-1.3 and the views_customfield-6.x-1.x-dev with the patch in #1 applied
besides the steps outline in the original post above, you might also need to edit your customfields, and separate the actual value from the formatted output for this to work (for eg. im outputting prices, and i needed to take the '$' out of the actual value)
with latest views_calc -DEV:
PHP Fatal error: Cannot use object of type stdClass as array in sites/all/modules/views_customfield/includes/views_customfield_calc_table.inc on line 14
i attached a re-roll of the patch because #1 applied with fuzz
Comment #17
abaddon CreditAttribution: abaddon commentedim actually hitting this item in the patch:
if i comment it out, i dont get this error in the logs and everything seems fine:
"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ',,,,,,,,,,,,,,,,,,,,,,,,,,)))... i have this in the sql that gives the error "AND (node.nid IN (,,,,,,,,,,,,,,,,,,,,,,,,,,,)))"
@infojunkie, any hints on what would be needed for the TODO item?
Comment #18
brigitte CreditAttribution: brigitte commentedHi,
Thanks for this great module and this patch.
In my view, the total SUM doesn't display if equal to 0.
Any idea how to display 0 ?
Thanks for any help.
Comment #19
scott859 CreditAttribution: scott859 commentedGreetings,
I am having the same issue as #15, I successfully applied the patch in #1, then when I select the "Views Custom Field Calc Table" option it reverts to the previous selection.
Any insight into what may be the issue here would be appreciated.
Thanks in advance.
Scott
Comment #20
SanDiego CreditAttribution: SanDiego commentedI installed the patch and now I can see the new style option in the views. But no calculation is made, namely all calculation rows are blank/empty. "Views calc" computes correctly though. By the way, I don't get any error when selecting a customfield, which I did with "Views Calc Table". Do you have any idea what might go wrong?
Comment #21
krisrobinson CreditAttribution: krisrobinson commentedSubscribing
Comment #22
DarrellDuane CreditAttribution: DarrellDuane commentedThis patch worked perfectly for me against the dev version, I recommend this patch be committed into the module.
Comment #23
Sol Roth CreditAttribution: Sol Roth commentedDid you ever figure this out? I'm having this same issue as #15 and #19 - on drupal 6 site I'm doing some dev work on.
Comment #24
soulfroys#16 works!!!
Views Calc 6.x-1.3
Views Custom Field 6.x-1.x-dev
Thank you!
Comment #25
jvieille CreditAttribution: jvieille commentedNot committed yet?
Comment #26
jvieille CreditAttribution: jvieille commented#16 is OK.
I confirm that Views Calc DEV does not work with this patch.
I understand that this patch provides a new Views style that only works with custom fields, so one cannot mix database actual fields and computed custom field values in the same table.
I would have thought that this patch would better be part of Views Calc.