Conditional counting of consecutive rows - ms-access

For a given current client i am trying to find how many consecutive years they have renewed a policy with us. My thinking on how to do this is to match a field in the current row with the previous row. I'm trying to write a function for this but if there is an easier way please let me know. Here is what i have for the function
Option Compare Database
'Renewal Count Returns count of consecutive renewals
Public Function RenewCount(strLocationID As Integer, _
strQuoteID As Integer, _
strOriginalQuoteID As Variant) As String
Static strLastLocationID As Integer
Static strLastQuoteID As Integer
Static strCount As Integer
If strLocationID = strLastLocationID And strOriginalQuoteID = strLastQuoteID Then
strCount = strCount + 1
Else
strLastLocationID = strLocationID
strLastQuoteID = strQuoteID
strCount = 0
End If
RenewCount = strCount
End Function
Here is a little sample of the data
LocationID QuoteID OriginalQuoteID
2 1094117
2 1125718 1094117
2 1148296 1125718
2 1176466 1148296
5 1031892
5 1044976 1031892
5 1059216 1044976
5 1077463 1059216
There are also dates for each policy that i can manipulate as well.
My idea would be to have the following and just find the max of the last column for each location.
LocationID QuoteID OriginalQuoteID Renewal_Count
2 1125718 1094117 0
2 1148296 1125718 1
2 1176466 1148296 2
5 1031892 0
5 1044976 1031892 1
5 1059216 1044976 2
5 1077463 1059216 3
5 1098124 1077463 4
5 1100215 0
5 1198714 1100215 1
5 1254125 1198714 2
Any help would be appreciated. Thanks
I forgot to mention that this has been sorted on the location ID and that OriginalQuoteID will be null for any New policy. When i try to run the function i get #num! in the column for a majority of the rows. What i want is for a given QuoteID the number of consecutive renewals there have been. So for the above for locationID 5 QuoteID 1254125 there have been 2 Renewals.

This might work (appears to be with my tests) - create a cartesian product on itself, link the QuoteID to the OriginaQuoteID and count.
SELECT T1.LocationID,
T1.QuoteID,
T1.OriginalQuoteID
FROM Table1 T1, Table1 T2
WHERE T1.QuoteID = T2.OriginalQuoteID
to get the count per LocationID (add 1 to count the Null field):
SELECT COUNT(*)+1
FROM Table1 T1, Table1 T2
WHERE T1.QuoteID = T2.OriginalQuoteID
GROUP BY T1.LocationID
or if you prefer joins:
SELECT COUNT(*)+1
FROM Table1 T1 INNER JOIN Table1 T2 ON T1.QuoteID = T2.OriginalQuoteID
GROUP BY T1.LocationID

Related

How to find the first available value in a table in MS Access

In SQL Server 2008 I find the first available slot in a table with the following stored procedure.
If Exists ( Select * From Methods Where MethodSerno =1 )
Select #SlotCode = Min(MethodSerno) + 1
From Methods
Where MethodSerno + 1 Not In ( Select MethodSerno From Methods )
Is there an equivalent way in MS Access?
Thanks in adavance...
For a table named [Methods] containing rows
MethodSerno
-----------
1
2
4
5
8
10
we can start with a query to find each value that does not have an immediate successor (n+1)
SELECT t1.MethodSerno
FROM
Methods t1
LEFT JOIN
Methods t2
ON t1.MethodSerno + 1 = t2.MethodSerno
WHERE t2.MethodSerno IS NULL
returning
MethodSerno
-----------
2
5
8
10
The "first available" value will simply be (the smallest of those values)+1
SELECT MIN(t1.MethodSerno) + 1 AS NextSerno
FROM
Methods t1
LEFT JOIN
Methods t2
ON t1.MethodSerno + 1 = t2.MethodSerno
WHERE t2.MethodSerno IS NULL
returning
NextSerno
---------
3

mySQL count occurances of value on multiple fields. How?

I have a table with 5 fields. Each field can store a number from 1 - 59.
Similar to countif in Excel, how do I count the number of times a number from 1 - 59 shows up in all 5 fields?
Here's an example for the count of occurances for the number 1 in all five fields:
SELECT SUM(pick_1 = 1 OR pick_2 = 1 OR pick_3 = 1 OR pick_4 = 1 OR pick_5 = 1) AS total_count_1
FROM tbldraw
Hopefully I made sense.
There was an answer here that had a solution. I think this is just a variation.
Step1: Create a numbers table (1 field, called id, 59 records (values 1 -59))
Step2:
SELECT numbers_table.number as number
, COUNT(tbldraw.pk_record)
FROM numbers_table
LEFT JOIN tbldraw
ON numbers_table.number = tbldraw.pick_1
OR numbers_table.number = tbldraw.pick_2
OR numbers_table.number = tbldraw.pick_3
OR numbers_table.number = tbldraw.pick_4
OR numbers_table.number = tbldraw.pick_5
GROUP BY number
ORDER BY number
How about a two step process? Assuming a table called summary_table ( int id, int ttl), for each number you care about...
insert into summary_table values (1,
(select count(*)
from table
where field1 = 1 or field2 = 1 or field3 = 1 or field4 = 1 or field5 = 1))
do that 59 times, once for each value. You can use a loop in most cases. Then you can select from the summary_table
select *
from summary_table
order by id
That will do it. I leave the coversion of this SQL into a stored procedure for those that know what database is in use.
The ALL() function, which returns true if the preceding operator is true for all parameters, makes the query particularly elegant and succinct.
To find the count a particular number (eg 3):
select count(*)
from tbldraw
where 3 = all (pick_1, pick_2, pick_3, pick_4, pick_5)
To find the count of all such numbers:
select pick_1, count(*)
from tbldraw
where pick_1 = all (pick_2, pick_3, pick_4, pick_5)
group by pick_1

Update access column from existing values

I have a MS access table with the following columns and sample records.
How do I update the adDate value with the least LastSaleDate for each ProductID.
orderID productID lastsaleDate adDate
1 1 10/20/2012
2 1 5/10/2007
3 1 4/1/2004
4 1 20/11/2011
5 2 10/10/2010
6 2 12/10/1972
For example the adDate for ProductID 1 will be 4/1/2004
and for ProductID 2 will be 12/10/1972
You can use DMin:
UPDATE sales
SET sales.adDate = DMin("lastsaleDate","sales","productID=" & productid)
Unless you have a compelling reason to actually store adDate values in your table, consider simply computing adDate with a query any time you need it. That way you don't risk ever displaying adDate values which have not been updated to reflect the latest changes to the underlying data.
SELECT
y.orderID,
y.productID,
y.lastsaleDate,
sub.adDate
FROM
YourTable AS y
INNER JOIN
(
SELECT productID, Min(lastsaleDate) AS adDate
FROM YourTable
GROUP BY productID
) AS sub
ON y.productID = sub.productID;

MySQL String Comparison with Percent Output (Position Very Important

I am trying to compare two entries of 6 numbers, each number which can either can be zero or 1 (i.e 100001 or 011101). If 3 out of 6 match, I want the output to be .5. If 2 out of 6 match, i want the output to be .33 etc.
Note that position matters. A match only occurs when both entries have a 1 in the first position, both have a 0 in the second position etc.
Here are the SQL commands to create the table
CREATE TABLE sim
(sim_key int,
string int);
INSERT INTO sim (sim_key, string)
VALUES (1, 111000);
INSERT INTO sim (sim_key, string)
VALUES (2, 101101);
My desired output to compare the two strings, which share 50% of the characters, and output 50%.
Is it possible to do this sort of comparison in SQL? Thanks in advance
Have a look at this example.
CREATE TABLE sim (sim_key int, string int);
INSERT INTO sim (sim_key, string) VALUES (1, 111000);
INSERT INTO sim (sim_key, string) VALUES (2, 101101);
select a.string A, b.string B,
sum(case when Substring(A.string,Pos,1) = Substring(B.string,Pos,1) then 1 else 0 end) Matches,
count(*) as RowCount,
(sum(case when Substring(A.string,Pos,1) = Substring(B.string,Pos,1) then 1 else 0 end) /
count(*) * 100.0) as PercentMatch
from sim A
cross join sim B
inner join (
select 1 Pos union all select 2 union all select 3
union all select 4 union all select 5 union all select 6) P
on P.Pos between 1 and length(A.string)
where A.sim_key= 1 and B.sim_key = 2
group by a.string, b.string
It is crude and probably included more than required but shows how it can be done. It is better to create a numbers table with just numbers from 1 to 1000 or so, that can be used repeatedly in many queries where a number sequence is required. Such a table will replace the (select .. union virtual table used in the inner join)
Instead of keeping 10010101 as integer convert this binary version to true integer when compare use bit logic AND, result convert to binary and count '1' to how many match...
for convert: http://dev.mysql.com/doc/refman/5.5/en/binary-varbinary.html
for compare: http://dev.mysql.com/doc/refman/5.5/en/bit-functions.html bitwise AND
...

MySQL - What's wrong with the query?

I am trying to query a database to find the following.
If a customer searches for a hotel in a city between dates A and B, find and return the hotels in which rooms are free between the two dates.
There will be more than one room in each room type (i.e. 5 Rooms in type A, 10 rooms in Type B, etc.) and we have to query the database to find only those hotels in which there is at least one room free in at least one type.
This is my table structure:
**Structure for table 'reservations'**
reservation_id
hotel_id
room_id
customer_id
payment_id
no_of_rooms
check_in_date
check_out_date
reservation_date
**Structure for table 'hotels'**
hotel_id
hotel_name
hotel_description
hotel_address
hotel_location
hotel_country
hotel_city
hotel_type
hotel_stars
hotel_image
hotel_deleted
**Structure for table 'rooms'**
room_id
hotel_id
room_name
max_persons
total_rooms
room_price
room_image
agent_commision
room_facilities
service_tax
vat
city_tax
room_description
room_deleted
And this is my query:
$city_search = '15';
$check_in_date = '29-03-2010';
$check_out_date = '31-03-2010';
$dateFormat_check_in = "DATE_FORMAT('$reservations.check_in_date','%d-%m-%Y')";
$dateFormat_check_out = "DATE_FORMAT('$reservations.check_out_date','%d-%m-%Y')";
$dateCheck = "$dateFormat_check_in >= '$check_in_date' AND $dateFormat_check_out <= '$check_out_date'";
$query = "SELECT $rooms.room_id,
$rooms.room_name,
$rooms.max_persons,
$rooms.room_price,
$hotels.hotel_id,
$hotels.hotel_name,
$hotels.hotel_stars,
$hotels.hotel_type
FROM $hotels,$rooms,$reservations
WHERE $hotels.hotel_city = '$city_search'
AND $hotels.hotel_id = $rooms.hotel_id
AND $hotels.hotel_deleted = '0'
AND $rooms.room_deleted = '0'
AND $rooms.total_rooms - (SELECT SUM($reservations.no_of_rooms) as tot
FROM $reservations
WHERE $dateCheck
GROUP BY $reservations.room_id) > '0'";
The number of rooms already reserved in each room type in each hotel will be stored in the reservations table.
The thing is the query doesn't return any result at all. Even though it should if I calculate it myself manually.
I tried running the sub-query alone and I don't get any result. And I have lost quite some amount of hair trying to de-bug this query from yesterday. What's wrong with this? Or is there a better way to do what I mentioned above?
Edit: Code edited to remove a bug. Thanks to Mark Byers.
Sample Data in reservation table
1 1 1 2 1 3 2010-03-29 2010-03-31 2010-03-17
2 1 2 3 3 8 2010-03-29 2010-03-31 2010-03-18
5 1 1 5 5 4 2010-03-29 2010-03-31 2010-03-12
The sub-query should return
Room ID : 1 Rooms Booked : 7
Room ID : 2 Rooms Booked : 8
But it does not return any value at all.... If i remove the dateCheck condition it returns
Room ID : 2 Rooms Booked : 8
Your problem is here:
$rooms.total_rooms - (SELECT SUM($reservations.no_of_rooms) as tot,
$rooms.room_id as id
FROM $reservations,$rooms
WHERE $dateCheck
GROUP BY $reservations.room_id) > '0'"
You are doing a subtraction total_rooms - (tot, id) where the first operand is a scalar value and the second is a table with two columns. Remove one of the columns in the result set and make sure you only return only one row.
You also should use the JOIN keyword to make joins instead of separating the tables with commas. That way you won't forget to add the join condition.
You probably want something along these lines:
SELECT column1, column2, etc...
FROM $hotels
JOIN $rooms
ON $hotels.hotel_id = $rooms.hotel_id
JOIN (
SELECT SUM($reservations.no_of_rooms) as tot,
$rooms.room_id as id
FROM $reservations
JOIN $rooms
ON ??? /* Aren't you missing something here? */
WHERE $dateCheck
GROUP BY $reservations.room_id
) AS T1
ON T1.id = room_id
WHERE $hotels.hotel_city = '$city_search'
AND $hotels.hotel_deleted = '0'
AND $rooms.room_deleted = '0'
AND $rooms.total_rooms - T1.tot > '0'