SQL GROUP BY and differences on same field (for MS Access) - sql

Hi I have the following style of table under MS Access: (I didn't make the table and cant change it)
Date_r | Id_Person |Points |Position
25/05/2015 | 120 | 2000 | 1
25/05/2015 | 230 | 1500 | 2
25/05/2015 | 100 | 500 | 3
21/12/2015 | 120 | 2200 | 1
21/12/2015 | 230 | 2000 | 4
21/12/2015 | 100 | 200 | 20
what I am trying to do is to get a list of players (identified by Id_Person) ordered by the points difference between 2 dates.
So for example if I pick date1=25/05/2015 and date2=21/12/2015 I would get:
Id_Person |Points_Diff
230 | 500
120 | 200
100 |-300
I think I need to make something like
SELECT Id_Person , MAX(Points)-MIN(Points)
FROM Table
WHERE date_r = #25/05/2015# or date_r = #21/12/2015#
GROUP BY Id_Person
ORDER BY MAX(Points)-MIN(Points) DESC
But my problem is that i don't really want to order by (MAX(Points)-MIN(Points)) but rather by (points at date2 - points at date1) which can be different because points can decrease with the time.

One method is to use first and last However, this can sometimes produce strange results, so I think that conditional aggregation is best:
SELECT Id_Person,
(MAX(IIF(date_r = #25/05/2015#, Points, 0)) -
MIN(IIF(date_r = #21/05/2015#, Points, 0))
) as PointsDiff
FROM Table
WHERE date_r IN (#25/05/2015#, #21/12/2015#)
GROUP BY Id_Person
ORDER BY (MAX(IIF(date_r = #25/05/2015#, Points, 0)) -
MIN(IIF(date_r = #21/05/2015#, Points, 0))
) DESC;
Because you have two dates, this is more easily written as:
SELECT Id_Person,
SUM(IIF(date_r = #25/05/2015#, Points, -Points)) as PointsDiff
FROM Table
WHERE date_r IN (#25/05/2015#, #21/12/2015#)
GROUP BY Id_Person
ORDER BY SUM(IIF(date_r = #25/05/2015#, Points, -Points)) DESC;

Related

Oracle SQL - Selecting records into groups and filtering based on a comparison of row 1 + row 2

I've got a database that contains data on monitored manufacturing machines that has these fields within (and more) :
ID | WORK_ORDER_ID | WORK_CENTER_ID | MFGNO | ...
The records are realtime data and are entered in sequentially based on when work_order_id changes. I want to check between work orders if the MFGNO is the same but grouped based on the work_center_id.
For example:
1. 998 | 100 | 205 | TEST_MFG
2. 997 | 100 | 205 | TEST_ MFG
This would return true (or 1 row), as the mfgno's are the same.
Currently I'm able to do this for each work_center_id individually like this:
SELECT * FROM
(
select * FROM (select ID, WORKORDER_ID, TIMESTAMP, MFGNO from
HIST_ILLUM_RT where WORK_CENTER_ID = 5237
ORDER BY ID desc) where rownum = 1
)
where MFGNO = (
SELECT mfgno FROM
(
select * FROM (select ID, WORKORDER_ID, TIMESTAMP, MFGNO from
HIST_ILLUM_RT where WORK_CENTER_ID = 5237
ORDER BY ID desc
) where rownum < 3 order by id asc
) where rownum = 1
)
This produces either 0 rows if there are no current back to back MFGNO's, then 1> if there is.
This way I have to write this expression for each individual work_center_id (there's about 40). I want to have an expression that checks the top two rows of each grouped work_center_id and only returns a row if the MFGNO's match.
For example:
1. 998 | 101 | 205 | TEST_MFG
2. 997 | 098 | 206 | SomethingElse
3. 996 | 424 | 205 | TEST_MFG
4. 995 | 521 | 206 | NotAMatch
5. 994 | 123 | 205 | Doesn'tCompareThis
6. 993 | 664 | 195 | Irrelevant
For this it would only return 1, as only the work_center_id = 205 has a back to back (row 1&2) MFGNO, compared to 206 which doesn't for example.
I'm running Oracle 11g which seems to be limiting me, but I am unable to upgrade or find a work around to create this expression in this current version.
I think you want lag() and some logic:
select count(*)
from (select t.*,
lag(MFGNO) over (partition by WORK_CENTER_ID order by id) as prev_mfgno
from t
) t
where prev_mfgno = mfgno

How to search max value from group in sql

I am just learning some SQL, so I have a question.
-I have a table with name TABL
-a variable :ccname which has a value "Bottle"
The table is as follows:
+----------+---------+-------+--------+
| Name | Price | QTY | CODE |
+----------+---------+-------+--------+
| Rope | 3.6 | 35 | 236 |
| Chain | 2.8 | 15 | 237 |
| Paper | 1.6 | 45 | 124 |
| Bottle | 4.5 | 41 | 478 |
| Bottle | 1.8 | 12 | 123 |
| Computer | 1450.75 | 71 | 784 |
| Spoon | 0.7 | 10 | 412 |
| Bottle | 1.3 | 15 | 781 |
| Rope | 0.9 | 14 | 965 |
+----------+---------+-------+--------+
Now I want to find the CODE from the variable :ccname with the higher quantity! So I translated like this:
SELECT CODE
FROM TABL
GROUP BY :ccname
WHERE QTY=MAX(QTY)
In a perfect world that would turn as a result 478.
In the SQL world what should I write in order to get 478?
You probably want something like that:
SELECT code
FROM TABL
WHERE Name=:ccname
ORDER BY QTY DESC
LIMIT 1
The idea is we find all rows of the table whose Name column is the same as the contents of the variable :ccname, then order them by the quantity in descending order, and filally we select first one, which has to be the one with the largest quantity because they are sorted in descending order.
Try this
SELECT CODE
FROM TABLENAme
WHERE QTY = (SELECT MAX(QTY) FROM TablName WHERE Name = :ccname)
Use ORDER BY, a proper WHERE, and the something to limit the result set to one row:
SELECT CODE
FROM TABL
WHERE name = :ccname
ORDER BY QTY DESC
FETCH FIRST 1 ROW ONLY;
Note: Some databases spell the ANSI standard FETCH FIRST 1 ROW ONLY as LIMIT or as SELECT TOP 1.
Depending on your specific database, you can use one of the following options to restrict your result set to a single value after ordering your existing columns through an ORDER BY clause:
SELECT TOP 1
LIMIT 1
FETCH FIRST 1 ROW ONLY
Syntax Examples
SELECT TOP 1 Code
FROM TABL
WHERE Name = :ccname
ORDER BY QTY DESC
or
SELECT Code
FROM TABL
WHERE Name = :ccname
ORDER BY QTY DESC
LIMIT 1
or
SELECT CODE
FROM TABL
WHERE Name = :ccname
ORDER BY QTY DESC
FETCH FIRST 1 ROW ONLY;
Using join can also effectively solve the question:
Select t1.Code
From TABL As t1 Join (
Select Name, Max(table.QTY) as MaxQTY
From TABL
Where Name = :ccname
Group by Name
) As t2
Where t1.QTY = t2.MaxQTY And t1.Name = t2.Name
Explanation:
You first calculate the maximum value for "Bottle" using the subquery and then join the two tables to select corresponding row with MaxQTY and same name.

Select the difference of two consecutive columns

I have a table car that looks like this:
| mileage | carid |
------------------
| 30 | 1 |
| 50 | 1 |
| 100 | 1 |
| 0 | 2 |
| 70 | 2 |
I would like to get the average difference for each car. So for example for car 1 I would like to get ((50-30)+(100-50))/2 = 35. So I created the following query
SELECT AVG(diff),carid FROM (
SELECT (mileage-
(SELECT Max(mileage) FROM car Where mileage<mileage AND carid=carid GROUP BY carid))
AS diff,carid
FROM car GROUP BY carid)
But this doesn't work as I'm not able to use current row for the other column. And I'm quite clueless on how to actually solve this in a different way.
So how would I be able to obtain the value of the next row somehow?
The average difference is the maximum minus he minimum divided by one less than the count (you can do the arithmetic to convince yourself this is true).
Hence:
select carid,
( (max(mileage) - min(mileage)) / nullif(count(*) - 1, 0)) as avg_diff
from cars
group by carid;

Counting Just One Record Per Pupil Though Multiple Are Matched

I've set up a SQL Fiddle to illustrate the question...
I have a database of pupils (referenced by PupilId) who have assessments (AssessmentLevelId) recorded in various subjects (NCSubjectId) at various period (PeriodId).
Not every possible period may have an assessment in it.
PupilId | PeriodId | NCSubjectId | AssessmentLevelId
-----------------------------------------------------
100 | 1 | 10 | 1
100 | 3 | 10 | 2
200 | 1 | 10 | 1
300 | 1 | 10 | 1
400 | 1 | 10 | 1
100 | 5 | 10 | 2
300 | 7 | 10 | 2
100 | 15 | 10 | 2
I want to find the number of pupils who have a particular assessment level by a particular PeriodId.
So far I have this:
SELECT PupilId, COUNT(1) FROM NCAssessment
WHERE AssessmentLevelId = 2
AND NCSubjectId=10
AND PeriodId <= 10
GROUP BY PupilId
Which finds the pupil ids, but pupil 100 has a count of 2. I guess I need to wrap this in another query but am stumped. Any suggestions?
This is using Azure SQL.
Thanks.
If I understand your question correctly, I think this might be what you are looking for:
AssessmentLevelId = 2 has been removed from the query, because some Periods may not have an assessment.
SELECT AssessmentLevelID, PeriodID, COUNT(DISTINCT PupilID)
FROM NCAssessment
WHERE NCSubjectId=10 AND
PeriodId <= 10
GROUP BY AssessmentLevelID, PeriodID
If this isn't correct, could you please post a sample result you are expecting. Thanks!
If you want the number of distinct pupils that match, then use count(distinct):
SELECT COUNT(DISTINCT PupilId) as NumMatchingPupils, COUNT(*) as NumMatchingAssessments
FROM NCAssessment
WHERE AssessmentLevelId = 2 AND NCSubjectId = 10 AND PeriodId <= 10;
COUNT(DISTINCT) will count each pupil once, regardless of the number of maps. COUNT(*) or COUNT(1) will count the number of assessments that match.

MS-SQL get difference value

I have this query that calculates current gallons value from all fuel tanks in my database.
SELECT DISTINCT y.TankNumber as TankNumber
, y.Gallons as Gallons
, y.timeUpdated
, y.FuelType as FuelType
FROM (
SELECT TankNumber, max(timeUpdated) as maxdate
FROM someTable
GROUP BY TankNumber) as x
JOIN someTable y
ON x.TankNumber = y.TankNumber
AND x.maxdate = y.timeUpdated
ORDER BY y.TankNumber
Based on the fuel usage, data gets dumped in to my database automatically at any time. And query above will give me only the current gallons value in each fueltank:
TankNumber | Gallons | timeUpdated | FuelType
1 | 14 | 2012-10-22 04:16 | 89
2 | 8 | 2012-10-22 04:14 | 93
and etc..
My problem is, that I am trying to add another output value to my page, that will give me a difference how much fuel was used since last update. So it will look something like this:
TankNumber | Gallons | timeUpdated | FuelType | GallonsUsed
1 | 14 | 2012-10-22 04:16 | 89 | 5
2 | 8 | 2012-10-22 04:14 | 93 | -11
Unfortunately my SQL experience is not as solid for this type of problem and I have spent about two days trying to figure out or google something close. So, any help will be greatly appreciated.
Assuming you're using MS SQL 2005 or later, you can use the ROW_NUMBER function:
WITH cteOrderedUpdates As
(
SELECT
TankNumber,
Gallons,
TimeUpdated,
FuelType,
ROW_NUMBER() OVER
(
PARTITION BY
TankNumber
ORDER BY
TimeUpdated DESC
) As RowNumber
FROM
someTable
)
SELECT
x.TankNumber,
x.Gallons,
x.TimeUpdated,
x.FuelType,
x.Gallons - IsNull(y.Gallons, 0) As GallonsUsed
FROM
cteOrderedUpdates As x
LEFT JOIN cteOrderedUpdates As y
ON x.TankNumber = y.TankNumber
And x.RowNumber = y.RowNumber - 1
WHERE
x.RowNumber = 1
ORDER BY
x.TankNumber
;