• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

SQL Server question (coldfusion used)

Hi Guys,

I have a query that runs that selects all matching records according to a user_id. Each record is for a shortened url, something like tinyurl.com.

I have another table that collects data about each time a url is visited. (time, date, location).

Then there is a 3rd table which is for listing which member actually owns each record.

I know DNS is for domain name system, but it's the shorter than 'redirect' and works for me.

dns (table for all redirect entries)
dns_hits (table that counts hits on a particular redirect)
dns_owners (table that relates redirects to members)


What I'm trying to do is query select all records from table 'dns' that match a particular member id. I have a query for that below, and it works. However, I'm also wanting to compile a hit count for each redirect that matches in the query.

So as an example, the query below might grab 6 records for a particular member. This means they have 6 entries in our database for url shorteners (like having 6 tinyurl.com addresses). The query fetches the shortened url (tinyurl.com/12kdf) and the url it forwards to (mylongurl.com/2343/blalal), and it also grabs the unique ID

Record # - Shortened URL - Destination - unique ID

1 - tinyurl.com/sdfdfd - www.mytesturl.com - 456
2 - tinyurl.com/sdadsfdd - www.mytesturl123.com - 458
3 - tinyurl.com/DFDdfd - www.mytesturl456.com - 455

So the query grabs those records and each record contains a unique ID from another table. (as seen above). How can I join that table of hits, use the unique ID to query the table of hits, count the matches, and include that in the same query? Or will I have to do two queries and combine the data somehow?

<CFQUERY NAME="list_member_dns" DATASOURCE="dns">

SELECT
dns.dns_id,
dns.dns_domain,
dns.dns_subfolder,
dns.dns_redirect_url

FROM dns

INNER JOIN
dns_owners on dns.dns_id = dns_owners.dns_id

WHERE dns_owners.dns_member_id = <CFQUERYparam value="#myid#">

</CFQUERY>

I'd want to modify the query above to also join to the dns_hits table and do a count on the number of records that match the unique_id (dns.dns_id).
 
Last edited:
I kinda got it figured out, but not totally. I've modded the sql to this

<CFQUERY NAME="list_member_dns" DATASOURCE="dns">

SELECT
dns.dns_id,
dns.dns_domain,
dns.dns_subfolder,
dns.dns_redirect_url

(
Select
Sum(dns_hit_id) As dnshits
From dns_hits
Where dns_hits.dns_id = dns.dns_id
)
AS Totalhits

FROM dns

INNER JOIN
dns_owners on dns.dns_id = dns_owners.dns_id

WHERE dns_owners.dns_member_id = <CFQUERYparam value="#myid#">

</CFQUERY>

However, I'm summing the wrong value.

What I'm getting is a sum of the dns_hit_id's, not the amount of records.

So if there are 3 hits

ID - TIME - IP

56 - 07/19/2010 5:45pm - 66.76.125.23
78 - 07/19/2010 8:35pm - 62.26.225.13
156 - 07/21/2010 1:15pm - 55.12.12.33

The code above is summing the ID's, which in the case would be 56+78+156 = 290

So 'TotalHits' is set to 290, when what I want is it to be set to '3'
(because there is 3 records)

Any ideas?
 
Back
Top