I want output of sql Query and i already done some query part but it fails - sql

The query which i have written
SELECT Date_current,
COUNT(*) as'Total'
FROM Call_Register
WHERE (DATEDIFF(dd,'02/1/2014',Date_current) >=0)
AND (DATEDIFF(dd,'02/12/2014',Date_current) <=0)
GROUP BY Date_current
HAVING COUNT(*)>=(convert(int,'02/12/2014')) \
ORDER BY Date_current
But this query gives me error:
Conversion failed when converting the varchar value '02/12/2014' to data type int.
Date Total
---------- -------
Feb 3 2014 2:58PM 10
Feb 4 2014 2:59PM 10
Please Help me
getting Output like
Date Total
---------- -------
Feb 3 2014 2:58PM 1
Feb 3 2014 2:59PM 1
Feb 3 2014 3:00PM 1
Feb 3 2014 3:08PM 1
Feb 3 2014 3:20PM 1
Feb 3 2014 4:05PM 1
Feb 3 2014 4:17PM 1
Feb 3 2014 4:19PM 1
Feb 3 2014 4:21PM 1
Feb 3 2014 4:24PM 1
Feb 4 2014 1:11PM 1
Feb 4 2014 2:35PM 1
Feb 4 2014 2:37PM 1
Feb 4 2014 5:19PM 1

Firstly, you should either use the culture invariant date format yyyyMMdd, or explicitly set the date format using SET DATEFORMAT DMY, or prepare to get inconsistent results.
Secondly, the following is potentially very inefficient:
WHERE (DATEDIFF(dd,'02/1/2014',Date_current) >=0)
AND (DATEDIFF(dd,'02/12/2014',Date_current) <=0)
If you have an index on Date_Current it will not be used because you are performing a function on it. You should instead use:
WHERE Date_Current >= '20140102'
AND Date_Current <= '20141202'
You then have a sargable query. I have had to guess at whether '02/1/2014' meant 1st February 2014, or 2nd January 2014 as it is not clear (hence the importance of my first point).
Finally (this part has already been answered but including it for completeness as I couldn't not point out the first two errors) you cannot convert to int here:
convert(int,'02/12/2014')
You presumably need to convert to date time first:
CONVERT(INT, CONVERT(DATETIME, '20141202'))
Although I suspect this is still not what you want, you are just filtering the days to those that have 41973 records or more, seems like a fairly arbitrary filter....

You need to Cast your String Date after that only you can proceed with Int CAST
CAST('02/12/2014' AS Datetime)
Try this
SELECT Date_current,COUNT(*) AS 'Total'
From Call_Register
WHERE (DATEDIFF(dd,'02/1/2014',Date_current)>=0) AND
(DATEDIFF(dd,'02/12/2014',Date_current)<=0)
Group By Date_current
having COUNT(*)>=(convert(int, CAST('02/12/2014' AS Datetime)) order By Date_current

At last got the as for my question thnx everyone
SELECT cast(Date_current as DATE),COUNT(*) AS 'Total'
From Call_Register
WHERE (DATEDIFF(dd,'02/1/2014',Date_current)>=0) AND
(DATEDIFF(dd,'02/13/2014',Date_current)<=0)
Group By cast(Date_current as DATE)
can i use order by in this cos i want it in descending order pl help for that

Related

Return the first and last value from one column when value from another column changes

I am trying to write a PostgreSQL query to return the first and last dates corresponding to indices. I have a table:
Datetime
Index
March 1 2021
0
March 2 2021
0
March 3 2021
0
March 4 2021
1
March 5 2021
1
March 6 2021
2
In this case, I would want to return:
I am wondering how I would write the PostgreSQL query for this.
I think this can be done with the following:
SELECT MIN("Datetime") AS Start
, MAX("Datetime") AS End
, "Index"
FROM <your_table>
GROUP BY "Index"
ORDER BY "Index"
;

MS Access SQL – How can I count the occurrence of a number from one table in strings of another table

In MS Access 365 I have 2 tables and I want to count the occurrence of a year from the first table in part of a string of the second table, purely with SQL (so far I used VBA, but I want to simplify).
The first table (tDistinctYears) contains all the years, in which one of our members paid:
ID
PaymentYear
1
2015
2
2016
3
2017
3
2018
4
2019
5
2020
6
2021
7
2022
The second table (tPayments) has all payments from members with one column containing a membership number and the other one containing payment years. Sometimes a member pays for one year, sometime for several years. The table therefore looks like that:
MembershipNr
YearPayment
11
2016
11
2017
11
2018
26
2017
26
2018;2019
26
2020;2021;2022
38
2016
38
2017
38
2018;2019;2020;2021
I want a query which tells me how many members paid in which year:
PaymentYear
Count
2015
0
2016
2
2017
3
2018
3
2019
2
2020
2
2021
2
I used the following SQL query, which I found using various answers on stackoverflow:
SELECT tDistinctYears.PaymentYear, (COUNT(tPayments.YearPayment)) AS [Count]
FROM tDistinctYears
LEFT JOIN tPayments ON tDistinctYears.PaymentYear like "*" & tPayments.YearPayment & "*"
WHERE (tDistinctYears.PaymentYear > 0 AND tDistinctYears.PaymentYear <= YEAR(NOW()))
GROUP BY tDistinctYears.PaymentYear;
But what I get is this:
PaymentYear
Count
2015
0
2016
2
2017
3
2018
1
2019
0
2020
0
2021
0
It seems as if the above query does not use the “like” expression in the JOIN ON section.
Can someone help me, please?
I think you are close just alter column in where condition tPayments.YearPayment should be first and tDistinctYears.PaymentYear should be inside like operator.
SELECT tDistinctYears.PaymentYear, (COUNT(tPayments.YearPayment)) AS [Count]
FROM tDistinctYears
LEFT JOIN tPayments ON tPayments.YearPayment like "*" &
tDistinctYears.PaymentYear
& "*" WHERE (tDistinctYears.PaymentYear > 0 AND tDistinctYears.PaymentYear <=
YEAR(NOW()))
GROUP BY tDistinctYears.PaymentYear;

Calculate number of months between 2 dates SQL

I want to calculate number of full months between 2 dates
declare #startdate date='2021-03-03';
declare #enddate date='2021-05-02';
select datediff(mm,#startdate,#enddate)
This is giving me output as 2 but I want it to be 1 as it should count full months only. So from 03/03/2021 to 03/04/2021 is 1 month and from 03/04/21 to 02/05/21 is still not full month. So, the answer should be 1 in this example. How do I achieve this?
One way is
declare #startdate date='2021-03-03';
declare #enddate date='2021-05-02';
select datediff(mm,#startdate,#enddate) - case when dateadd(mm, datediff(mm,#startdate,#enddate), #startdate) > #enddate then 1 else 0 end
Full month is a tricky thing in some cases.
If I start on Jan 31 and end on Feb 28 is that a full month?
If I start on Feb 28, 2019 and end on Feb 28, 2020 is that 12 full months?
If I start on Feb 29, 2020 and end on Feb 28, 2021 is that 12 full months?
If you did not care about these cases and just wanted a simplistic rule like "it's a full month if the EndDay>=StartDay you could adjust the difference with a "Case When Day(Enddate)<Day(Startdate) Then 1 Else 0 End"

How to change start date in a table to a pair of start date and end date using SQL

The title must be confusing, but the thing I am trying to do is very easy to understand with an example. I have a table like this:
Code Date_ Ratio
73245 Jan 1 1975 12:00AM 10
73245 Apr 18 2006 12:00AM 4
73245 Dec 26 2007 12:00AM 10
73245 Jan 30 2009 12:00AM 4
73245 Apr 21 2011 12:00AM 2
Basically for each security it gives some ratio for it with a date when the ratio starts to be effective. This table will be much easier to use if instead of just having a start date, it has a pair of start date and end date, like the following:
Code StartDate_ EndDate_ Ratio
73245 Jan 1 1975 12:00AM Apr 18 2006 12:00AM 10
73245 Apr 18 2006 12:00AM Dec 26 2007 12:00AM 4
73245 Dec 26 2007 12:00AM Jan 30 2009 12:00AM 10
73245 Jan 30 2009 12:00AM Apr 21 2011 12:00AM 4
73245 Apr 21 2011 12:00AM Dce 31 2049 12:00AM(or some random date in far future) 2
How do I transform the original table to the table I want using SQL statements? I have little experience with SQL and I could not figure how.
Please help! Thanks!
In SQL Server 2012:
SELECT code,
date_ AS startDate,
LEAD(date_) OVER (PARTITION BY code ORDER BY date_) AS endDate,
ratio
FROM mytable
In SQL Server 2005 and 2008:
WITH q AS
(
SELECT *,
ROW_NUMBER() OVER (PARTITION BY code ORDER BY date_) AS rn
FROM mytable
)
SELECT q1.code, q1.date_ AS startDate, q2.date_ AS endDate, q1.ratio
FROM q q1
LEFT JOIN
q q2
ON q2.code = q1.code
AND q2.rn = q1.rn + 1
Maybe it would also be possible to use OUTER APPLY, something like:
SELECT t1.Code, t1.Date_ AS StartDate_, ISNULL(t2.EndDate_, CAST('20491231' AS DATETIME)) AS EndDate_
FROM t1 AS t1o
OUTER APPLY
(
SELECT TOP 1 Date_ AS EndDate_
FROM t1
WHERE t1.Code = t1o.Code AND t1.Date_ > t1o.Date_
ORDER BY t1.Date_ ASC
) AS t2

How to calculate Rank SQL query

HI, I have the following table which save agent ranking on daily basis on basis of tickets status.
No. **Agent Name** **Incidents** **workorder** **Rank** **TimeStamp**
1 cedric 200 29 1 21 Jan 2011
2 poul 100 10 2 21 Jan 2011
3 dan 200 20 1 21 Jan 2011
4 cedric 100 19 2 22 Jan 2011
5 poul 200 26 1 22 Jan 2011
6 dan 150 20 2 22 Jan 2011
Now i need query which fetch ranking between two dates means if i select date between 21 jan 2011 to 22 jan 2011 then query return me agents average ranking between these two dates of agent not return the agent ranking details on date wise. I need single name of agent with his ranking.
Regards,
Iftikhar hashmi
Try
SELECT [Agent Name], AVG(RANK) FROM MY_TABLE WHERE [TimeStamp] BETWEEN DATE1 AND DATE2
GROUP BY [Agent Name]
(Update)
Thanks to Martin which reminded me I need to cast RANK.
SELECT [Agent Name], AVG(CAST(RANK AS FLOAT)) FROM MY_TABLE WHERE [TimeStamp] BETWEEN DATE1 AND DATE2
GROUP BY [Agent Name]