I have data based on column id and date...Data Type for date is DATETIME
DataBase : sql server 2008
Query 1:
Select *
from table t1
where (t1.date between '2016-05-11' and '2016-05-13')
Query 2:
select *
from table t1
where t1.date IN ('2016-05-11')
Result is NUll even i have records on this date
So is there any Alternative to fetch records except (<=/</>/>=) greater than or less than
You can use below query but your query is also correct and its running on my server.
Select * from table t1
where t1.date between '20160511 00:00:00' and '20160513 23:59:59';
select * from table t1 where cast(t1.date as date) IN ('20160511');
Try this,
Select *
from table t1
where cast(t1.date as date) between '2016-05-11' and '2016-05-13'
ie Convert your datetime column to date and compare, because your parameters are only date values.
or
where cast(t1.date as date) in ('2016-05-11')--For your second query
or use greater than or less than
Select *
from table t1
where t1.date >= '2016-05-11' and t1.date < '2016-05-14'
Related
I use the below Openquery statement to connect to the linked server and then I want to fetch those records to my local server. The problem is that I want to change the date every day in the where statement below, so I want to make an Openquery by putting the system date as a variable, but an error occurs. Please help me.
INSERT INTO [A-SERVER\SQLEXPRESS].[ASQL].[dbo].[ATB]
SELECT * FROM OPENQUERY ([LINKB], 'SELECT T.DATE, T.ID, O.DES,S.PR,SUM(T.TQ), P.T1, P.T2,
P.T3, P.T4, SUM(T.TL)
FROM [BSQL].[dbo].[BDL] T
JOIN [BSQL].[dbo].[OBT] O ON O.ID = T.ID
JOIN [BSQL].[dbo].[POT] P ON P.ID = T.ID
JOIN [BSQL].[dbo].[ICE] S ON S.ID = T.ID
WHERE CAST(T.DATE AS DATE) = ''2022-07-28'' AND T.TOL = ''3''
GROUP BY T.DATE, T.ID, O.DES, S.PR, P.T1, P.T2,P.T3, P.T4 ');
GO
use GETDATE() cast it as Date
Returns the current a DateTime value of the database system
select CAST(GETDATE() AS DATE); --2022-07-29
So in your condition change the static date ''2022-07-28'' with GETDATE() to get the date dynamically:
WHERE CAST(T.DATE AS DATE) = CAST(GETDATE() AS DATE)
I have a field, "ID", and it has repeat values. (In the example; A21, B42, and C14). My two other fields in the table are "Date" and "Measurement". I want to create a query that will call the previous date WITH matching ID and display the results of that previous row. My end goal is to have a field in my query that will find the change between the current measurement for the ID and the measurement from the date prior.
I have attached an image of the table I have and what I want the query to display.
Sadly MS Access does not support lag(). This however can be emulated with a self-join and a not exists condition with a correlated subquery:
select
t.id,
tprev.date as previous_date,
tprev.measureement as previous_measurement
from Table1 as t
left join Table1 as tprev
on (tprev.id = t.id)
and (tprev.dat < t.date)
and (not exists (
select 1
from Table1 as t1
where
t1 = t.id
and t1.dat < t.date
and t1.dat > tprev.date
))
This is how to make the described query function:
SELECT t.NUM, t.ID, tprev.Date_ AS previous_date, tprev.Measurement AS previous_measurement
FROM Table1 AS t LEFT JOIN Table1 AS tprev ON (tprev.Date_ < t.Date_) AND (tprev.id = t.id)
WHERE not exists
(select 1
from Table1 AS t1
where
t1.ID = t.ID
and t1.Date_ < t.Date_
and t1.Date_ > tprev.Date_);
I have table contain multiple records for the name and DT, I need a query to check the Name don't have any new record in past 2 days based on the DT, how to create
Name DT
ABC 2017-09-17 06:02:23.000
ACD 2017-09-15 06:02:23.000
I think You need something like this:
SELECT Name,dt
from
(SELECT Name,MAX(dt) dt
FROM your_table
GROUP BY NAME) a
where dt < GETDATE()-2
Without knowing your table schema or sample data this is a wild guess at a query, but what you want should be doable with the GETDATE() function. If you want to use UTC time you can also use the GETUTCDATE() function.
Edit: Updated to include the ROW_NUMBER() function.
Edit 2: Replaced the GETDATE() where clause with a CTE to exclude names that have dt within the last 2 days.
WITH CTE AS (
SELECT
name
FROM table
WHERE dt > GETDATE()-2
)
SELECT
name,
dt
FROM (
SELECT
name,
dt,
ROW_NUMBER() OVER (PARTITION BY name ORDER BY dt desc) AS rn
FROM table
LEFT JOIN CTE ON
table.name = CTE.name
WHERE CTE.name IS NULL
) tbl
WHERE rn = 1
BEGIN TRAN
CREATE TABLE #CM (Name NVARCHAR(06), DT DATETIME)
INSERT INTO #CM
SELECT 'ABC','2017-09-17 06:02:23.000' UNION ALL
SELECT 'ACD','2017-09-15 06:02:23.000'
SELECT * FROM #CM
WHERE CONVERT(NVARCHAR(105),dt) < CONVERT(NVARCHAR(105),GETDATE()-2)
ROLLBACK TRAN
Since i'm new to Oracle SQL i'm in need of help.
I wish to create a new employee table in my database - fetching data from my raw table and basing the new data on 'date'.
EXAMPLE:
One of my employees have changed last name and i wish to create my table with his/hers latest data entry.
table1_raw with
columns pnbr; fname; lname; date
197001014688 ;Eva ;Andersson ;20150501
197001014688 ;Eva ;Sandsten ;20160501
198401011133 ;Peter ;Larsson ;20150102
198401011133 ;Peter ;Larsson ;20160102
194408011237 ;Sven ;Hansson ;20130203
table2 with
columns pnbr; fname; lname; date
197001014688 ;Eva ;Sandsten ;20160501
198401011133 ;Peter ;Larsson ;20160102
194408011237 ;Sven ;Hansson ;20130203
In SQL Server i've used the following query, with Common Table Expressions:
;WITH CTE as
(SELECT RN = ROW_NUMBER() OVER (PARTITION BY pnbr ORDER BY date DESC),
pnbr,
fname,
lname
FROM table1_raw)
SELECT
fname,
lname,
pnbr
INTO
table2
FROM
CTE
WHERE
RN = 1
How do i write this query in Oracle SQL?
Is there a more efficient/easier way to do this?
In Oracle your query will look like this -
insert into table2
select t.pnbr, t.fname, t.lname, t.date
from table1 t
inner join (
select pnbr, max(date) as MaxDate
from table1 tm
group by username
) tm on t.username = tm.username
and t.date = tm.MaxDate
To create a table based on a SELECT you need to use create table as in Oracle (and standard SQL)
Using rn = ... to define a column alias is invalid (standard) SQL as well, you need to use ... AS rn
And finally, the ; goes to the end of the statement, not in front of the with keyword:
create table table2
as
WITH CTE as (
SELECT ROW_NUMBER() OVER (PARTITION BY pnbr ORDER BY date DESC) as rn,
pnbr,
fname,
lname
FROM table1_raw
)
SELECT fname,
lname,
pnbr
FROM cte
WHERE rn = 1;
Is there a possible way to update an existing table with a group by summary of the same table?
Example:
Table A
data (decimal(5,2)) | id (int) | year (date)
In table A there are many records like
1.05 | 1 | 31.11.2015
10 | 1 | 31.11.2015
...
I now want to group by ID & YEAR and only have those records in table A.
11.5 | 1 | 31.11.2015
...
Is there a way to achieve this, without a copy of the table A? Like can I store a complete resultset in an variable, then truncate the table and insert the new ones grouped by in table A?
If you want to truncate the tableA and insert the new result set to tableA, then store the new result set to a temporary table, truncate tableA and then insert data from temp table to tableA.
Query
select sum(data) as data,
id,[year]
into #tbl
from tableA
group by id,[year];
truncate table tableA;
insert into tableA(data,id,[year])
select data,id,[year] from #tbl;
drop table #tbl;
select * from tableA;
Use a select into #TempResult, Truncate your table and reinsert the values from your temporary result...
I think you should calculate SUM(data) GROUP BY ID, Year, you can use Window functions (SQL Server 2005+) to resolve this, like this:
SELECT DISTINCT
SUM([data]) OVER(PARTITION BY ID, [Year]) AS [data],
ID, [Year]
FROM TableA
If your DBMS not support Window functions, you can try this:
SELECT DISTINCT
SUM([data]) AS [data],
ID, [Year]
FROM TableA
GROUP BY ID, [Year]
Without keeping a copy is kind of difficult. It is possible though, can't guarantee the performance:
This will update all rows with the new sum and after that delete all rows except 1 for each id combined with year of the date.
DECLARE #t table(data decimal(5,2), id int, year date)
INSERT #t
values(1.05, 1, '20151130'),(10, 1, '20151130')
;WITH CTE as
(
SELECT
data, SUM(data) OVER (partition by id, year(year)) new_data
FROM #t
)
UPDATE CTE SET data = new_data
;WITH CTE as
(
SELECT row_number() OVER (partition by id, year(year) ORDER BY (SELECT 1)) rn
FROM #t
)
DELETE CTE
WHERE rn > 1
SELECT * FROM #t