I have a table that has attendance of employee. This table has two columns:
first is the personnel number
second is the time of arrival
I want to isolate the earliest time in this table, because an employee can register multiple times.
Indeed I want to gain Least time of arrivalTime field for each personelNumber
I wrote the following code but it's wrong and can't separate rows
SELECT tal.PersonNo, min(tal.AttendanceTime)
FROM mqa.T_AttendanceLog tal
GROUP BY tal.PersonNo, tal.AttendanceTime
You're almost there. Just remove the AttendanceTime from the group by.
SELECT tal.PersonNo, min(tal.AttendanceTime)
FROM mqa.T_AttendanceLog tal
GROUP BY tal.PersonNo;
If you want the entire row (incase you have other columns) you can use something like this:
select *
from mqa.T_AttendanceLog a
where (PersonNo, AttendanceTime) in(
select b.PersonNo, min(b.AttendanceTime)
from mqa.T_AttendanceLog b
group by b.PersonNo);
Modify your group by clause
SELECT tal.PersonNo,min(tal.AttendanceTime)
FROM mqa.T_AttendanceLog tal
GROUP BY tal.PersonNo
I think you will need the minimum of AttendanceTime for each day. Try this:
SELECT tal.PersonNo,min(CAST(tal.AttendanceTime AS Time))
FROM mqa.T_AttendanceLog tal
GROUP BY tal.PersonNo,CAST(tal.AttendanceTime AS Date)
Related
here is my query
select narr,vocno,count(*)
from KontenLedger
WHERE VOCDT>'2018-07-01'
group by narr,vocno
having count(*)<'3'
actually if i wright as i given above ,the result which calculates two fields ('narr' and 'vocno') if i remove the field ('narr') answer is correct. i need to view the field 'narr' also without counting
Without knowing your database, nor having some limited sample date, nor expected output?
SELECT
vocno,
COUNT(*) AS total,
MAX(narr) AS max_narr
FROM KontenLedger
WHERE vocdt > '2018-07-01'
GROUP BY vocno
HAVING COUNT(*) < 3
Goal: SUM/AVG Client Data over multiple dates/transactions.
Detailed Question: How do I properly Group clients ('PlayerID') then SUM the int(MinsPlayed), then AVG (AvgBet)?
Current Issue: my Results are giving individual transactions day by day over the 90 day time period instead of the SUM/AVG over the 90 days.
Current Script/Results: FirstName-Riley is showing each individual daily transaction instead of 1 total SUM/AVG over set time period
Firstly, you don't need to use DISTINCT as you are going to be aggregating the results using GROUP BY, so you can take that out.
The reason you are returning a row for each transaction is that your GROUP BY clause includes the column you are trying to aggregate (e.g. TimePlayed). Typically, you only want to GROUP BY the columns that are not being aggregated, so remove all the columns from the GROUP BY clause that you are aggregating using SUM or AVG (TimePlayed, PlayerSkill etc.).
Here's your current SQL:
SELECT DISTINCT CDS_StatDetail.PlayerID,
StatType,
FirstName,
LastName,
Email,
SUM(TimePlayed)/60 AS MinsPlayed,
SUM(CashIn) AS AvgBet,
SUM(PlayerSkill) AS AvgSkillRating,
SUM(PlayerSpeed) AS Speed,
CustomFlag1
FROM CDS_Player INNER JOIN CDS_StatDetail
ON CDS_Player.Player_ID = CDS_StatDetail.PlayerID
WHERE StatType='PIT' AND CDS_StatDetail.GamingDate >= '1/02/17' and CDS_StatDetail.GamingDate <= '4/02/2017' AND CustomFlag1='N'
GROUP BY CDS_StatDetail.PlayerID, StatType, FirstName, LastName, Email, TimePlayed, CashIn, PlayerSkill, PlayerSpeed, CustomFlag1
ORDER BY CDS_StatDetail.PlayerID
You want something like:
SELECT CDS_StatDetail.PlayerID,
SUM(TimePlayed)/60 AS MinsPlayed,
AVG(CashIn) AS AvgBet,
AVG(PlayerSkill) AS AvgSkillRating,
SUM(PlayerSpeed) AS Speed,
FROM CDS_Player INNER JOIN CDS_StatDetail
ON CDS_Player.Player_ID = CDS_StatDetail.PlayerID
WHERE StatType='PIT' AND CDS_StatDetail.GamingDate BETWEEN '2017-01-02' AND '2017-04-02' AND CustomFlag1='N'
GROUP BY CDS_StatDetail.PlayerID
Next time, please copy and paste your text, not just linking to a screenshot.
I'm struggling to get the correct result with this query:
select max(kts.my_date), kts.name
join ktt on ktt.someId = kts.someOtherId
where ktt.someId = 'example'
group by kts.name;
I have two (possibly stupid) questions:
Will this max() take time into account? I know that order by does if the dates are the same. Does max do the same?
This is connected to my previous question, but when I run the query above, if the dates are same, it orders it by the name. I want the latest date at the top. Do I need to put an order by clause for the date in? If so, using Max is pointless, right?
Thanks for the help.
Yes,
--2
select max(kts.my_date) over (partition by kts.name) as maxdate, kts.name
from -- chose your table
join ktt on ktt.someId = kts.someOtherId
where ktt.someId = 'example'
order by --chose here your column
give this a try
I am trying to build a SQL query where I group by 1 column, but then also include the values of other columns from an arbitrary record in each group. So, something like
SELECT BoxNo
FROM MuffinData
WHERE FrostingTimeApplied >= CONVERT(date, GETDATE())
GROUP BY BoxNo
but including some value from columns MuffinType, FrostingType in the result (I know that there will be only 1 value of MuffinType and FrostingType per box.)
You have to use an aggregate function for each column selected that is not present in the GROUP BY clause:
SELECT BoxNo, MAX(MuffinType) AS MuffinType, MAX(FrostingType) AS FrostingType
FROM MuffinData
WHERE FrostingTimeApplied >= CONVERT(date, GETDATE())
GROUP BY BoxNo
If there is only 1 value of MuffinType and FrostingType per box, then these unique values per box no are going to be selected in the above query.
I know that there will be only 1 value of MuffinType and FrostingType
per box
If that's indeed the case, a simple DISTINCT should do the trick, like so:
SELECT DISTINCT BoxNo, MuffinType, FrostingType
FROM MuffinData
WHERE FrostingTimeApplied >= CONVERT(date, GETDATE());
If that's not the case, you're dealing with a problem known generally as the Top N per group problem. You can find coverage of the problem and suggested solutions here.
Cheers,
Itzik
If you're grouping by anything, then the only way to do this in a single statement (that I'm aware of) is to have the other columns you're returning be the result of an aggregate function. Aggregate functions are anything that take multiple values but return you a single result like: SUM, MAX, MIN, COUNT, etc...
SELECT BoxNo, COUNT(MuffinData.ID), MAX(FrostingType.FlavorID) FROM MuffinData, FrostingType etc...
You might have to adjust your WHERE logic or have another data source in your FROM list (subquery).
You can use a CTE and join back to the original table to get the fields you want. In this case,
WITH BoxGroup AS (SELECT BoxNo FROM MuffinData WHERE FrostingTimeApplied >= CONVERT(date, GETDATE()) GROUP BY BoxNo) SELECT md.BoxNo,md.MuffinType,md.FrostingType FROM MuffinData md INNER JOIN BoxGroup bg ON bg.BoxNo = md.BoxNo
Here is what my table looks like.
Person Date Entry
Person1 05-20-14 142
Person2 05-20-14 443
Person1 05-21-14 248
Person1 05-21-14 142
I need two things.
First the number of times a Person made an entry for the first time.
I tried doing it with these queries. But the problem is I need this information per day.
That is if I query for 05/21, I need to see output
"Person1 1"
142 wont be included because it already exists.
In my query, I am filtering by date already, so I am not sure how to go out and search in the rest of the dates values. Here is what I have.
SELECT PERSON, Count(distinct Entry)
from [table]
where date >= 05/21/2014
and date < 05/22/2014
group by person
order by person.
This gives me
Person1 2
Both 248 and 142 are considered here. How do I look for 142 was an entry already made in previous dates. I am not very good at nested queries.
Thanks for looking.
Will this solve your problem or give you an idea how inner query should be?
SELECT PERSON, Count(distinct Entry)
from [table]
where date >= 05/21/2014
and date < 05/22/2014
and Entry not in (select distinct entry from [table] where date <> 05/21/2014)
group by person
order by person.
in the above query i have just added an inner query to get the distinct entry from other dates
select distinct entry from [table] where date <> 05/21/2014
and i have added the where condition that the current result should not consider those entries by
and Entry not in (select distinct entry from [table] where date <> 05/21/2014)
hope this helps you.
For the first query, it sounds like you need something like this:
SELECT Person
FROM sample
GROUP BY date, person
HAVING date = '05-21-2014'
See http://sqlfiddle.com/#!3/4653d/1
This might also help:
SELECT Person, date
FROM sample
GROUP BY date, person
ORDER BY date
Hopefully that helps, let me know if I am misunderstanding something..