5
Answers

SQL query

hello!

Problem: I want to select max and min date from the table for every row(GROUPING BY id) then perform date difference operation over it. Afterwards i want to divide that diff with 30 and store in another table. HOW TO DO THAT ?
eX TABLE

NameDateID
A5/3/141
B6/4/14 2
A5/4/141
C11/8/143
D6/7/144
C7/7/143
D9/9/144

my QUERY
SELECT INSERT INTO <TAB2>
SELECT DATEDIFF/30 AS DIFFFROM
(SELECT MAX(DATE),  MIN(DATE) FROM <TAB1>
GROUP BY ID) ;


but its not working pls suggest how to do date
Answers (5)
1
Abhishek  Jaiswal
NA 22.1k 5.1m 10y
Thanks!
:)
0
Ramesh  Maruthi
NA 6.1k 406.2k 10y
Declare  @a TABLE(diffBY30 DATETIME)
 INSERT INTO @a(diffBY30)
 Select
  Convert(int,  DateDiff(D,MaxDate, MinDate))/30 as diff
  from(
  SELECT(Select CAST( Max
  (o.Created) AS DATETIME)) AS MaxDate
  ,(select CAST( MIN(o.Created) AS DATETIME)) as MinDate
  from [Order] AS o)A
  select * from @a
Group by diffBY30
0
Abhishek  Jaiswal
NA 22.1k 5.1m 10y
How to apply count operation over this query for calculating number of same DIFF,
such as if after diff values are 10, 2,10,10,3
then it give result after count that 10 is 3 times, 2 is once etc.

my query is

SELECT 
MaxDate,
MinDate,
DATEDIFF(D, MinDate, MaxDate)/30 AS DIFF
FROM (
SELECT 
   MAX(TDate) AS MaxDate, 
   MIN(TDate) AS MinDate 
   FROM EDATA
   GROUP BY TCard
   
)a 
0
Abhishek  Jaiswal
NA 22.1k 5.1m 10y
Heyy Ramesh!

what if i want to store that diff into same table but in a new column ?
0
Ramesh  Maruthi
NA 6.1k 406.2k 10y
 Declare  @a TABLE(diffBY30 DATETIME)
 INSERT INTO @a(diffBY30)
 Select
  Convert(int,  DateDiff(D,MaxDate, MinDate))/30 as diff
  from(
  SELECT(Select CAST( Max
  (o.Created) AS DATETIME)) AS MaxDate
  ,(select CAST( MIN(o.Created) AS DATETIME)) as MinDate
  from [Order] AS o)A
  select * from @a

In this example @a table is your Tab2