Ranking Values in .asp using Access - ms-access

fairly new to .asp and the boards, so please bear with me. I am trying to utilize an access DB which stores 24 unique records (baseball teams) with values for TeamName, TeamUID (unique ID), TotalHR, TotalRS, TotalWins and TotalLosses. I would like to use this table to display where a team ranks in each category. For example each team has their own page which is based on their TeamUID. I want to show where they rank (1st through 24th) in each category but cannot figure out how to do that in the page. Any suggestions?

You can assign a rank with MS Access, for example:
SELECT table1.ANumber,
(SELECT count(*)
FROM table1 t
WHERE t.anumber<=table1.anumber) AS rank
FROM table1
WHERE (((table1.ANumber) Is Not Null))
ORDER BY table1.ANumber;
Run against an ADO connection, you can use GetString or GetRows to assign this to a string or array.
The above example will give a duplicate number where there is a duplicate value for ANumber. For example:
ANumber Rank
-1 1
1 7
1 7
1 7
1 7
1 7
1 7
2 11
2 11

Related

Count Unique Values Throughout One Day For Nonconsecutive Student ID's

I am trying to figure out how to count all instances where a student is online without counting duplicate instances.
For example, in the screenshot below, I want to see a column counting only instances where a student is logged in. So, if Student A is logged in at 5 AM, count = 1. Student B logged in at 7, Count = 2. At some point student A logged off and logged back on at 8 am, the count should be 2, not 3.
Thank you!
Student
Time.
Desired Column (Count)
A
5 AM
1
B
7 AM
2
A
8 AM
2
C
9 AM
3
D
10 AM
4
E
11 AM
5
D
12 PM
5
I am mainly trying to track the activity and only count when someone is logged in. If those students appear multiple times, we can assume they logged off at some point and logged back in. It's basically a unique running count. Not sure how to write this in SQL. I hope this makes sense.
One option, use the exists operator with a correlated subquery to check if the student has logged in before:
SELECT Student, Time_,
SUM(flag) OVER (ORDER BY Time_) AS expected_count
FROM
(
SELECT *,
CASE
WHEN EXISTS(SELECT 1 FROM table_name D WHERE D.Student = T.Student AND D.Time_<T.Time_)
THEN 0 ELSE 1
END AS flag
FROM table_name T
) D
ORDER BY Time_
See demo.

How this query can be answered ? Select SUM(1) FROM table

select * from "Test"."EMP"
id
1
2
3
4
5
Select SUM(1) FROM "Test"."EMP";
Select SUM(2) FROM "Test"."EMP";
Select SUM(3) FROM "Test"."EMP";
why the output of these queries is?
5
10
15
And
I don't understand why they write table name like this "Test"."EMP"
your table has 5 records. the statement select 1 from test.emp returns 5 records with values as 1 for all 5 records.
id
1
1
1
1
1
This is because db engine simply returns 1 for each existing record without reading the contents of the cell. and same happens for select <any static value> from test.emp
same happens for 2 and 3
id
2
2
2
2
2
hence there are 5 records returned with the static values and sum of those values will be the product of static number passed in the select statement and total records in the table
additional fact: It is always recommended to perform count(1) than count(*) as it consumes less resource and hence less load on the server
I don't think it's "Test"."EMP" with double quotes.. it's probably `Test`.`EMP` with backticks instead. The definition means its database_name.table_name. This is the recommended format to get the correct table_name from database_name; in this case, you're specifically making the syntax to query from `Test`.`EMP`. Read more about identifier qualifiers.
As for SUM(x), the x get's repeated according to the rows present in the table. So SUM(1) on 5 rows is 1+1+1+1+1, SUM(2) on 5 rows is 2+2+2+2+2, and so on.

Get records from multiple tables, but only show 1 per ID?

First of all im sorry for the title, it's difficult to explain what I'm trying to achieve.
I have 2 tables, a table for property records, and a table for the images uploaded for each property.
In my listing_details table I enter 1 record per property that has a unique ID and property slug. I have a prop_gallery table where I can have hundreds of records that share the same property slug so I can relate it back to my my property.
I'm trying to write a query to pull the records from both tables, but I only want to show each property once, at the moment it's looping through all the records in the gallery and showing that property for as many records their are in the gallery. Hope this makes sense?
My query is...
$listings = $db->query('
SELECT *
FROM listing_details
JOIN prop_gallery
ON prop_gallery.prop_gallery_id = listing_details.prop_slug
WHERE (prop_slug LIKE prop_gallery_id OR prop_gallery_id LIKE prop_slug)
AND listing_details.prop_mandate = 1'
)->fetchAll();
If there's a property called Liams house then there will be a record for that in listing_details and if I've uploaded 10 pictures, there will be 10 records for that in prop_gallery.
When I loop through my results this means I'm now showing Liams house 10 times, when I want to show it just the once.
EDIT
Result of the above query
prop_id prop_agent prop_title prop_slug prop_mandate id prop_gallery_id prop_gallery
37 2 House in switzerland house-in-switzerland 1 4 6 main1.png
37 2 House in switzerland house-in-switzerland 1 4 6 main2.png
37 2 House in switzerland house-in-switzerland 1 4 6 main3.png
You can use the ROW_NUMBER() function. Assuming you have a [any] property in the table listting_details you can sort rows by you can do it cleanly; I assumed the property recorded_at.
For example:
SELECT *
FROM (
SELECT *,
row_number() over(partition by prop_slug order by recorded_at) as rn
FROM listing_details d
JOIN prop_gallery g
ON g.prop_gallery_id = l.prop_slug
WHERE prop_slug LIKE prop_gallery_id OR prop_gallery_id LIKE prop_slug
AND d.prop_mandate = 1
) x
where rn = 1

Need Help building a SQL query

I was wondering if anyone could help me with this, I have had a go at a number of things like using union queries but I think I am looking in the wrong place.
I am trying to run a SQL query on a singular table to provide to users with only datareader permissions to discover total sales.
The table itself contains sales and refunds which have a type of 1 or 3 in the database.
Table example structure:
Reference TransactionType StockCode Value
Sale01 1 Bat1 10
Sale01 1 Bat1 10
Sale09 1 Ball1 3
Sale15 1 Shin1 50
Sale16 1 Bat1 10
Refund06 3 Bat1 10
What I need to get is a total value for stock whereby the refund total is taken away from the total sum so that it appears as such:
StockCode TotalSales
Bat1 20
Ball1 3
Shin1 50
This means that I would have to have a calcuation under one column that says:
Select (if type is 1 then sum(value) - (if type is 3 then sum(value)
In honestly I simply can't do it! I have tried using temporary tables and such but it has completely stumped me.
Here you go
SELECT StockCode,
SUM( (CASE WHEN TransactionType = 3 THEN -1 ELSE 1 END) * Value)
FROM yourtable
GROUP BY StockCode

How to output the number of times data occurs using SQL?

Given the following data:
visit_id
1
1
1
2
3
3
4
5
is it possible using only sql (mysql's dialect actually, and no loops in another programming language) to output:
total visits number of visitor ids
1 3
2 1
3 1
i.e. to break down the data into the number of times they occur? So in the example above, there are 3 visit ids that only occur once (2,4,5), one visit id that occurs twice (3), and one that occurs three times (1).
thanks
Of course, it's called grouping.
select visit_id, count(visit_id) from visits group by visit_id
Building on FrantiĊĦek's answer
select acc.visitCount as total_visits,
count(acc.visitCount) as number_of_visitor_ids
from (
select visit_id,
count(visit_id) as visitCount
from visits
group by visit_id
) acc
group by acc.visitCount