I have following sql query
SELECT [POINT],[Timestamp],AvgValue=AVG(ValueNumeric)
FROM [myDatabase].[dbo].[Value]
WHERE [Point] In (98,99,100,101,102,103,104,105)
AND Timestamp between DATEADD(DAY, -90, GETDATE()) and GETDATE()
Group by [timestamp],[Point]
order by [Timestamp], [Point]
which returns table as below
but I need to transpose this as below
Timestamp|Point1|Value1|Point2|Value2|Point3|Value3|
Is this doable?
Related
What would be the conversion of this MS Access sql statement to MS SQL statement:
Select *
from iapt
where appt_id in (Select distinct appt_id
from iaptd where po_id in(Select distinct po_id
from irct
where verify_dtim = Date()-1))
Subtract one day from GETDATE() (today).
Select *
from iapt
where appt_id in (Select distinct appt_id
from iaptd where po_id in(Select distinct po_id
from irct
where verify_dtim = DATEADD(day, -1, convert(date, GETDATE())))
I guess you only need to change
Date() - 1
to
DATEADD(dd, -1, CAST(GETDATE() AS DATE))
I have a table with a list of databases, about 10k rows, 1 row for each database. The table contains attributes for:
ServerId (int)
DBID (int)
DBName (varchar(255))
Createtime(datetime)
LastCheckTime(datetime).
LastCheckTime is frequently updated until the database is removed on the SQL Server, and then it stops being updated.
I would like to count how many "living" databases there have been each year. If the LastCheckTime < getdate()-1 then I assume it's alive and should be counted for each year from Createtime until LastCheckTime is more than 1 day.
The table resides in a SQL Server 2008R2 database. I can upgrade to SQL Server 2016 if it's easier to accomplish this.
The following query returns the desired output:
CREATE TABLE [DB]
(
ServerId INT,
[DBID] INT,
DBName varchar(255),
Createtime DATETIME,
LastCheckTime DATETIME
)
INSERT INTO [DB] VALUES
(1,1, 'DB1', '2010-09-10', '2013-09-01'),
(1,2, 'DB2', '2010-09-10', GETDATE()),
(1,3, 'DB3', '2010-09-12', GETDATE()),
(1,4, 'DB4', '2011-09-13', GETDATE()),
(1,5, 'DB5', '2011-09-10', GETDATE()),
(1,6, 'DB6', '2013-09-10', GETDATE()),
(1,7, 'DB7', '2014-09-10', GETDATE()),
(1,8, 'DB8', '2015-09-10', GETDATE()),
(1,9, 'DB9', '2016-09-10', GETDATE()),
(1,10, 'DB10', '2017-09-10', GETDATE());
--CTE holds all the years starting from the oldest DB
;WITH CTE
AS ( SELECT DATEPART(YEAR, ( SELECT MIN(CreateTime)
FROM [DB]
)) AS year
UNION ALL
SELECT year + 1
FROM CTE
WHERE year < DATEPART(YEAR, GETDATE())
)
SELECT year ,
COUNT(*) DBCount
FROM CTE
LEFT JOIN [DB] ON CTE.year BETWEEN DATEPART(YEAR, [DB].Createtime)
AND DATEPART(YEAR,
[DB].LastCheckTime + 1)
WHERE [DB].LastCheckTime > DATEADD(DAY, -1, GETDATE()) -- filter only live DBs
GROUP BY year;
And the result would be:
What will be the best script to get the records from a certain table with column createdDate and modifiedDate where modifiedDate is the only nullable column and is equal to date now/current date?
In case that there will be null values from modified date column, the script will get the created date.
The output should get all records with latest created or modified data. I tried these script below:
SELECT *
FROM table
WHERE ISNULL(CONVERT(date, ModifiedDate),
CONVERT(date,CreatedDate)) = CONVERT(date, getdate())
or
SELECT *
FROM table
WHERE CASE WHEN ModifiedDate IS NULL
THEN CONVERT(date, CreatedDate)
ELSE CONVERT(date,ModifiedDate) END = CONVERT(date, getdate())
If you just need this against today (= GETDATE()) this should be simple as this:
SELECT *
FROM tbl
WHERE CAST(CreatedDate AS DATE) = CAST(GETDATE() AS DATE)
OR (ModifiedDate IS NOT NULL AND CAST(ModifiedDate AS DATE)= CAST(GETDATE() AS DATE));
Some thoughts: It is very bad to use functions (here: ISNULL()) in predicats. The engine will not be able to apply existing indexes. Read about sargable. One exception is CAST(SomeDateTime AS DATE) which is sargable.
If you do not need this for any date (just for today), it should be enough to say: This row was created today or it was modified today.
Important Be sure that there are indexes for ModifiedDate and CreatedDate!
Is this what you are looking for?
SELECT
someColumn1,
someColumn2,
CASE
WHEN modifiedDate IS NULL THEN createdDate
ELSE modifiedDate
END AS someDate
FROM someTable
You may use ISNULL() function in TSQL
SELECT *
FROM table
WHERE
ISNULL(CONVERT(date,ModifiedDate), CONVERT(date, CreatedDate)) = CONVERT(date, getdate())
If using function will affect the indexes, kindly try this one if this is efficient.
SELECT *
FROM table
WHERE ModifiedDate IS NOT NULL
AND CONVERT(date,ModifiedDate) = CONVERT(date, getdate()
UNION
SELECT *
FROM table
WHERE ModifiedDate IS NULL
AND CONVERT(date,CreatedDate) = CONVERT(date, getdate()
I have a table with two fields - datetime and int. I want to do a group by on the datetime only on the date ignoring the hour and minute. The SELECT statement should return a date that maps to the sum of the int of a single day.
SELECT CAST(Datetimefield AS DATE) as DateField, SUM(intfield) as SumField
FROM MyTable
GROUP BY CAST(Datetimefield AS DATE)
As he didn't specify which version of SQL server he uses (date type isn't available in 2005), one could also use
SELECT CONVERT(VARCHAR(10),date_column,112),SUM(num_col) AS summed
FROM table_name
GROUP BY CONVERT(VARCHAR(10),date_column,112)
I came researching the options that I would have to do this, however, I believe the method I use is the simplest:
SELECT COUNT(*),
DATEADD(dd, DATEDIFF(dd, 0, date_field),0) as dtgroup
FROM TABLE
GROUP BY DATEADD(dd, DATEDIFF(dd, 0, date_field),0)
ORDER BY dtgroup ASC;
-- I like this as the data type and the format remains consistent with a date time data type
;with cte as(
select
cast(utcdate as date) UtcDay, DATEPART(hour, utcdate) UtcHour, count(*) as Counts
from dbo.mytable cd
where utcdate between '2014-01-14' and '2014-01-15'
group by
cast(utcdate as date), DATEPART(hour, utcdate)
)
select dateadd(hour, utchour, cast(utcday as datetime)) as UTCDateHour, Counts
from cte
Personally i prefer the format function, allows you to simply change the date part very easily.
declare #format varchar(100) = 'yyyy/MM/dd'
select
format(the_date,#format),
sum(myfield)
from mytable
group by format(the_date,#format)
order by format(the_date,#format) desc;
I have a table with 3 columns,sql server 2008
item, count , date
I would like a query that selects the total count per item for current day with
date colums having the format 2011-03-30 09:00:00.000
select
date,item, sum(count) as total
from
table1
group by
date,item
EDIT:
select item, dateadd(day,datediff(day,0,date),0) as dateInserted, sum(count)
as total from Table1 group by item, dateadd(day,datediff(day,0,date),0) order by 1
the first select returns totals but grouped by the datetime instead of date...then the second returns totals correctly but I find some items appearing twice with the same date but different totals
Sounds like you're looking for 'current day' results?
select item, dateadd(day, datediff(day, 0, date), 0) as dateInserted,
sum(count) as total
from Table1
WHERE
dateadd(day, datediff(day, 0, [date]), 0) =
dateadd(day, datediff(day, 0, getdate()), 0)
group by
item, dateadd(day, datediff(day, 0, date), 0)
order by 1
If you're using SQL Server 2008, use the DATE datatype.
select item,
CAST([date] as DATE)) as dateInserted,
sum(count) as total
from Table1
WHERE
CAST([date] as DATE)) = CAST(getdate() as DATE)
group by
item, CAST([date] as DATE))
order by 1