I have wrote the following query.
SELECT pro.[Id], COUNT(*) AS Count
FROM {Task} tsk
JOIN {profile} pro ON tsk.[ProfileId]=pro.[Id]
GROUP BY
pro.[Id]
HAVING
COUNT(*) > 1
This returns the records I am interested in but it returns the following...
ID Count
12345 3
21254 2
25458 2
I now need to take it a stage further and I think I would need to use the query I have wrote within another query to get what I need.
I basically need to see the underlying data in the count e.g. task-number. So the end result will look something like this based on the above example.
ID Count
12345 123-345
12345 135-564
12345 136-985
21254 124-856
21254 135-854
25458 214-854
25458 365-850
Am I correct in thinking I need a subquery to do this and how would I go about it?
Thanks
You could go with a CTE, count, filter then join
WITH CTE AS (
SELECT pro.[Id], COUNT(*) AS Count
FROM {Task} tsk
JOIN {profile} pro ON tsk.[ProfileId]=pro.[Id]
GROUP BY
pro.[Id]
HAVING
COUNT(*) > 1
)
SELECT tsk.[Id], tsk.[ProfileId] FROM CTE
JOIN {Task} tsk ON CTE.[Id] = tsk.[ProfileId]
Related
I have a data set like the following:
BaseID| SecondaryTypeID| Value
------------------------------
1 5 1
1 5 3
1 5 8
What I'd like to get is something like:
BaseID| SecondaryTypeID| Value
------------------------------
1 5 12
Now, I can return that data set with the following:
select BaseID,
SecondaryTypeID,
(select sum(Value) from TableA) as SummedValue
from TableA
group by BaseID, SecondaryTypeID
However, I can't do that as part of a merge query, the inner select breaks the merge with an error that looks like this:
Query processor could not produce a query plan because of the hints
defined in this query. Resubmit the query without specifying any hints
and without using SET FORCEPLAN.
Any thoughts on how to resolve this would be gratefully received!
Why would you use a subquery? Just do:
select BaseID, SecondaryTypeID, sum(Value) as SummedValue
from TableA
group by BaseID, SecondaryTypeID;
If you have multiple rows in the result set and want the overall sum, then use window functions:
select BaseID, SecondaryTypeID,
sum(sum(Value)) over () as SummedValue
from TableA
group by BaseID, SecondaryTypeID;
This is what your query really is saying to do. I doubt it is what you want.
Every time I used 'Count()' to count the duplicate PointIDs' on the query I get this error.
I have narrowed down the problem is with Count() function, used MAX() with Group by and didn't have any problem. This is on access database populated using ODBC connection. All the help is appriciated - I have done all the research and this is my last online resort.
SELECT Event1.PointID, Event1.LogTimeStamp, Count(Event1.PointID) AS acount
FROM Event1
GROUP BY Event1.PointID, Event1.LogTimeStamp;
I suspect you actually want to return all duplicate records which is a 2 step operation.
Step 1 get find the ids which are duplicated
SELECT Event1.PointID, Count(*) AS NumOfRecords
FROM Event1
GROUP BY Event1.PointID
HAVING COUNT(*) > 1
Step 2 join that result back to the original table to find the records
SELECT e.*, d.NumOfRecords
FROM
Event1 e
INNER JOIN (
SELECT Event1.PointID, Count(*) AS NumOfRecords
FROM Event1
GROUP BY Event1.PointID
HAVING COUNT(*) > 1
) d
ON e.PointId = d.PointId
This is the standard syntax for what you want to do:
SELECT Event1.PointID, Event1.LogTimeStamp, Count(*) AS acount
FROM Event1
GROUP BY Event1.PointID, Event1.LogTimeStamp;
Got two tables:
tblJumper
JumperID
JumperName
and
tblWidthScored
ScoreID
fkJumperID
fkScoredWidth
Tables related by tblWidthScored.fkJumperID = tblJumper.JumperID
Table Contains following data:
tblJumper
1 Tom
2 Jerry
3 Bugs
tblWidthScored
1 1 5,72m
2 2 6,13m
3 1 5,80m
4 3 6,40m
5 2 6,30m
6 3 6,20m
What I'm trying to get is a list of each Jumpers personla best:
Tom 5,80m
Jerry 6,30m
Bugs 6,40m
Tried SELECT DISTINCT... in various forms but didn't succeed in any way.
Anyone couldl give a hint, please?
Thanks!
Although I think this is just a Join question and appropriate articles could be found on the web, I'll post one of the answers...
SELECT
J.JumperName,
S.Score
FROM tblJumper as J
INNER JOIN(
SELECT
fkJumperID,
Max(fkScoredWidth) as Score
FROM tblWidthScored
GROUP BY
fkJumperID
) as S on J.JumperID = S.fkJumperID
The hints you're asking for are in your actual question actually:
You're trying to find personal bests! That means some kind of aggregation: Maximum.
You're trying to find personal bests distinctly. Not "the best score amongst whole jumpers" but every person's personal best distinctly. That means some kind of distinction or grouping that you can partition the score datas people by people: Group By Clause
you can try this query. for this type of query, you have to use Group BY Clause with Sub-query.
SELECT JumperName, innertblWidthScored
FROM tblJumper INNER JOIN(SELECT fkJumperID,
Max(fkScoredWidth) as innertblWidthScored
FROM tblWidthScored
GROUP BY fkJumperID) WidthScored
on tblJumper.JumperID = WidthScored.fkJumperID
SQL FIDDLE DEMO
You could as well start with the JOIN, and do the GROUP BY afterwards. And if your sample output does actually suggest a sort order, you need an ORDER BY, too.
SELECT
JumperName
, MAX(fkScoredWidth) PersonalBest
FROM tblJumper
JOIN tblWidthScored
ON JumperID = fkJumperID
GROUP BY JumperName
ORDER BY MAX(fkScoredWidth) DESC;
SQL Fiddle
From result it seems that you want to retrieve jumper with its best score
try this -
select
a.jumperName,
b.bestScore
from tblJumper a, (
select
max(fkScoredWidth) as bestScore,
fkJumperID
from tblWidthScored
group by
fkJumperID
) b on b.fkJumperID = a.JumperID
order by
a.JumperID
I have been trying to get this to work for 12 hrs now and I cannot :-( Can someone please show me how I can get the ssnumber to group and get the total for each ssnumber.
Here is what I have now. In Table number 1 I have this code
SELECT
UNIT_NO, SUM(RATEB) AS TOTALRTE
FROM TABLE1
WHERE
TRUCK_PAID = 1
AND PICK_UP_DATE >= '(fromdate)'
AND PICK_UP_DATE <= '(todate)'
GROUP BY
UNIT_NO
ORDER BY
UNIT_NO
But table number 2 is where the ssnumber column is, so what I'm trying to do is the rateB sum from all of the loads for each unit_no and then group them and then go into table number 2 and group the ssnumber with the unit number from table number 1 and sum the rateB from table number 1.
Something like this (see below) but its not working :-(
SELECT
UNIT_NO, SUM(RATEB)
FROM
TABLE1
WHERE
TRUCK_PAID = 1
AND PICK_UP_DATE >= '(fromdate)'
AND PICK_UP_DATE <= '(todate)'
GROUP BY
UNIT_NO
JOIN
TABLE TABLE1.UNIT_NO = TABLE2.UNIT_NO GROUP BY TABLE2.SS_NUM
or
SELECT
UNIT_NO, SUM(RATEB) AS TOTALRATE
FROM
TABLE1
GROUP BY
UNIT_NO
JOIN
TRUCKS ON (TABLE1.UNIT_NO = TABLE2.UNIT_NO)
GROUP BY
TABLE2.SSNUMBER
Thank you guys so much for any help...
As requested, it is hard to really understand what you are trying to accomplish without more info about table2 and maybe an example of what you are expecting. However, what I got from your description is that you are trying to accomplish something like this?
SELECT UNIT_NO, TOTALRTE, TOTALLDSRTE
FROM
(
SELECT UNIT_NO,SUM(RATEB) AS TOTALRTE
FROM LOADS
GROUP BY UNIT_NO
) AS tbl1
JOIN
(
SELECT SS_NUM, SUM(RATEB) AS TOTALLDSRTE
FROM LOADS
GROUP BY SS_NUM
) AS tbl2
ON tbl1.UNIT_NO = tbl2.SS_NUM
I would suggest instead of getting data from two select queries in one select query, try to fetch them as separate queries. This saves a lot of time. That, or you can create a table for the result and update the result of each query into the table.
So I have a table containing different elements and dates.
It basically looks like this:
actieElement beginDatum
1 1/01/2010
1 1/01/2010
1 10/01/2010
2 1/02/2010
2 3/02/2010
What I now need is the smallest date for every actieElement.
I've found a solution using a simple GROUP BY statement, but that way the query loses its scope and you can't change anything anymore.
Without the GROUP BY statement I get multiple dates for every actieElement because certain dates are the same.
I thought of something like this, but it also does not work as it would give the subquery more then 1 record:
SELECT s1.actieElement, s1.begindatum
FROM tblActieElementLink AS s1
WHERE (((s1.actieElement)=(SELECT TOP 1 (s2.actieElement)
FROM tblActieElementLink s2
WHERE s1.actieElement = s2.actieElement
ORDER BY s2.begindatum ASC)));
Try this
SELECT s1.actieElement, s1.begindatum
FROM tblActieElementLink AS s1
WHERE s1.begindatum =(SELECT MIN(s2.begindatum)
FROM tblActieElementLink s2
WHERE s1.actieElement = s2.actieElement
);
SELECT DISTINCT T1.actieElement, T1.beginDatum
FROM tblActieElementLink AS T1
INNER JOIN (
SELECT T2.actieElement,
MIN(T2.beginDatum) AS smallest_beginDatum
FROM tblActieElementLink AS T2
GROUP
BY T2.actieElement
) AS DT1
ON T1.actieElement = DT1.actieElement
AND T1.beginDatum = DT1.smallest_beginDatum;
Add a DISTINCT clause to your SELECT.