Compare current and next record using Hive SQL Query - hive

My RFID tag file has huge amount of data and data was grouped by Date & Time value (every group has multiple tags) . I would like to know missing Tag # between 1st set and 2nd set of the data.
Please help me here…
Sample file:
field names: Tag # Date & Time
1st line -> 00045512|05-01-2013 12:02:03
2nd line -> 00052450|05-01-2013 12:02:03
Same file continued with different times but missing 1st line from above 2 set sorted by time..(below)
00052450|05-01-2013 13:02:03
Basically I would like to find missing tag when my 'Date & time" field changes.
This is similar problem solved in SQL...(link attached)
http://www.milesdennis.com/2011/06/comparing-current-and-previous-records.html

Use left outer join:
select s1.tag, case s2.tag when null then 1 else 0 end as missing_flag
from
set1 s1
left outer join set2 s2 on (s1.tag=s2.tag)

Related

Dynamic Selection Query with Criteria in Ms Access | SQL

I have a query in which I select multiple values. To create my form.
The results look like this :
the last Column [Candidat Nom] is a drop down list with an other query (lets call it Query 1) that select my drop down list values.
The selection is good and is what I'm looking for. Except I get the same value for all lines when they need to be different.
To simplify
let's take the following exemple
I have the following candidate that wants to join for the following job (Represented with their ID).
As we can see 2 candidates wants job N° 12. and 1 candidate for each other job.
What I get
All candidates are listed for every job.
What I want
What I actually did
is I put the following query (Query 1) on my column.
SELECT T_SALARIE.SALARIE_nom & " " & T_SALARIE.SALARIE_prenom AS Candidat Nom
FROM T_EMPLOI INNER JOIN (T_SALARIE INNER JOIN (T_SALARIE_EMPLOI LEFT JOIN T_STATUT_EMPLOI ON T_SALARIE_EMPLOI.SALARIE_EMPLOI_statut_id = T_STATUT_EMPLOI.STATUT_EMPLOI_id) ON T_SALARIE.SALARIE_NNI = T_SALARIE_EMPLOI.SALARIE_EMPLOI_salarie_nni) ON T_EMPLOI.EMPLOI_identifiant = T_SALARIE_EMPLOI.SALARIE_EMPLOI_emploi_identifiant
WHERE (((T_STATUT_EMPLOI.STATUT_EMPLOI_statut) Like "*valid*" Or (T_STATUT_EMPLOI.STATUT_EMPLOI_statut) Like "*décidé*") AND ((T_EMPLOI.EMPLOI_entreprise_id)=1));
This gave me the result I want but with the issue I mentioned previously (Same result for each line)
So
I thought I needed a new Criteria.
I added one, where It's going to select the candidate when the two "emploi ID" of my actual table (Shown before) and the one helping me select the candidates are equal.
With the following query:
SELECT T_SALARIE.SALARIE_nom & " " & T_SALARIE.SALARIE_prenom AS Candidat, T_SALARIE_EMPLOI.SALARIE_EMPLOI_emploi_identifiant
FROM T_EMPLOI INNER JOIN (T_SALARIE INNER JOIN (T_SALARIE_EMPLOI LEFT JOIN T_STATUT_EMPLOI ON T_SALARIE_EMPLOI.SALARIE_EMPLOI_statut_id = T_STATUT_EMPLOI.STATUT_EMPLOI_id) ON T_SALARIE.SALARIE_NNI = T_SALARIE_EMPLOI.SALARIE_EMPLOI_salarie_nni) ON T_EMPLOI.EMPLOI_identifiant = T_SALARIE_EMPLOI.SALARIE_EMPLOI_emploi_identifiant
WHERE (((T_STATUT_EMPLOI.STATUT_EMPLOI_statut) Like "*valid*" Or (T_STATUT_EMPLOI.STATUT_EMPLOI_statut) Like "*décidé*") AND ((T_SALARIE_EMPLOI.SALARIE_EMPLOI_emploi_identifiant)=[R_Select_COMOB]![ACTION_identifiant_emploi]));
But I keep on getting the following pop up that asks me to enter a Job ID
So how can I make the query for each line compare and select the right values?
I hope I was clear in explaining. If not please let me know so that I can add more details.
Thank you !
Thanks to your help and specially #June7 for his propositions, I found a solution regarding my problem :
I added a criteria to select values based on JobID that I wasn't selecting in the first hand.
And then based on the column (jobID) select the values needed
here is my final query :
SELECT
[SALARIE_nom] & " " & [SALARIE_prenom] & " (" & [SALARIE_NNI] & ")" AS Salarié, T_SALARIE_EMPLOI.SALARIE_EMPLOI_salarie_nni, T_SALARIE_EMPLOI.SALARIE_EMPLOI_id, T_SALARIE_EMPLOI.SALARIE_EMPLOI_emploi_identifiant
FROM
(T_STATUT_EMPLOI INNER JOIN T_SALARIE_EMPLOI ON T_STATUT_EMPLOI.STATUT_EMPLOI_id = T_SALARIE_EMPLOI.SALARIE_EMPLOI_statut_id) LEFT JOIN R_Select_Salarie ON T_SALARIE_EMPLOI.SALARIE_EMPLOI_salarie_nni = R_Select_Salarie.SALARIE_NNI
WHERE
(((T_SALARIE_EMPLOI.SALARIE_EMPLOI_emploi_identifiant)=[Formulaires]![F_COMOB]![ACTION_identifiant_emploi]) AND ((T_STATUT_EMPLOI.STATUT_EMPLOI_statut) Like "*validé*") AND ((T_SALARIE_EMPLOI.SALARIE_EMPLOI_Entreprise) Like "*RTE*"));
And Then to update my values for each line. I added a VBA code that requery on input.
Private Sub ACTION_Candidats_P_Enter()
ACTION_Candidats_P.Requery
End Sub
With that my problem is solved.

Find changes made in PGSQL table using SQL in Excel VBA

Is it possible to create an SQL query to compare a field within a single table to see if a change has been made and if possible list the before and after?
I have the following SQL query written in Excel 2010 VBA, which connects to an Oracle PostGreSQL database
Dim au As String
au = "SELECT id, priority, flag, code " _
& "FROM hist WHERE ( aud_dt >= '18/05/2020' AND aud_dt <='18/05/2020' ) " _
Set rs = conn.Execute(au)
With ActiveSheet.QueryTables.Add(Connection:=rs, Destination:=Range("A1"))
.Refresh
End With
Where fields include:
"priority" is the field that I'd like to check for changes which will
be a single number between 0-9
"code" is the record that has been
assigned the priority and is a mixture of numbers and letters up to 7
characters
"flag" shows a 1 as the active record, and 2 as an edited
record
"id" refers to the user account
I'd ideally like to end up with something like: id | priority | flag | priority_old | flag_old | code
Which should show the before and after changes to the priority. If the record shows priority=3 and flag=2 and code=Ab12, there must also be record with a 1 flag, as that is now the active record. If it has the same priority number for the code I'm not interested in it as that just means something else was changed instead as I have not listed all the column fields.
If the active record now shows priority=4, flag=1 and code=Ab12, that would be exactly the record I need to see.
Consider a self-join query (possibly you need to adjust date filter in WHERE depending on when items change):
SELECT h1.id, h1.priority, h1.flag,
h2.priority AS priority_old, h2.flag AS flag_old, h1.code
FROM hist h1
LEFT JOIN hist h2
ON h1.code = h2.code
AND h1.priority <> h2.priority
AND h1.flag = 1
AND h2.flag <> 1
WHERE (aud_dt >= '2020-05-18' AND aud_dt <='2020-05-18')

Over By Partition Issues

I am having some issues with Over Partition by. I am trying to get:
Desired Result
The inventory ordered column is the problem using this code:
select
l.whseloc,l.invtid,
l.qty,
case when f.MRPFlag= 0 then 'NON-Usable' else 'Usable' end as NetStatus,
SUM(p.qtyord-p.qtyrcvd) over (Partition by l.invtid) as InventoryOrdered,
SUM(l.qty*f.mrpflag)over(partition by l.invtid) as TotalNet
from location l (nolock)
join inventory i (nolock) on l.invtid=i.invtid
join loctable f (nolock) on l.whseloc=f.whseloc
left join [dbo].[opspurord] p (nolock) on l.InvtID=p.InvtID
What am I doing wrong?
First Try
There is not enough information (no sample data) in the question to tell you where you have gone wrong, so I will describe how to debug and fix the problem.
Display PARTITION BY columns and aggregated columns in your result set as separate columns, while keeping everything else the same.
select l.whseloc, l.invtid, l.qty,
case when f.MRPFlag= 0 then 'NON-Usable' else 'Usable' end as NetStatus,
SUM(p.qtyord-p.qtyrcvd) over (Partition by l.invtid) as InventoryOrdered,
p.qtyord-p.qtyrcvd AS InventoryOrderedRowSUM
SUM(l.qty*f.mrpflag)over(partition by l.invtid) as TotalNet,
l.qty*f.mrpflag AS TotalNetRowSUM
from location l (nolock)
...
ORDER BY l.invtid -- Add Order BY to make it easier to analyse data
l.invtid - this will show you groups of rows which summed by SUM function
InventoryOrderedRowSUM - shows you the value produced for each row. If you manually add up these values for a given l.invtid you should get the same result as InventoryOrdered.
Where to go from here:
By looking at the individual InventoryOrderedRowSUM you should be able to tell what combination of these values gives you the desired result. Once you have this worked out, you can adjust your InventoryOrdered function. If this is not obvious, then you need repeat the above process while adding p.qtyord and p.qtyrcvd to the result set.
I suspect that InventoryOrdered should just be p.qtyord-p.qtyrcvd (no SUM needed) based on the fact that 11200 is divisible by 1600.

MS Access-Return Record Below Current Record

I am very new to Access, and what I am trying to do seems like it should be very simple, but I can't seem to get it.
I am a structural engineer by trade and am making a database to design buildings.
My Diaphragm Analysis Table includes the fields "Floor_Name", "Story_Number", "Wall_Left", and "Wall_Right". I want to write a new query that looks in another query called "Shear_Wall_incremental_Deflection" and pulls information from it based on input from Diaphragm Analysis. I want to take the value in "Wall_Right" (SW01), find the corresponding value in "Shear_Wall_incremental_Deflection", and report the "Elastic_Deflection" corresponding to the "Story_Below" instead of the "Story_Number" in the Diaphragm Analysis Table. In the case where "Story_Number" = 1, "Story_Below" will be 0 and I want the output to be 0.
Same procedure for "Wall_Left", but I'm just taking it one step at a time.
It seems that I need to use a "DLookup" in the expression builder with TWO criteria, one that Wall_Right = Shear_Wall and one that Story_Number = Story_Below, but when I try this I just get errors.
"Shear_Wall_incremental_Deflection" includes shearwalls for all three stories, i.e. it starts at SW01 and goes through SWW for Story Number 3 and then starts again at SW01 for Story Number 2, and so on until Story Number 1. I only show a part of the query results in the image, but rest assured, there are "Elastic_Deflection" values for story numbers below 3.
Here is my attempt in the Expression Builder:
Right_Defl_in: IIf(IsNull([Diaphragm_Analysis]![Wall_Right]),0,DLookUp("[Elastic_Deflection_in]","[Shear_Wall_incremental_Deflection]","[Shear_Wall_incremental_Deflection]![Story_Below]=" & [Diaphragm_Analysis]![Story_Number]))
I know my join from Diaphragm_Analysis "Wall_Left" and "Wall_Right" must include all records from Diaphragm_Analysis and only those from "Shear_Wall_incremental_Deflection"![Shear_Walls] where the joined fields are equal, but that's about all I know.
Please let me know if I need to include more information or send out the database file.
Thanks for your help.
Diaphragm Analysis (Input Table)
Shear_Wall_incremental_Deflection (Partial Image of Query)
I think what you are missing is that you can and should join to Diaphragm_Analysis twice, first time to get the Story_Below value and second to use it to get the corresponding Elastic_Deflection value.
To handle the special case where Story_Below is zero, I would write a separate query (only requires one join this time) and 'OR together' the two queries using the UNION set operation (note the following SQL is untested):
SELECT swid.Floor_Name,
swid.Story_Number,
swid.Wall_Left,
da2.Elastic_Deflection AS Story_Below_Elastic_Deflection
FROM ( Shear_Wall_incremental_Deflection swid
INNER JOIN Diaphragm_Analysis da1
ON da1.ShearWall = swid.Wall_Left )
INNER JOIN Diaphragm_Analysis da2
ON da2.ShearWall = swid.Wall_Left
AND da2.Story_Number = da1.Story_Below
UNION
SELECT swid.Floor_Name,
swid.Story_Number,
swid.Wall_Left,
0 AS Story_Below_Elastic_Deflection
FROM Shear_Wall_incremental_Deflection swid
INNER JOIN Diaphragm_Analysis da1
ON da1.ShearWall = swid.Wall_Left
WHERE da1.Story_Below = 0;
I've assumed that there is no data where Story_Number is zero.

Multiple Table Access Query

I am trying to combine multiple tables in Microsoft Access to return a LIKE wildcard value. However, I want it done by a list of things, not just a specific item. I have the below table labled "SomeData"
ID Fund TextFld Type1
2 Sewer COMP - DZ Docs
3 Sewer SEW: ZO Docs
I have a second table labeled GL where in I am looking up on TextFld: For instance there might be:
COMP - ABC
COMP - DEF
SEW: GHS
SEW: XYZ
I put into SQL:
SELECT GL.Text, SomeData.Type1
FROM SomeData INNER JOIN GL ON SomeData.Fund = GL.Type
WHERE GL.Text LIKE SomeData.TextFld;
However, this is only returning exact matches. How can I do this so that any similar match will be found.
Please note that there will be many lines of these, so I cannot just use anything such as LIKE "COMP*" OR "SEW*"
Thank you very much in advance. Any help would be greatly appreciated.
You don't have the wildcard in your query. The wildcard in MS Access is the asterisk so your query should look like this:
SELECT GL.Text, SomeData.Type1
FROM SomeData INNER JOIN GL ON SomeData.Fund = GL.Type
WHERE GL.Text LIKE '*' & SomeData.TextFld & '*';
I haven't ever used a LIKE statement while using a field from another table but I'm fairly certain that syntax is correct.
Note: In this query you are getting any records where the COMP or SEW exists anywhere in the field. If you want it to show only when it is found at the start of the field it would be:
WHERE GL.Text LIKE & SomeData.TextFld & '*';
And likewise if you want it to only match where it is found at the end:
WHERE GL.Text LIKE '*' & SomeData.TextFld;
You should concatenate wild character at the end (or beginning, or both) of the field TextFld
WHERE GL.Text LIKE '%' + SomeData.TextFld + '%'