I need help with a query. I want to group a query by the ID field, and the MIN() of the date field. However, I want the min of all dates in the future.
Would I need a subquery here? The following will exclude all records that don't occur in the future.
SELECT CompanyCode, MIN(NextDate) AS NextFollowUpDate
FROM dbo.LeadNote AS LeadNote
GROUP BY CompanyCode
HAVING (MIN(NextDate) > GETDATE())
Just use a regular where:
SELECT CompanyCode, MIN(NextDate) AS NextFollowUpDate
FROM dbo.LeadNote AS LeadNote
WHERE NextDate > GETDATE()
GROUP BY CompanyCode
If you need every company code with nulls for missing items you can do this
SELECT cc.CompanyCode, g.NextFollowUpDate
FROM
(
SELECT DISTINCT CompanyCode
FROM dbo.LeadNote
) AS cc
LEFT JOIN
(
SELECT CompanyCode, MIN(NextDate) AS NextFollowUpDate
FROM dbo.LeadNote
WHERE NextDate > GETDATE()
GROUP BY CompanyCode
) AS g ON cc.CompanyCode = g.CompanyCode
Related
I have made an table which has 3 columns
Table
In this above table there are 3 columns i.e Trandsaction, EmpID, Date having some data. I need to form a table using some queries so that in the result table I will get how many transactions done in each month by a particular EmpID.
So the result table must be like:
So how to get month wise number of transactions per EmpID?
Get all employees (I assume you have an employee table).
Get all months (you could get them from the transaction table).
Cross join the two in order to get all combinations, because you want to show these in your result.
Outer join the transaction counts per month and employee (an aggregation subquery). SQL Server's date to string conversion is a bit awkward compared to other DBMS. You need to convert to a predefined format and use a substring of that.
Use COALESCE to turn null (for no count for the employee and month) to zero.
The query:
select m.month, e.empid, coalesce(t.cnt, 0) as transaction_count
from employees e
cross join (select distinct convert(varchar(7), t.date, 126) as month from transactions) m
left join
(
select convert(varchar(7), t.date, 126) as month, empid, count(*) as total
from transactions
group by convert(varchar(7), t.date, 126), empid
) t on t.empid = e.empid and t.month = m.month
order by m.month, e.empid;
If you don't want all employees, but only those that have at least one transaction in some month, then replace from employees e with from (select distinct empid from transactions) e.
SELECT Date, EmpID, COUNT(transaction)
FROM your_table
GROUP BY Date, EmpID
To get the date like "YYYY-MM" use
for MySQL (replace the String with the date column):
DATE_FORMAT("2019-11-25 10:49:30.000", "%Y - %m")
for oracle:
to_char(date, 'YYYY-MM')
I am querying SQL database to identify how many time a CustID appeared in the booking cancellation table with cancellation date range:
SELECT CustID
,COUNT(*) as Occurance
FROM [Test].[dbo].[vwBookingCancelletion]
WHERE [Cancellation Date] between '2018-12-01' and '2019-12-31'
GROUP BY CustID
ORDER BY occurance desc
Output i want to achieve is as below:
My current query does give me correct out put but when I try to add additional columns in the select statement I also have to add it in the Group by which brings incorrect output.
I have been trying sub query(learning while typing this) without any success and also few other solutions on SE.
any help will be appreciated.
You could do it this way:
select a.*, Occurance
FROM [Test] a inner join
(
SELECT CustID
,COUNT(*) as Occurance
FROM [Test].[dbo].[vwBookingCancelletion]
WHERE [Cancellation Date] between '2018-12-01' and '2019-12-31'
GROUP BY CustID
)b on a.CustID = b.CustID
This is my query which is not returning the correct result ordered by RegistrationDate Desc:
SELECT
Team,
CONVERT(VARCHAR(10), RegistrationDate, 103) AS RegistrationDate,
FormFilledAt, CreationBy
FROM
Table_Candidate_Info
WHERE
Status = 'Completed'
GROUP BY
Team, CONVERT(VARCHAR(10), RegistrationDate, 103), FormFilledAt, CreationBy
ORDER BY
RegistrationDate DESC
If I will use this query, it's returning the correct order by RegistrationDate Desc
select *
from Table_Candidate_Info
order by RegistrationDate desc
I want above first query should be RegistrationDate order by Desc with group by query
Try
order by CONVERT(VARCHAR(10),RegistrationDate,103) desc
or better if you want really keep order by date (and not text) try this:
select Team, CONVERT(VARCHAR(10),RegistrationDate,103) as RegistrationDate, FormFilledAt,CreationBy
from (
Select Team, cast(RegistrationDate as date) as RegistrationDate ,FormFilledAt,CreationBy
from Table_Candidate_Info
where Status='Completed'
group by Team,cast(RegistrationDate as date) ,FormFilledAt,CreationBy
) tmp
order by RegistrationDate desc
Note: if you want group by date + time remove cast… as date
use distinct and CONVERT(VARCHAR(10),RegistrationDate,103) in order by clause
Select distinct Team,CONVERT(VARCHAR(10),RegistrationDate,103)as RegistrationDate ,FormFilledAt,CreationBy
from Table_Candidate_Info where Status='Completed'
order by CONVERT(VARCHAR(10),RegistrationDate,103) desc
Note: You don't need group by since you are not using any aggregated function
The reason why results are not ordered by RegistrationDate when you convert it to a varchar in Select clause is because Order By clause is logically processed after evaluation of Select clause.
Now in first query when you write
Select * from Table_Candidate_Info order by RegistrationDate desc
[Though writing * in select list is a very bad practice] format of RegistrationDate still remains date in Select clause which holds true for further logical processing phase of Order By clause. Hence 31.01.2019 comes first and 31.12.2018 later.
But when you convert it to varchar(10) to get a required format then actually Order By clause is ordering a Varchar and not Date. Hence 31/12/2018 comes first and 31/01/2019 comes after it.
To resolve the problem if you want to retain the formatting of datetime/date column in Select but Order By with Date value then simply cast the datetime column back to Date in Order by clause.
Pseudo code as:
select CONVERT(VARCHAR(10),RegistrationDate,103) as RegistrationDate from
Table_Candidate_Info
order by cast(RegistrationDate as Date) desc -- cast it back to date
Demo Link here: https://rextester.com/WMLQL78387
Using the below example, I want to get a count of records grouped by Person and Product, for each day.
The example on the left is the data in the SQL Server 2008 database. The data on the right is my desired query result.
I could just substring off the time portion of the date values, then just do a Group By... but ideally the Max(date) would contain the full timestamp...
Is this what you want?
select t.person, t.product, count(*) as cnt, max(t.date) as date
from table t
group by t.person, t.product, cast(t.date as date);
SELECT date, id, sum(revenue)
FROM table
WHERE date between '2013-01-01' and '2013-01-08'
GROUP BY date, id
HAVING sum(revenue)>1000
Returns rows that have revenue>1000.
SELECT date, id, sum(revenue)
FROM table
WHERE date between '2013-01-01' and '2013-01-08'
AND id IN (SELECT id FROM table where date between '2013-01-01' and '2013-01-08' GROUP BY id HAVING sum(revenue)>1000)
GROUP BY date, id
Returns rows for id's whose total revenue over the date period is >1000 as desired. But this query is much slower. Any quicker way to do this?
Make sure you have indexes on the date and id columns, and try this variation:
select t.date, t.id, sum(t.revenue)
from table t
inner join (
select id
from table
where date between '2013-01-01' and '2013-01-08'
group by id
having sum(revenue) > 1000
) ts on t.id = ts.id
where t.date between '2013-01-01' and '2013-01-08'
group by t.date, t.id
it's not MySQL, it's Vertica ;)
Cris, what projection and order by you using in CREATE TABLE ???
Do you try using database designer
see http://my.vertica.com/docs/6.1.x/HTML/index.htm#14415.htm