ColdFusion Web Poll - Update poll results in MS Access Database - coldfusion

How can I code when option1 is selected to update access db with + 1 vote. Database only has one record with Option1, Option2, Option3 & etc in each column. The total vote count will display under each column based on which option is choosen.

Database only has one record with
Option1, Option2, Option3
The biggest problem is your table structure. Manipulating the data would be far easier if the options were stored in rows (not columns). For a very simple table, insert each option as a separate row, initialized with 0 votes:
RecordID | OptionName | TotalVotes
1 | T-Shirt 1 | 0
2 | T-Shirt 2 | 0
3 | T-Shirt 3 | 0
....
5 | T-Shirt 5 | 0
Then use the results of your SELECT query to populate your form (or display the totals if needed):
<cfoutput query="poll">
<input type="radio" name="TshirtOption" value="#RecordID#"> #OptionName#
...
</cfoutput>
When the form is submitted, increment the total votes for the selected option. Add validation of course.
<cfquery name="updateVote" datasource="fiteastpoll">
UPDATE Tshirt_poll
SET TotalVotes = TotalVotes + 1
WHERE RecordID = <cfqueryparam value="#form.TshirtOption#" cfsqltype="cf_sql_integer">
</cfquery>

I am assuming submitted option comes as form variable with name selectedOption and try below query...
<cfquery name="qUpdate" datasource="datasourcename">
Update TShirt_port set option#form.selectedOption# = option#form.selectedOption# + 1
</cfquery>

Related

Summarize a table based on a column in another table

I have two tables in Power BI model
Table A
value1
value2
value3
....
value 1000
Table B
value 1 | 10
value 2 | 10
value 1 | 50
value 3 | 10
value 1 | -10
value 2 | 70
Can I make a new column (or measure) in Table A to Sum UP connected values ???
Expected RESULT:
value 1 | 50 --- (10+50-10)
value 2 | 80 --- (10+70)
value 3 | 10 --- (10)
Just something like SUM.IF in Excel, which can I drag to all rows ? Thanks in advance.
I tried to CALCULATE, but I can't do this for all different rows in Table A
You don't need Table A for this. SUMMARIZE() will create a column of distinct values to group by. Use the following Calculated Table. Note that this is NOT a MEASURE!
Result =
SUMMARIZE(
'Table B',
'Table B'[value ID],
"Sum", SUM('Table B'[number])
)
Yes, Possible ! Just Add a new column on your table, and write this DAX Code after relationship is created!

Loop through a ColdFusion query and sorting results

I have two tables:
Users (2 columns): ID, DisplayName, Active
Ticket_Followups (4 colums): id, requested_by, requested_date, ticket_id
I am tryiwng to group all the similar records in the ticket_followup table, first by recordcount and then by displayName.
Here is what I have so far:
<cfquery name="active_users" datasource="#datasource#">
select * from users
where active='1'
</cfquery>
<cfloop query="active_users">
<cfquery name="get_followups" datasource="#datasource#">
select date_of_followup_request, requested_by, ticket_id
from ticket_followup
where requested_by = '#active_users.displayName#'
</cfquery>
<cfoutput>
<tr>
<td>#active_users.displayName#</td>
<td>#get_followups.recordcount#</td>
</tr>
</cfoutput>
</cfloop>
I am able to successfully show the output for the total records by user, but there is no order to the output. I would like to group it so that it shows the DisplayName with the highest recordcount first, descending in order.
How can I do that?
This is a SQL issue, CF is just displaying data after the data is gathered.
You need to do this in one query.
You need to associate the ticket follow ups by user ID, not by name (Name could change, but not the ID).
There's a table of tickets I assume, but we'll stick to your two tables.
First, the tables:
Users
----------
id
DisplayName
Active
Ticket_Followups
----------
id
requested_by_id (Users.id)
requested_date
ticket_id
You can technically join by name, but it's a much slower query and I've no idea how much data you have.
This query joins the two tables and gives you a count of ticket follow ups by user. You can add an ORDER BY statement before the GROUP BY depending on your needs.
SELECT
a.DisplayName
, count(*) AS requested_count
FROM
Users AS a
INNER JOIN
Ticket_Followups b ON b.requested_by_id = a.id
WHERE
a.active = 1
GROUP BY
a.id
If you don't do this in one query, then for every user that has an active ticket, you're making another query.
10 users, 11 queries
20 users, 21 queries
etc.
Updated 2022-02-15
Query using DisplayName with an ORDER BY clause. This should make it clearer that you're counting the tickets per user and not the number of users.
SELECT
a.DisplayName
, count(a.*) AS ticket_count
FROM
Ticket_Followups AS a
INNER JOIN
Users AS b ON b.DisplayName = a.DisplayName
WHERE
a.active = 1
ORDER BY
a.DisplayName DESC
GROUP BY
a.DisplayName
Output:
<cfoutput query="queryName">
<li>#queryName.DisplayName# - #queryName.ticket_count#</li>
</cfoutput>

Django query annotate with applied distinct on another field

assume I have a db table with following rows:
some_key - price
1 | 100
1 | 100
2 | 150
2 | 150
3 | 100
3 | 100
I want to list users with their total expending according to orders table. but for whatever (stupid) reason each row may have been duplicated. so I should add distinct on "some_key" column and obviously this code bellow won't work.
how could I annotate Sum of prices with distinct on "some_key" column
query = User.objects.filter(<...>).annotate(price_sum=Sum("orders_set__price", distinct=True))
any suggestion?

Dax - Creating Column That Checks Dates Sequence

I have im my model table that contains data from reports based on monthly reporting of employees with column names "ReportDate" and enployye numbers.
I want to check that there is no gaps between the monthly dates to each employee with DAX.
For example:
EmpNum | ReportDate | CheckColumn
111 | 30.08.2019
111 | 30.09.2019
111 | 31.10.2019
222 | 30.08.2019
222| 31.10.2019 ----------> Here I want alert in my CheckColumn
Can someone find me a solution?
First you need to create a index column. Go to Edit Queries > Add Column > Index Column, starting with 1 for example.
Next you add a column with DAX which has a shift of 1 to the original column with this expression (make sure this column is from the same date format as your original column; Modelling > Format):
ShiftColumn = DATEVALUE(CALCULATE(MAX('Table'[Report Date]);FILTER('Table';'Table'[Index]=EARLIER('Table'[Index])-1)))
Next add the column with the check:
Column 2 = IF(DATEADD('Table'[Report Date].[Date];-1;DAY) = 'Table'[ShiftColumn]; TRUE(); FALSE())
The result:

How to order a query by values in different columns

I have a recordset named rsProductClass that is returned from a table in the database. It is a very simple SELECT * FROM Table WHERE ProductID = {ID Value Here} and the table is like this:
ProductID | UPPERTIER | LOWERTIER | NATIER | OTHERTIER
1 20 60 10 10
2 10 90 NULL NULL
3 NULL 40 NULL 5
The table may or may not have a value for each of the various tiers.
What I want to do is show to the user which column has the highest value and what the name of that column is. So for example, if you were looking at ProductID 2, then the page should display "This is likely to be a LOWERTIER product"
I need to sort the rsProductClass query in such a way that it returns me a list of columns in that query ordered by the value in each column. I want to treat the NULL values as zeros.
I tried to mess about with doing valuelist() and some ArrayToList() type functions but it crashes on the NULL values. Say I add columns to an array, and then use ArraySort() to get them in some kind of order, I'll get an error saying something like "Position 1 is not numeric" because it has a NULL value.
Is this something that can be done by ColdFusion? I suppose its some sort of pivoting of the recordset which is beyond my ability.
Something like this would work:
<cfquery name="tiers" datasource="...">
SELECT ProductID, UPPERTIER VALUE, 'UPPERTIER' TIER
WHERE UPPERTIER IS NOT NULL
UNION
SELECT ProductID, LOWERTIER VALUE, 'LOWERTIER' TIER
WHERE LOWERTIER IS NOT NULL
UNION
SELECT ProductID, OTHERTIER VALUE, 'OTHERTIER' TIER
WHERE OTHERTIER IS NOT NULL
UNION
SELECT ProductID, NATIER VALUE, 'NATIER' TIER
WHERE NATIER IS NOT NULL
ORDER BY ProductID, VALUE
</cfquery>
<cfset productGroup = StructNew()>
<cfoutput query="tiers" group="ProductID">
<cfset productGroup[ProductID].TIER = TIER>
<cfset productGroup[ProductID].VALUE = VALUE>
</cfoutput>
<cfdump var="#productGroup#">
Starting with ColdFusion 10 you can use <cfloop query="..." group="...">, before that <cfoutput> must be used.
If you're willing to unpivot your query, you might do something like the following. I used COALESCE() instead of ISNULL() (either one works in this situation, but COALESCE() is the ANSI standard). The column tier_rank will give the rank of the given tier -- that is, the tier with the highest value will have a rank of 1. If there are two tiers that both have the highest value, then both will have a value in tier_rank of 1 (this is why you would use RANK() instead of ROW_NUMBER() -- you could also use DENSE_RANK() if it better fits your requirements):
SELECT p1.product_id, p1.tier_name, p1.tier_value
, RANK() OVER ( PARTITION BY p1.product_id ORDER BY p1.tier_value DESC ) tier_rank
FROM (
SELECT product_id, 'UPPERTIER' AS tier_name
, COALESCE(uppertier, 0) AS tier_value
FROM products
UNION ALL
SELECT product_id, 'LOWERTIER' AS tier_name
, COALESCE(lowertier, 0) AS tier_value
FROM products
UNION ALL
SELECT product_id, 'NATIER' AS tier_name
, COALESCE(natier, 0) AS tier_value
FROM products
UNION ALL
SELECT product_id, 'OTHERTIER' AS tier_name
, COALESCE(othertier, 0) AS tier_value
FROM products
) p1
Please see SQL Fiddle demo here.
It might be possible to re-pivot the above unpivoted query, but I must admit my attempts at doing so failed.
I had to do something similar to this recently and looked into UNPIVOT in SQL Server. Going with the suggestion to Unpivot your query like David said, you could do something like this. This doesn't add RANK column, but it does order the values.
SELECT ProductID, Tier, TierValue
FROM
(SELECT ProductID, ISNULL(UpperTier,0) UpperTier, ISNULL(LowerTier,0) LowerTier, ISNULL(NaTier,0) NaTier, ISNULL(OtherTier,0) OtherTier
FROM products) p
UNPIVOT
(TierValue FOR Tier IN
(UpperTier, LowerTier, NaTier, OtherTier)
)AS unpvt
ORDER BY ProductID, TierValue Desc
SQL FIDDLE