I have a delta in specific records in same table - how do I find that specific row? - delta

I have a table A which contains list of counties for a given year along with other records.
Columns are 'Year' and 'CountyName'.
For 2020, there are 6434 'Counties' listed and for 2019, 6433 'Counties' are listed (in the same table).
How do I find the CountyName that is missing in 2019?
I can ofcourse run a simple query to get these records individually and then find out the missing county but I am trying to check if it can be done at the query level.
Thank you.

You can easily achieve it with the usage of the not in function or the exist one.
Something like:
Select countryname from table where year =2019 and countryname not in (Select countryname from table where year =2020)

Related

Tableau Target vs actual - data drop issue

I am having a serious issue in tableau when comparing the current month value to the current month target. I am currently using data blending for this purpose. But this issue wont solve even if I use tableau relationship joining. I have 3 join clauses. Such as branch ID, Staff ID and Month/year. Pls see the below
Ex - january Target - 95,000
Actual - 126,000
But when 3 joining clauses created, its dropping data to 111,900.
the reason is in january, even the allocated branch ID and month is matching, the other staff id clauses are dropping off. That means even though the table A has all branches , staff ID's and date key, the txn table is having only one staff ID maching for January. If the 3 matches are not satisfying , data is dropping itself. How to solve this isseue? I need to show the total value of 126,000 infront of 95,000. not 111,900.
Hope anyone can help.
Many thanks
This got achieved by a table unioning.
Seems like this could have been solved by changing the join type from an inner join to a right join.

How to select distinct combinations in T-SQL

I'm using SQL in Devexpress dashboard designer. I want to select distinct combinations of two parameters.
Perhaps Devexpress uses Transact-SQL but at the same time GROUP BY clause never works for me.
At the same time DISTINCT BY somehowe doesn't work as well.
Example:
There are two IDs 11 and 22
And there are two values of Date for 11, as an example: 21.01.2000 and 22.01.2000. And there's one for 22 as an example: 23.05.2008
Problem here is that I can't coose DISTINCT by date because there are many other IDs which have the same dates.
So I expect to have one distinct combination of ID and Date.
Does anyone faced with the same problem, can you advice any solution / code example?
Using select distinct will filter duplicates if you leave unique row properties out of the selected fields.
so:
Mike Smit
Mike Smit
Will be reduced to
Mike Smit
But if you're also asking for a PK like a Id field you get the following because id makes both rows distinct
1 Mike Smit
2 Mike Smit
Does this help?

How to select max date value while selecting max value

I have the following sample from a table with students results with date for a school entry exam
First student passed exam - This is the most common record found for most students
Second student failed 1st time entry and passed second time based on the date
3rd student had a failed input entry and was corrected based on the Version
I need the results to like like the picture above, so we take into regard using the latest date and highest version!
My basic query thus far is
select studentid
,examdate --(Date)
,result -- (charvar)
from StudentEntryExam
How should I approach this issue?
demo:db<>fiddle
SELECT DISTINCT ON (studentid)
*
FROM mytable
ORDER BY studentid, examdate DESC, version DESC
DISTINCT ON returns the first record of an ordered group. In that case the groups are the studentids. You must find the correct order to set the required record first. So, you need to order by studentid, of course. Then you need the most recent examdate first, which can be achieved with DESC order. If there are two records on the same date, you need to order the highest version first as well using the DESC modifier, too.

How to create MS Access data entry form with rolling 12 months for each row

I'm creating a data entry form in Access where sales people can select a product, then enter a quantity for that product in each of 12 columns representing the next twelve months. For a given project, there may be up to ten products, so each product will be on a separate row.
Tables:
Project - fields ProjectID, ProjectName, City, SalespersonID, StartDate
ProjectDetail - fields ProjectDetailID, ProjectID, ProductID, Date, Quantity
Product - fields ProductID, ProductName
Salesman - fields SalespersonID, SalespersonName
Currently I have the Project form with the simple project header information, and I want the ProjectDetail information to be a subform, of course. But - when I used a query to give me "buckets" of Quantity for Month0 (the current month), Month1, Month2, etc., I found that I can't enter data into the form because the fields are based on an expression.
What's the best way to handle this? Thanks in advance!
If each ProjectDetail entry will have multiple sets of Date and Quantity, it might be wise to pull those out into a separate table with a one-to-many relationship to ProjectDetailID, especially if you anticipate the number of date/quantity pairs could change from twelve. So, a ProjectDetailQuantity table would have fields ProjectDetailQuantityID, ProjectDetailID, Date, Quantity. On your form, you would have the Project main form, with a sub form to ProjectDetail and another subform to ProjectDetailQuantity.

Crystal Reports Filter by Most Recent Date of Field

I have a report I am creating through an ODBC connection. The report includes several invoices, where each invoice has several products. There is also a table which contains all the historical price changes for each product (field: unit-price). Currently there are duplicate product records being pulled, one for each time there was a price change. Therefore, I need to filter my data so that only the most recent unit-price is shown (date field: effective-date). How can I do this via the "Select Expert?"
In short, show the product's unit-price for the most recent effective-date.
Thank you!
You'll need to create a sql-expression field to get the most-recent effective date, then use this field in the record-selection formula.
// {%MAX_EFFECTIVE_DATE}
// most-likely you'll need to alias the table in the main report for this to work
(
SELECT Max(effective_date)
FROM price_history
WHERE product_id = price_history_alias.product_id
)
Record-selection formula:
{price_history_alias.effective_date}={%MAX_EFFECTIVE_DATE}
Instead of doing it in select expert. group by effective date and set the ordering as Descending.