SQL: Query new rows of a new date - sql

i have table as shown here in this picture --> http://www.directupload.net/file/d/3710/lj7etq5j_png.htm
I need the correct Query to get only data_id 10.
The query should be like this: Compare the latest date rows (2014-08-08) with the earliest date rows (2014-08-06). If there is a row on 2014-08-08 which is NOT at 2014-08-06, this row should returned.
I already tried it with self-joins and Sub-Selects, but i did't get it work.
Thx for any help!

Maybe something like this is what you're looking for?
select * from Table1
where
data not in (
select data from Table1
where dataOfDate = (select min(dataofdate) from Table1)
)
and dataOfDate = (select max(dataofdate) from Table1)
The first where clause compares the data field of the returned rows to the data field in the set of oldest rows and the second where clause limits the set of rows the the newest.
Note that I'm only comparing rows based on thedatafield, so you might have to change the query if you want to includenextTableIdin the comparison.
Here is a sample SQL Fiddle.

How about something like this:
SELECT d1.* FROM Dates d1 LEFT JOIN Dates d2 ON d1.nextTableId = d2.nextTableId WHERE d1.dataofDate = '2014-08-08' AND d2.dataofDate = '2014-08-06' AND d2.data_id IS NULL;

SELECT `data_id`
FROM `my_table`
WHERE `dataOfDate` = (SELECT MAX(`dataOfDate`) FROM `my_table`)
AND `nextTableId` NOT IN (
SELECT `nextTableId` FROM `my_table` WHERE `dataOfDate` = (SELECT MIN(`dataOfDate`) FROM `my_table`)
)
select all rows with max date
that don't have values amongst rows with min date
edit ops, too late

Related

Need help in sql Query.neo

In this table there are three colum and in need the value for of data which are lesser than code = 28,this is my query
SELECT value,code,date
FROM table
order by date,vchcode
but when i ad where clouse like
SELECT value,code,date
FROM table
where code < 28
order by date,vchcode
is only shows 2 row with code 26 and 27... i need 26,27 and 32.. and table colums are variable its not fix..
I think you wnat to take the date into account -- what you really want are all rows before the date of the row with code 28.
One method uses a subquery:
SELECT t.value, t.code, t.date
FROM table t
WHERE date < (SELECT date FROM table t2 WHERE t2.code = 28)
ORDER BY t.date, t.vchcode

SQL: How to join two rows on the same table, based on the same timestamp?

This is a data output table in postgreSQL.
I am looking for a way to join rows 7 and 8 on the timestamp condition to remove the [null] values.
This is the expected output I am looking for.
So that I will only have one row when the timestamp matches.
--------------------------------------------------------
05:32:33 | Pump2Stop | 49
--------------------------------------------------------
Any idea how can this be done?
assuming you only ever have one record per timestamp with a non-null value in a specific column, try this:
SELECT to_timestamp, MAX(str_v), MAX(long_v)
FROM table
GROUP BY to_timestamp;
You can use a self-join
select stp.to_timestamp,
stp.str_v,
strt.long_v
from the_table stp
join the_table strt
on stp.to_timestamp = strt.to_timestamp
and strt.str_v is null
where stp.str_v = 'Pump2Stop'
If I understand correctly, you want all rows -- with the specified rows combined. Based on your comment, I think this does what you want:
select t1.to_timestamp, t1.str_v
coalesce(t2.long_v, t1.long_v)
from t t1 left join
t t2
on t.to_timestamp = t2.to_timestamp and
t.str_v in ('Pump2Stop', 'Pump2Stop')
where t1.str_v is not null;
You can also use a window function:
select t.to_timestamp, t.str_v,
coalesce(long_v, imputed_long_v) as long_v
from (select t.*,
max(long_v) over (partition by to_timestamp) as imputed_long_v
from t
) t
where str_v is not null;

Get top 1 row for every ID

There is a few posts about it but i can't make it work...
I just want to select just one row per ID, something like row_number() over Partition in oracle but in access.
ty
SELECT a.*
FROM DATA as a
WHERE a.a_sku = (SELECT top 1 b.a_sku
FROM DATA as b
WHERE a.a_sku = b.a_sku)
but i get the same table Data out of it
Sample of table DATA
https://ibb.co/X4492fY
You should try below query -
SELECT a.*
FROM DATA as a
WHERE a.Active = (SELECT b.Active
FROM DATA as b
WHERE a.a_sku = b.a_sku
AND a.Active < b.Active)
If you don't care which record within each group of records with a matching a_sku values is returned, you can use the First or Last functions, e.g.:
select t.a_sku, first(t.field2), first(t.field3), ..., first(t.fieldN)
from data t
group by t.a_sku

Comparing date with the Result Set in Oracle

Below is my query which is going to Generate two rows for me after execution of the query.
SELECT *FROM ELP_COUNTRY,ELP_COUNTRY_Tax where
ELP_COUNTRY.COUNTRY_ID=Elp_Country_Tax.Country_Id
and ELP_COUNTRY.DESCRIPTION='Brasil' and GETENGLISHDESCFROMLISTITEM(ELP_COUNTRY_TAX.TAX_TYPE_ID)='Premiums'
and ELP_COUNTRY_TAX.DEALER_ID is null
This query is returning two rows and again from that rows there is a date named effective date. so again from those rows I want to get the Highest date.
Can anyone help me on this. I am new to Oracle
You could self-join the table with the effective date and evaluate against that by adding something like this: (note the joins may not be correct since I'm just guessing what table the effective date is in and how the tables are related)
and elp_country.effective_date = (select max(e2.effective_date) from elp_country e2 where e2.country_id = elp_country.country_id and elp_country.description = 'Brasil')
Or if it's just a grouping thing you're trying to do then your select would be something like this:
SELECT max(effective_date), column2, column3
FROM ELP_COUNTRY,
ELP_COUNTRY_Tax
where ELP_COUNTRY.COUNTRY_ID=Elp_Country_Tax.Country_Id
and ELP_COUNTRY.DESCRIPTION='Brasil' and
GETENGLISHDESCFROMLISTITEM(ELP_COUNTRY_TAX.TAX_TYPE_ID)='Premiums'
and ELP_COUNTRY_TAX.DEALER_ID is null
group by column2, column3;
You could use the rank function, and wrap that with a "select *" and a where clause predicate filtering the rank of 1, like so:
SELECT * FROM (
SELECT
c.*,
ct.*,
rank() over (order by ct.effective_date) as eff_date_rnk
FROM
ELP_COUNTRY c,
ELP_COUNTRY_Tax ct
WHERE
ELP_COUNTRY.COUNTRY_ID = Elp_Country_Tax.Country_Id
and ELP_COUNTRY.DESCRIPTION='Brasil'
and GETENGLISHDESCFROMLISTITEM(ELP_COUNTRY_TAX.TAX_TYPE_ID)='Premiums'
and ELP_COUNTRY_TAX.DEALER_ID is null
) t
WHERE t.eff_date_rnk = 1;

SQL Server 2012 - updating a column based on row to row comparison

I have a table that contains dates and times. For example columns are Date, ExTime, NewTime, Status. I am ordering them based on a expkey column that makes them show in the right order.
I want to do a row by row comparison and compare the second row column of extime to the first row column NewTime. If extime < Newtime then I want to update status with a "1". And then traverse through the table row by row where second row in the above example becomes the first and a new second is pull and used. Here is a sample of what I have now - but it is not hitting and working all all of the rows for some reason.
UPDATE t
SET t.Status = 1
FROM MyTable t
CROSS APPLY (SELECT TOP 1 NewTime
FROM MyTable
WHERE ID = t.ID AND [Date] = t.[Date]
ORDER BY ExpKey) t1
WHERE t.Extime < t1.NewTime
This is not hitting all the rows like I want it to. I have the where clause comparing fields ID and Date to insure that the rows are attached to the same person. If the ID or Dates are not the same it is not attached to the same person so I would not want to update the status. So basically if the ID of Row 2 = ID of Row 1 and Date of Row 2 = Date of Row 1 I want to compare extime of row 2 and see if it is less than newtime of Row 1 - if so then update the status field of row 2.
Any help in figuring out why this sort of works but not on all would be appreciated.
Ad.
On SQL Server 2012 you can easily update status with window function lag():
with cte as (
select
extime,
lag(newtime) over(partition by id, date order by expKey) as newtime,
status
from table1
)
update cte set status = 1 where extime < newtime;
sql fiddle demo
I haven't tested this, but I've dealt with similar issues of comparing adjacent rows. I put this together off-the-cuff, so it may need tweaking, but give it a try.
;WITH CTE AS
( SELECT ID,[Date],ExpKey,ExTime,NewTime,
ROW_NUMBER()OVER(PARTITION BY ID,[Date] ORDER BY ExpKey) AS Sort
FROM MyTable
)
UPDATE row2
SET row2.[Status] = 2
WHERE row2.ExTime < row1.NewTime
FROM CTE row2
CROSS JOIN CTE row1
ON row1.ID = row2.ID
AND row1.[Date] = row2.[Date]
AND row1.Sort = row2.Sort-1 --Join to prior row