I am working on a small reporting application. I have two tables
Agent Table Data
AgentID AgentName
------- ---------
1001 ABC
1002 XYZ
1003 POI
1004 JKL
Report Table Data
ReportID AgentId Labor Mandays Amount SubmitDate
-------- ------- ----- ------- ------ ----------
1 1001 30 10 5000 11/12/2011
2 1001 44 18 8000 11/14/2011
3 1002 33 75 3022 11/12/2011
4 1001 10 10 1500 11/14/2011
5 1002 10 10 1800 11/14/2011
6 1001 10 10 1400 11/14/2011
7 1003 40 40 1500 11/14/2011
8 1003 40 40 1800 11/14/2011
I want to generate a report which gives us output like
ReportID AgentId Labor Mandays Amount SubmitDate
-------- ------- ----- ------- ------ ----------
1 1001 30 10 5000 11/12/2011
3 1002 33 75 3022 11/12/2011
6 1001 10 10 1400 11/14/2011
5 1002 10 10 1800 11/14/2011
8 1003 40 40 1800 11/14/2011
Thanks in Advance
You didn't mention what VERSION of SQL Server you're using - if you're on 2005 or newer, you can use a CTE (Common Table Expression) with the ROW_NUMBER function:
;WITH LastPerAgent AS
(
SELECT
AgentID, ReportID, Labor, Mandays, Amount, SubmitDate,
ROW_NUMBER() OVER(PARTITION BY AgentID,SubmitDate
ORDER BY ReportID DESC) AS 'RowNum'
FROM dbo.Report
)
SELECT
AgentID, ReportID, Labor, Mandays, Amount, SubmitDate,
FROM LastPerAgent
WHERE RowNum = 1
This CTE "partitions" your data by AgentID and SubmitDate, and for each partition, the ROW_NUMBER function hands out sequential numbers, starting at 1 and ordered by ReportID DESC - so the "last" row (with the highest ReportID) for each (AgentID, SubmitDate) pair gets RowNum = 1 which is what I select from the CTE in the SELECT statement after it.
PS: this doesn't work 100% on your input data, since you've not defined how to group and how to eliminate rows.... you might need to adapt this query a bit, based on your requirements...
Related
SQL written by me:
select distinct resortid,sum(adultcount+childcount)"TOTAL GUEST"
from booking
having count(1)>=1
group by resortid
order by resortid;
Output:
RESORTID TOTAL GUEST
---------- -----------
1001 11
1002 10
1003 2
1004 2
Expected Output:
RESORTID TOTAL GUEST
---------- -----------
1002 10
1003 2
1004 2
Remove the distinct. Change the having to do what you want.
select resortid,sum(adultcount+childcount)"TOTAL GUEST"
from booking
having sum(adultcount+childcount) BETWEEN 1 AND 10
group by resortid
order by resortid;
I have a data set that looks like this:
Key TypeSeq Type Duration
-------------------------------------
29671461 10 1001 4
29671461 20 1002 2
29671461 30 1003 0
29671461 40 1004 0
29671461 70 1007 261
29671463 10 1001 3
29671463 20 1002 5
29671463 30 1003 7
29671463 40 1004 8
29671463 70 1007 261
I have found this but I am trying to group by ID rather that sum by it
select t1.id, t1.SomeNumt, SUM(t2.SomeNumt) as sum
from #t t1
inner join #t t2 on t1.id >= t2.id
group by t1.id, t1.SomeNumt
order by t1.id
I need a 5th column that does a cumulative total by key column
You can use sum() as a window function. If an order by is used together with that, you get a cumulative sum:
select "Key", TypeSeq, type, duration,
sum(duration) over (partition by "Key" order by TypeSeq) as sum
from the_table
order by "Key", TypeSeq;
Lets say I have the following 2 datasets:
A B C
----------- ----------- -----------
1 100 1000
1 120 1001
2 140 1002
2 160 1003
3 180 1004
3 190 1005
3 200 1006
and
A D E
----------- ----------- -----------
1 61 2000
1 62 2001
1 63 2001
2 64 2002
3 65 2004
3 66 2005
3 67 2006
3 68 2006
Is it possible to generate the following output dataset (. represents null)?
A B C D E
----------- ----------- ----------- ----------- -----------
1 100 1000 61 2000
1 120 1001 62 2001
1 . . 63 2001
2 140 1002 64 2002
2 160 1003 . .
3 180 1004 65 2004
3 190 1005 66 2005
3 200 1006 67 2006
3 . . 68 2006
The merge takes all the records from both tables, and adds them to the result set at most once.
If records join they are not multiplied as in a classical sql join. Each record is aligned with a matching record and when they run out of records null is inserted.
I've been thinking that perhaps the new partitioning functions can achieve this, but I've been away from SQL too long now and I cant think of a way to do this "special join".
I've also considered making a distinct list of the keys and then left joining them to the 2 tables, but then I get stuck, because the join will still multiply the record counts..
You can do this with the row_number() windowing function. Naming the two datasets DS1 and DS2 the result will look like this:
WITH DS1Seq As (
SELECT A, B, C, row_number() OVER (partition by A order by A, B, C) As SeqNumber
FROM DS1
),
DS2Seq As (
SELECT A, D, E, row_number() OVER (partition by A order by A, D, E) As SeqNumber
FROM DS2
)
SELECT coalesce(DS1Seq.A, DS2Seq.A) As A, B, C, D, E
FROM DS1Seq
FULL JOIN DS2Seq on DS1Seq.A = DS2Seq.A AND DS1Seq.SeqNumber = DS2Seq.SeqNumber
Using the following tables,
Productivity:
PRODUCTIVITYID PDATE EMPLOYEEID ROOMID ROOMS_SOLD SCR
81 03/26/2016 7499 21 56 43
82 03/26/2016 7566 42 - -
102 03/26/2016 7499 22 434 22
101 03/26/2016 7566 21 43 53
ProductivityD:
PRODUCTIVITYID WORKHRS MEALPANELTY DESCRIPTION
2 50 4 -
21 6.4 1 -
102 6 - -
81 1.32 - -
101 3.6 - -
Rooms:
ID ROOM PROPERTCODE
22 102 6325
41 103 6325
42 104 6325
43 105 6325
EMP:
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7566 JONES MANAGER 7839 04/02/1981 2975 - 20
7788 SCOTT ANALYST 7566 12/09/1982 3000 - 20
7902 FORD ANALYST 7566 12/03/1981 3000 - 20
7369 SMITH CLERK 7902 12/17/1980 800 - 20
7499 ALLEN SALESMAN 7698 02/20/1981 1600 300 30
The following query is generating below output but I need to group employees and sum workhrs and then pivot RM_ROOM and RM_SCR
WITH pivot_data AS (
SELECT eNAME,workhrs,room, 'RM' as RM,SCR from PRODUCTIVITY p,PRODUCTIVITYd d, emp e, ROOMS R
where p.PRODUCTIVITYID=d.PRODUCTIVITYID and e.empno=p.employeeid
AND R.ID=P.ROOMID
)
SELECT *
FROM pivot_data
PIVOT (
MIN(room) as room,min(scr) as SCR --<-- pivot_clause
FOR RM--<-- pivot_for_clause
IN ('RM') --<-- pivot_in_clause
)
Current Output:
ENAME WORKHRS 'RM'_ROOM 'RM'_SCR
JONES 3.6 101 53
ALLEN 6 102 22
ALLEN 1.32 101 43
Desired Output:
ENAME WORKHRS 'RM'_ROOM 'RM'_SCR 'RM'_ROOM 'RM'_SCR
JONES 3.6 101 53 - -
ALLEN 7.32 101 43 102 22
You are pivoting on a fixed value, the string literal 'RM', so you're really not doing anything useful in the pivot - the output is the same as you'd get from running the 'pivot_data' query on its own:
SELECT eNAME,workhrs,room, SCR from PRODUCTIVITY p,PRODUCTIVITYd d, emp e, ROOMS R
where p.PRODUCTIVITYID=d.PRODUCTIVITYID and e.empno=p.employeeid
AND R.ID=P.ROOMID;
ENAME WORKHRS ROOM SCR
----- ---------- ---------- ----------
JONES 3.6 101 53
ALLEN 1.32 101 43
ALLEN 6 102 22
You want the aggregate workhrs for each employee, and a pivot of the rooms they sold. If you change that query to get the analytic sum of workhrs and a ranking of the room/scr values (and using modern join syntax) you get:
select e.ename, r.room, p.scr,
sum(d.workhrs) over (partition by e.ename) as wrkhrs,
rank() over (partition by e.ename order by r.room, p.scr) as rnk
from productivity p
join productivityd d on d.productivityid = p.productivityid
join emp e on e.empno=p.employeeid
join rooms r on r.id = p.roomid;
ENAME ROOM SCR WRKHRS RNK
----- ---------- ---------- ---------- ----------
ALLEN 101 43 7.32 1
ALLEN 102 22 7.32 2
JONES 101 53 3.6 1
You can then pivot on that generated rnk number:
with pivot_data as (
select e.ename, r.room, p.scr,
sum(d.workhrs) over (partition by e.ename) as wrkhrs,
rank() over (partition by e.ename order by r.room, p.scr) as rnk
from productivity p
join productivityd d on d.productivityid = p.productivityid
join emp e on e.empno=p.employeeid
join rooms r on r.id = p.roomid
)
select *
from pivot_data
pivot (
min(room) as room, min(scr) as scr --<-- pivot_clause
for rnk --<-- pivot_for_clause
in (1, 2, 3) --<-- pivot_in_clause
);
ENAME WRKHRS 1_ROOM 1_SCR 2_ROOM 2_SCR 3_ROOM 3_SCR
----- ---------- ---------- ---------- ---------- ---------- ---------- ----------
ALLEN 7.32 101 43 102 22
JONES 3.6 101 53
You need to know the maximum number of rooms any employee may have - i.e. the highest rnk could ever be - and include all of those in the in clause. Which means you're likely to end up with empty columns, as in this example where there is no data for 3_room or 3_scr. You can't avoid that though, unless you get an XML result or generate the query dynamically.
What you are saying makes no sense. What do you mean by "pivot RM_ROOM"? So I have to guess. I am guessing you want to group employees and sum workhrs, and then pivot the result. The "Output" you show seems to be the output for pivot_data, your subquery.
Your answer will only have eNAME and for each of them, a count of hours worked. So you don't need to SELECT the room numbers in the pivot_data subquery. You only need eNAME and workhrs. Then it is a simple matter of using the PIVOT syntax:
WITH pivot_data AS (
SELECT eNAME, workhrs FROM PRODUCTIVITY p,PRODUCTIVITYd d, emp e, ROOMS R
where p.PRODUCTIVITYID=d.PRODUCTIVITYID and e.empno=p.employeeid
AND R.ID=P.ROOMID
)
SELECT *
FROM pivot_data
PIVOT (
SUM(workhrs)
FOR eNAME IN ('JONES', 'ALLEN')
)
/
Output:
'JONES' 'ALLEN'
---------- ----------
3.6 7.32
I have a TableA
ID MatCh01 Match02 Status
1 1001 12
2 1001 12
3 1001 12
4 1002 44
5 1002 47
6 1003 22
7 1003 22
8 1004 55
9 1004 57
I want to populate column = status with "FAIL" when :
For same match01, there exist different match02. Expected TableA :
ID MatCh01 Match02 Status
1 1001 12 NULL
2 1001 12 NULL
3 1001 12 NULL
4 1002 44 FAIL
5 1002 47 FAIL
6 1003 22 NULL
7 1003 22 NULL
8 1004 55 FAIL
9 1004 57 FAIL
Please NOTE: FAIL all 'match01' if its corresponding 'match02' is different.
Thanks
Basically this says Update all Values in TableA when the MAX and MIN of Column Match02 are not equal (meaning match01 has multiple rows with different values for match 02).
UPDATE A
SET Status = 'FAIL'
FROM TableA A
INNER JOIN (SELECT
a2.Match01
FROM TableA A2
GROUP BY a2.Match01
HAVING MAX(Match02) <> MIN(Match02)) B ON
A.Match01 = B.Match01
When there's more than one distinct value of match02 for any match01, update those rows with the same match01.
UPDATE t1
SET Status = 'FAIL'
FROM TableA t1
WHERE t1.Match01 in
(
SELECT t2.Match01
FROM TableA t2
GROUP BY t2.Match01
HAVING COUNT(DISTINCT t2.Match02) > 1
)