Calculated Column Duplicate Values - ms-access

I am fairly new to access. I am trying to create a column that calculates duplicates values in a query. Taking in consideration that the duplicates value may change after other few filters taking action.
Supplier Spec SupplierDuplicate
A X 2
B Y 1
A Z 2
C X 1
I most appreciate your help!

I think you can run a SELECT query with DCount() function to get expected result.
SELECT Table1.Supplier, Table1.Spec, DCount("[Supplier]","[Table1]","[Supplier]='" & Table1.Supplier & "'") AS SupplierDuplicate FROM Table1;

Related

SumIIF Access Query

I am struggling to get the desired results i need using an Access query, and was wondering if what i was looking to do was actually achievable in one query, or whether i would need two queries and then export to Excel and interrogate the results there.
I've a table with a number of columns, i am specifically looking at three columns
Row Type - this will either be populated with A or U
Account Number - there will be potentially multiple instances of account number within the table. Although only once against row type "A", and multiple on row type "U"
Value - a currency field. At Account number level, the sum of "U" row, should equal the "A" value
I am looking to produce a query that will list three things.
[Account Number]
Sum of [Value] when [RowType] = "U"
[Value] when [RowType] = "A"
Would i need to create a new column in my table to generate a value for the requirement "Sum of Value when 'U')
I've tried
SUM(IIF([ROWTYPE]='U',[Value],0)) - but that doesn't seem to work.
I've also tried to use the builder within the Query to replicate the same, but again that also doesn't seem to work.
If all else fails i'm content to have to run two queries in Access and then join Excel, but i tihnk for my own learning and knowledge it would be good to know if what i am trying to do is possible.
I was hoping this is possible to compile in Access, but my knowledge of the application is seriously lacking, and despite looking on the MS Access support pages, and also some of the response on the StackOverflow forums, i still can't get my head around what i need to do.
Example of the data
Row Type
Account ID
Value
A
123456789
50.00
U
123456789
30.00
U
123456789
20.00
A
987654321
100.00
U
987654321
80.00
U
987654321
20.00
The data has been loaded into Access, table called "TEST"
This is the SQL i've got, but doesn't give me the desired results.
SELECT [TEST].[ROW TYPE], SUM([TEST].[VALUE]) AS [TEST].[ACCOUNT ID]
FROM [TEST]
GROUP BY [TEST].[ROW TYPE], [TEST].[ACCOUNTID]
When the query generates, would hope to see two rows, one for each account number.
Three row -
Account Number
Sum Value (where row is U)
Value (Where row is A)
I currently get 4 rows in the query. Two results for each account number, one result is the Value when Row Type = A, the other when Row Type = U.
I guess this is what you are after:
SELECT
[Account ID],
Sum(IIf([Row Type]="A",[Value],0)) AS A,
Sum(IIf([Row Type]="U",[Value],0)) AS U
FROM
TEST
GROUP BY
[Account ID];
Output:
Account ID
A
U
123456789
50,00
50,00
987654321
100,00
100,00

Need to find whether overlap is present in sql

I haev been stuck in a problem from quite some time and trying to figure out how to solve this using sql.
I have a table which has 3 columns :
LowerLimit UpperLimit Code
1 10 A
10.01 20 B
20.01 40 C
40.01 100 D
So in such case I need to check if there overlap present or not. The Upperlimit should not match with the LowerLimit of the next row and the permissible difference is only 0.01 . Is it possible to solve this using queries or do I need to iterate the whole range and find whether there is no overlap???
Any help is appreciated.
You can do this with exists to get the first row of overlap. For your specific logic:
select t.*
from t
where exists (select 1
from t t2
where t2.upperlimit >= t.lowerlimit and
t2.upperlimit < t.upperlimit + 0.01
);
If you want both rows, you can formulate this as a join or using a second exists to get the previous row.
I don't like your data representation. I would simple make the lower bound inclusive and the upper bound exclusive. Then the next lower bound could simply be the previous upper bound. You would not be able to use between but that is a bad idea anyway on numbers with decimal parts.

SQL query for operation between rows under some condition

Its little complicated query as it contains some conditions.
I have tables like this:
table DC - which contains one row for one northing-easting pair
Columns - Id Northing Easting
PossibleValues - Guid Std value Std value
table DCR - which contains multiple rows for each row in DC. Each row here corresponds to data on each pass on that exact location.
Columns - Id VibStatus DrivingDir CompactionValue UtcDate
PossibleValues - Guid 0/1 Forward/Reverse/Neutral +ve integers Timestamp
table DCMappings - which contains mapping between both tables.
DCId DCRId
The output I need should contain fields like this:
ResultTable
DCId DCRId Northing Easting VibStatus DrivingDir CompValue Position CompProgress
Here, Position is its position in chronological order when sorted by UtcDate grouped by DC.Id(See query at end to understand more).
And CompProgress has some conditions which is making it complicated.
CompProgress is percentage increase/decrease in CompValue compared to its previous row which was in same driving direction when arranged in ASC order of UtcDate(chronological) where the rows to be considered here should only be the ones with VibStatus set to ON(1) grouped by DCId's.
Each row in DC has multiple rows in DCR. So if row 1 in DC has 10 rows in DCR, the CompProgress should consider these 10 rows alone for calculation and then for row 2 in DC, etc...
I have written following query to extract needed fields except calculation of CompProgress. Please help me in this.
SELECT DC.Id, DCR.Id, Northing, Easting, VibStatus, DrivingDir, CompValue, ROW_NUMBER() OVER (PARTITION By dcm."DCId" ORDER BY dcr."UtcDate") as passNo
FROM "DCR" dcr LEFT JOIN "DCMappings" dcm ON "Id" = dcm."DCRId"
LEFT JOIN "DC" dc ON dc."Id" = dcm."DCId"
Need evaluation of CompProgress in this query.
Sorry for lot of text. But it was necessary to make others understand what is needed.

Can SQL query do this?

I have a table "audit" with a "description" column, a "record_id" column and a "record_date" column. I want to select only those records where the description matches one of two possible strings (say, LIKE "NEW%" OR LIKE "ARCH%") where the record_id in each of those two matches each other. I then need to calculate the difference in days between the record_date of each other.
For instance, my table may contain:
id description record_id record_date
1 New Sub 1000 04/14/13
2 Mod 1000 04/14/13
3 Archived 1000 04/15/13
4 New Sub 1001 04/13/13
I would want to select only rows 1 and 3 and then calculate the number of days between 4/15 and 4/14 to determine how long it took to go from New to Archived for that record (1000). Both a New and an Archived entry must be present for any record for it to be counted (I don't care about ones that haven't been archived). Does this make sense and is it possible to calculate this in a SQL query? I don't know much beyond basic SQL.
I am using MySQL Workbench to do this.
The following is untested, but it should work asuming that any given record_id can only show up once with "New Sub" and "Archived"
select n.id as new_id
,a.id as archive_id
,record_id
,n.record_date as new_date
,a.record_date as archive_date
,DateDiff(a.record_date, n.record_date) as days_between
from audit n
join audit a using(record_id)
where n.description = 'New Sub'
and a.description = 'Archieved';
I changed from OR to AND, because I thought you wanted only the nr of days between records that was actually archived.
My test was in SQL Server so the syntax might need to be tweaked slightly for your (especially the DATEDIFF function) but you can select from the same table twice, one side grabbing the 'new' and one grabbing the 'archived' then linking them by record_id...
SELECT
newsub.id,
newsub.description,
newsub.record_date,
arc.id,
arc.description,
arc.record_date,
DATEDIFF(day, newsub.record_date, arc.record_date) AS DaysBetween
FROM
foo1 arc
, foo1 newsub
WHERE
(newsub.description LIKE 'NEW%')
AND
(arc.description LIKE 'ARC%')
AND
(newsub.record_id = arc.record_id)

How to perform a complex range query in MySQL

I have a table (Table A) with a field of integers (Field B). For each row of Table A, I would like to construct a range of +/- 100 surrounding the integer value of Field B then find all values from Field B that are within these ranges. The query needs to be performed for all values in Field B. The query needs to return each row that is within each row range. Here is an example of what I am trying to do:
Table A
_______
A 1000
B 3000
C 5000
D 1090
Using the above Table A, the query would first find the ranges (+/- 100) for all integers in Field B.
900 - 1100
2900 - 3100
4900 - 5100
990 - 1190
The query would then iterate through these ranges and return rows from Table A that fall within the generated ranges. Using the above example, the query would return:
A 1000
A 1000
B 3000
C 5000
D 1090
D 1090
A and D are returned twice because it they fall within their own ranges. How can I construct a query that will return each row that falls between the range of each row? Thanks in advance for the help.
SELECT t2.*
FROM tableA AS t1
INNER JOIN tableA AS t2 ON t2.fieldB >= (t1.fieldB - 100) AND t2.fieldB <= (t1.fieldB + 100)
Shouldn't A also be shown twice, since it's also in D's range? (that's the case with above query - if incorrect, please elaborate why ^^)
Start with your inner-most pre-qualifier of every Table A record... Then re-join to table A again. I've added the qualifying Group ranges low and hi to show the qualifier basis you were looking for... In addition to D showing up twice, A should show up twice too as it qualifies the "D"s range too.
select
a2.ShowLetter,
a2.FieldB,
GrpRanges.RangeLow,
GrpRanges.RangeHi
from
( select distinct
a1.FieldB - 100 as RangeLow,
a1.FieldB + 100 as RangeHi
from
TableA a1 ) GrpRanges
JOIN TableA a2
on a2.FieldB between GrpRanges.RangeLow and GrpRanges.RangeHi
order by
a2.ShowLetter