Quantcast
Channel: ฟอรัม Getting started with SQL Server
Viewing all articles
Browse latest Browse all 8428

FInding whats not there

$
0
0

I tried asking this question a few weeks ago, and because of my own stupidity, was chastised by Joe Celko (deservedly) but when I finally did give a better explanation of the problem. The scenario is a group of cyclists that log their miles as they are completed. The logs are inserted to a mileagelog table:

Create table #mileagelog
(
    ride_id int ---PK
,  rider_id int --fK
,  ride_date DATETIME
 , distance_miles int
 , ride_time  int   (in minutes)
, century int --[1 century = 100  miles in bicycle slang. If the individual ride is 100 or more miles this is 1 if less then 0]
);

Data:

INSERT  INTO #mileagelog VALUES
(303,23,'2001-01-01 00:00:00.000',  60, 217,    0),
(304,23,'2001-01-02 00:00:00.000',   7, 24,     0),
(305,24,'2001-01-01 00:00:00.000',  25, 120,	0),
(306,18,'2001-01-04 00:00:00.000', 100,	455,	1),
(307,23,'2001-01-04 00:00:00.000', 100,	385,	1),
(308, 2,'2001-01-06 00:00:00.000', 24,	103,	0),
(309,24,'2001-02-10 00:00:00.000',  101,382,	1),
(310,23,'2001-02-05 00:00:00.000',   9,	 50,	0),
(311,25,'2001-02-06 00:00:00.000', 101,	463,	1),
(312,18,'2001-02-06 00:00:00.000', 100,	509,	1),
(313,24,'2001-02-16 00:00:00.000', 101, 382,	1);


Daily query

SELECT rider_id AS [Rider]
, distance_miles AS [Distance]
, ride_date
, elapsed_time
, century (1 or 0)
This query shows riders daily logs -
(if they rode over 100 miles then century = 1
if less 0)

Monthly query

SELECT riderid,
SUM(distance_miles),
SUM(century) as [centuries],
MONTH(ride_date)
GROUP BY RIDERID, MONTH(ride_date)
ORDER BY riderid, Month(ride_date)

This returns the total miles and centuries from one month.

What I don't know is how to get the yearly total  and whether or not they should or should  not get a 'goldstar'. To  pass they must have at least one century a month for all twelve months completed in each month. They cannot miss a month, and you have to be able to calculate as the year passes, not just at year end.

Hope this is clear enough, feel free to ask...

Thank you, Dave


Viewing all articles
Browse latest Browse all 8428

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>