Grouping Data TSQL - forms

I'm trying to make a report which shows a users submitted forms.
I want each line to show one occurrence of each filed per user.
Username | First form submitted | Form address (1st form) | Last form submitted | Form Address (last form)
Here's what I have currently:
SELECT form.Name
,(SELECT COUNT (*) FROM dbo.vAdvF_155 af WHERE af.Name = form.Name) AS [TotalForms]
,(SELECT TOP 1 p.Timetag1 FROM dbo.vAdvF_155 af WHERE af.Name = form.Name ORDER BY [TimeTag1] ASC) AS [Started]
,(SELECT TOP 1 af.aField143 FROM dbo.vAdvF_155 af WHERE af.TimeTag1 = form.TimeTag1 ORDER BY [TimeTag1] DESC) AS [FirstFormAddress]
,(SELECT TOP 1 p.Timetag2 FROM dbo.vAdvF_155 af WHERE af.Name = form.Name ORDER BY [TimeTag2] DESC) AS [Submitted]
,(SELECT TOP 1 af.aField143 FROM dbo.vAdvF_155 af WHERE af.RecId = form.RecId ORDER BY [TimeTag2] DESC) AS [LastFormAddress]
FROM dbo.vAdvF_155 AS form INNER JOIN
dbo.PhoneData AS p ON form.RecId = p.RecID
ORDER BY form.Name
Results:
Name TotalForms Started FirstFormAddress Submitted LastFormAddress
CARL SUTTON 14 2016-07-22 09:30:55.000 19 Lilac Close KEYWORTH 2016-07-22 11:17:36.000 19 Lilac Close KEYWORTH
CARL SUTTON 14 2016-07-22 12:46:31.000 23 Lincoln Street NEWARK 2016-07-22 13:20:19.000 23 Lincoln Street NEWARK
CARL SUTTON 14 2016-07-25 10:24:52.000 104 Shireoaks COMMON 2016-07-25 12:04:59.000 104 Shireoaks COMMON
CARL SUTTON 14 2016-07-25 13:59:11.000 43 Milton DRIVE RAVENSHEAD 2016-07-25 15:53:28.000 43 Milton DRIVE RAVENSHEAD
CARL SUTTON 14 2016-07-26 10:22:53.000 17 LISMORE COURT MANSFIELD 2016-07-26 11:36:07.000 17 LISMORE COURT MANSFIELD
CARL SUTTON 14 2016-07-26 13:52:02.000 3 Ruby's AVENUE BALDERTON 2016-07-26 15:51:42.000 3 Ruby's AVENUE BALDERTON
CARL SUTTON 14 2016-07-27 09:35:54.000 The Elms Station Road NG14 7GD 2016-07-27 14:53:28.000 The Elms Station Road NG14 7GD
CARL SUTTON 14 2016-07-28 09:09:10.000 Main Road BULCOTE 2016-07-28 10:35:17.000 Main Road BULCOTE
CARL SUTTON 14 2016-07-28 12:04:17.000 NULL 2016-07-28 12:06:21.000 NULL
CARL SUTTON 14 2016-07-28 13:13:48.000 2 Midlands AVENUE STAPLEFORD 2016-07-28 15:14:32.000 2 Midlands AVENUE STAPLEFORD
CARL SUTTON 14 2016-07-31 08:14:03.000 Summit Close KIRKBY 2016-07-31 11:44:32.000 Summit Close KIRKBY
CARL SUTTON 14 2016-07-31 12:49:29.000 4 Archway Old Clipstone 2016-07-31 14:07:05.000 4 Archway Old Clipstone
CARL SUTTON 14 2016-08-01 08:20:21.000 5 RAVENSHEAD COURT 2016-08-01 10:08:39.000 5 RAVENSHEAD COURT
CARL SUTTON 14 2016-08-02 07:56:23.000 Field CLOSE GEDLING 2016-08-02 09:48:13.000 Field CLOSE GEDLING
CASEY MORTON 13 2016-07-22 09:12:08.000 10 ByRON CRESENT Ng162sx 2016-07-22 11:42:30.000 10 ByRON CRESENT Ng162sx
CASEY MORTON 13 2016-07-22 13:27:12.000 146 2016-07-22 16:05:29.000 146
CASEY MORTON 13 2016-07-25 09:14:37.000 5 Cossall Road NG93PG 2016-07-25 14:16:53.000 5 Cossall Road NG93PG
CASEY MORTON 13 2016-07-26 08:28:14.000 TenYSON Street NG74GA 2016-07-26 11:46:42.000 TenYSON Street NG74GA
CASEY MORTON 13 2016-07-26 14:31:17.000 NULL 2016-07-26 14:54:23.000 NULL
CASEY MORTON 13 2016-07-27 06:38:28.000 34 Sturton STREET Ng76hu 2016-07-27 09:24:37.000 34 Sturton STREET Ng76hu
CASEY MORTON 13 2016-07-27 09:59:05.000 12 TUDOR Close Ng42dr 2016-07-27 15:04:08.000 12 TUDOR Close Ng42dr
CASEY MORTON 13 2016-07-28 08:43:23.000 12 Ardmore Ng24gp 2016-07-28 11:27:35.000 12 Ardmore Ng24gp
CASEY MORTON 13 2016-07-28 11:48:28.000 9 Sycamore Close NG122DJ 2016-07-28 13:46:55.000 9 Sycamore Close NG122DJ
CASEY MORTON 13 2016-07-28 14:40:49.000 15 GoodLIFFE STREET Ng76fz 2016-07-28 15:54:07.000 15 GoodLIFFE STREET Ng76fz
CASEY MORTON 13 2016-08-01 09:50:08.000 24 VALESIDE Gardens NG42EP 2016-08-01 12:28:27.000 24 VALESIDE Gardens NG42EP
CASEY MORTON 13 2016-08-01 13:51:53.000 285 Derby Road Ng93ja 2016-08-01 16:09:11.000 285 Derby Road Ng93ja
CASEY MORTON 13 2016-08-02 07:21:38.000 Melrose House Raleigh Street Ng74hf 2016-08-02 11:24:27.000 Melrose House Raleigh Street Ng74hf
As you can see with the results it shows all the forms as they are not distinct, I literally just want it grouped into one line per person but can't figure the grouping.

Assuming a lot about the structure of your vAdvF_155 table/view, you may be able to get the results you are looking for with this:
declare #vAdvF_155 table (Name nvarchar(50)
,aField143 nvarchar(50)
,Timetag1 datetime
,Timetag2 datetime
)
insert into #vAdvF_155 values
('CARL SUTTON' ,'4 Archway Old Clipstone' ,'2016-07-31 12:49:29.000','2016-07-31 14:07:05.000')
,('CARL SUTTON' ,'5 RAVENSHEAD COURT' ,'2016-08-01 08:20:21.000','2016-08-01 10:08:39.000')
,('CARL SUTTON' ,'Field CLOSE GEDLING' ,'2016-08-02 07:56:23.000','2016-08-02 09:48:13.000')
,('CASEY MORTON','10 ByRON CRESENT Ng162sx' ,'2016-07-22 09:12:08.000','2016-07-22 11:42:30.000')
,('CASEY MORTON','146 Street Name' ,'2016-07-22 13:27:12.000','2016-07-22 16:05:29.000')
,('CASEY MORTON','5 Cossall Road NG93PG' ,'2016-07-25 09:14:37.000','2016-07-25 14:16:53.000')
select f.Name
,f.TotalForms
,f.FirstForm
,ff.aField143 as FirstFormAddress
,f.LastForm
,lf.aField143 as LastFormAddress
from (
select Name
,count(1) as TotalForms
,min(Timetag1) as FirstForm
,max(Timetag2) as LastForm
from #vAdvF_155
group By Name
) f
inner join #vAdvF_155 ff
on(f.Name = ff.Name
and f.FirstForm = ff.Timetag1
)
inner join #vAdvF_155 lf
on(f.Name = ff.Name
and f.LastForm = lf.Timetag2
)

Related

PostgreSQL Lag Function between of Two Tables

I am new to Postgresql/Python, so please bear with me!
Assuming we have two tables:
item table having a itemid, price, time.
user table having colums userid, itemid, timecreated, quantity, firstprice, lastprice, difference.
Table examples like :
item table:
itemid price time
RBK 92 1546408800
LBV 51 1546408800
ZBT 49 1546408800
GLS 22 1546408800
DBC 17 1546408800
RBK 91 1546495200
LBV 55 1546495200
ZBT 51 1546495200
GLS 24 1546495200
DBC 28 1546581600
RBK 108 1546581600
LBV 46 1546581600
ZBT 49 1546581600
GLS 21 1546581600
DBC 107 1546581600
In item table all those values comes up with api.
and user table:
userid itemid timecreated quantitty firstprice currentprice difference
1 RBK 1546408800 20
2 RBK 1546408800 15
3 RBK 1546408800 35
3 GLS 1546408800 101
3 DBC 1546495200 140
1 RBV 1546495200 141
2 RBK 1546495200 25
2 RBV 1546581600 31
User table is djangobased table which is user can register\add new items to follow prices.
My struggle access the item table to fetch first price which is having a same timestamp. In that example userid 1 RBK First price (1546408800) must be filling with 92
I did some trick with postgresql with (lag) But this does not seems to be working:
update user
set firstprice = tt.prev_price
from (select item.*,
lag(price) over (partition by itemid order by time) as prev_price
from item
) tt
where tt.id = item.id and
tt.prev_close is distinct from item.price;
I can call current price from the api but didnt find out the way to filling firstprice from the item table. I will be making for a trigger for this query. I searched on google and on stackoverflow but I couldn't find anything that could help me. Thanks in advance.
I can advice next approach (may be not fastest):
update "user" set firstprice = (
select price from "item" i
where i.itemid = "user".itemid and i.time >= "user".timecreated order by i.time limit 1
);
It calculate firstprice using sub-query. Test this SQL here

Flatten one column keeping others in POSTGRESQL

I created a view which is presently giving the data like this:
practice_name message_type message_count
CHC ALOG_SYNC 1
CHC BULKNT 0
CHC PIE_SYNC 1
CHC PPRV_SYNC 1
CHC SYNC_PRACT 3
CHC SYNC_PROV 9
CHC SYNC_WTXT 3
CHC SYNC_XYZ 0
Midtown ALOG_SYNC 0
Midtown BULKNT 0
Midtown PIE_SYNC 0
Midtown PPRV_SYNC 0
Midtown SYNC_PRACT 3
Midtown SYNC_PROV 0
Midtown SYNC_WTXT 3
Midtown SYNC_XYZ 0
NextGen MedicalPractice ALOG_SYNC 0
NextGen MedicalPractice BULKNT 1
NextGen MedicalPractice PIE_SYNC 0
NextGen MedicalPractice PPRV_SYNC 0
NextGen MedicalPractice SYNC_PRACT 3
NextGen MedicalPractice SYNC_PROV 591
NextGen MedicalPractice SYNC_WTXT 3
NextGen MedicalPractice SYNC_XYZ 0
My View:
CREATE OR REPLACE VIEW sha.sha_export_queue_view AS
SELECT q3.practice_name,
q3.message_type,
q3.share_site_org_key,
COALESCE(q2.message_count, '0'::text) AS message_count
FROM ( SELECT q1.practice_name,
mt.message_type,
q1.share_site_org_key
FROM sha.message_types mt,
( SELECT DISTINCT jsonb_array_elements((ai.result_json -> 'Patient Portal Operational Information'::text) -> 'nxmd_export contents by message type'::text) ->> 'Practice Name'::text AS practice_name,
ai.share_site_org_key
FROM sha.sha_share_site_view ssv
LEFT JOIN ( SELECT mytable2.assessment_id,
mytable2.result_json,
mytable2.share_site_org_key,
mytable2.rnk
FROM ( SELECT assessment_info.assessment_id,
assessment_info.result_json,
assessment_info.share_site_org_key,
dense_rank() OVER (PARTITION BY assessment_info.share_site_org_key ORDER BY assessment_info.modified_datetime DESC) AS rnk
FROM sha.assessment_info
WHERE assessment_info.assessment_id = 8::numeric) mytable2
WHERE mytable2.rnk = 1) ai ON ssv.share_site_org_key = ai.share_site_org_key) q1) q3
LEFT JOIN ( SELECT jsonb_array_elements((ai.result_json -> 'Patient Portal Operational Information'::text) -> 'nxmd_export contents by message type'::text) ->> 'Practice Name'::text AS practice_name,
jsonb_array_elements((ai.result_json -> 'Patient Portal Operational Information'::text) -> 'nxmd_export contents by message type'::text) ->> 'Message Type'::text AS message_type,
jsonb_array_elements((ai.result_json -> 'Patient Portal Operational Information'::text) -> 'nxmd_export contents by message type'::text) ->> 'Message Count'::text AS message_count
FROM sha.sha_share_site_view ssv
LEFT JOIN ( SELECT mytable2.assessment_id,
mytable2.result_json,
mytable2.share_site_org_key,
mytable2.rnk
FROM ( SELECT assessment_info.assessment_id,
assessment_info.result_json,
assessment_info.share_site_org_key,
dense_rank() OVER (PARTITION BY assessment_info.share_site_org_key ORDER BY assessment_info.modified_datetime DESC) AS rnk
FROM sha.assessment_info
WHERE assessment_info.assessment_id = 8::numeric) mytable2
WHERE mytable2.rnk = 1) ai ON ssv.share_site_org_key = ai.share_site_org_key) q2 ON q3.message_type::text = q2.message_type AND q3.practice_name = q2.practice_name
ORDER BY q3.practice_name;
I want the second column to be flattened:
Practice Time Stamp <<message type 1>> <<message type 2>> <<message type 3>> <<message type 4 >> <<message type 5>> <<message type 6>> <<message type 7>> <<message type 8>>
Practice Name 1 21-12-2016 10:00 23 25 27 29 31 33 35 37
Practice Name 2 21-12-2016 10:00 24 26 28 30 32 34 36 38
Practice Name 3 21-12-2016 13:00 25 27 29 31 33 35 37 39
Practice Name 4 21-12-2016 13:00 26 28 30 32 34 36 38 40
Practice Name 5 24-12-2016 13:00 27 29 31 33 35 37 39 41
Practice Name 6 27-12-2016 13:00 28 30 32 34 36 38 40 42
Practice Name 7 30-12-2016 13:00 29 31 33 35 37 39 41 43
Practice Name 8 02-01-2017 13:00 30 32 34 36 38 40 42 44
Practice Name 1 05-01-2017 13:00 31 33 35 37 39 41 43 45
Practice Name 2 08-01-2017 13:00 32 34 36 38 40 42 44 46
Practice Name 3 11-01-2017 13:00 33 35 37 39 41 43 45 47
Is there any way I can achieve that?
Sorry for the little alignment issue.
The values are corresponding message type values
Sample for query (idea in comments to question):
SELECT
practice_name,
sum("ALOG_SYNC") AS "ALOG_SYNC",
sum("BULKNT") AS "BULKNT",
...
FROM (
SELECT
practice_name,
CASE WHEN q3.message_type = 'ALOG_SYNC' THEN sum(message_count) END AS "ALOG_SYNC",
CASE WHEN q3.message_type = 'BULKNT' THEN sum(message_count) END AS "BULKNT"
FROM
<your from + where clause>
) AS A
GROUP BY 1
Probably your query might be optimised.
Or you can use crosstab function (https://www.postgresql.org/docs/current/static/tablefunc.html)

Getting depth-first traversal insted of breadth first in T-SQL

I have the following T-SQL function: https://gist.github.com/cwattengard/11365802
This returns data in a breadth-first traversal. Is there a simple way to make this function return its data in a depth-first traversal? I have a treeview-component that excpects this (legacy system).
I already have a similar stored procedure that returns the tree in a depth-first traversal, but it's using cursors and is really slow. (6-7 seconds as opposed to this function that takes less than a second on the same data).
I think I just had a eureka moment. If I add the Path variable already supplied by the CTE, and sort by that, I get what I want. The OrgID is a unique ID. So ordering by it would make it sort by the expected output for the user (chronologically) and be depth-first for the treeview.
http://sqlanywhere.blogspot.in/2012/10/example-recursive-union-tree-traversal.html
Here's a diagram showing the primary keys for a tree-structured table:
1
|
---------------------------------------
2 93 4 5
| | | |
-------------- ------------ -------- ------
6 7 8 9 10 11 12 13 14 15 16 17 18 19
| | | |
----- ----- ----- -----
27 26 25 24 23 22 21 20
Here's what the breadth-first and depth-first queries should return:
Breadth-First Depth-First
1 1
2 2
93 6
4 7
5 27
6 26
7 8
8 9
9 10
10 93
11 11
12 12
13 13
14 25
15 24
16 14
17 4
18 15
19 16
27 17
26 23
25 22
24 5
23 18
22 21
21 20
20 19
If you order the output by tekst will that do it?
First populate a table variable #unsorted inside the function; then finally return Select * from #unsorted order by tekst?
I know this is very late to the game, but seems like you could use hierarchyid to get a nice depth first search...
The github file referenced in the op appears to have gone missing, but the basic formula is
Put hierarchyid::GetRoot() as Foo in your CTE anchor query
Put cast (cte.Foo.ToString() + cast(row_number() over(order by ) as varchar) + '/' as hierarchyid) as Foo in your recursive query
order By Foo when you invoke the CTE
and the results come out depth first

Subselect and Max

Alright, I've been trying to conceptualize this for a better part of the afternoon and still cannot figure out how to structure this subselect.
The data that I need to report are ages for a given student major grouped by the past 3 fiscal years. Each fiscal year has 3 semesters (summer, fall, spring). I need to have my query grouped on the fiscalyear and agerange fields and then count the distinct student id's.
I currently have this for my SQL statement:
Select COUNT(distinct StuID), AgeRange, FiscalYear
from tblStatic
where Campus like 'World%' and (enrl_act like 'REG%' or enrl_act like 'SCH%')
and StuMaj = 'LAWSC' and FiscalYear IN ('09/10', '10/11', '11/12')
group by FiscalYear, AgeRange
order by FiscalYear, AgeRange
So this is all fine and dandy except it doesn't match my headcount of students for the fiscalyear. The reason being, that people may cross over in the age ranges during the fiscal year and is adding them to my count twice.
How can I use a subselect to resolve this duplicate entry? The field I have been trying to get working is my semester field and using a max to find the max semester during a fiscalyear for a given student.
Data Sample:
Count AgeRange FiscalYear
3 1 to 19 09/10
20 20 to 23 09/10
60 24 to 29 09/10
96 30 to 39 09/10
34 40 to 49 09/10
14 50 to 59 09/10
3 60+ 09/10
2 1 to 19 10/11
24 20 to 23 10/11
73 24 to 29 10/11
109 30 to 39 10/11
43 40 to 49 10/11
11 50 to 59 10/11
2 60+ 10/11
1 1 to 19 11/12
17 20 to 23 11/12
75 24 to 29 11/12
123 30 to 39 11/12
44 40 to 49 11/12
14 50 to 59 11/12
2 60+ 11/12
Solution: (Just got this working and produced my headcounts that match what they are suppose to be)
Select COUNT(distinct S.StuID), AR.AgeRange, S.FiscalYear
from tblStatic S
INNER JOIN
( Select S.StuID, MIN(AgeRange) as AgeRange
From tblStatic S
Group By S.StuID) AR on S.StuID=AR.StuID
where Campus like 'World%' and (enrl_act like 'REG%' or
enrl_act like 'SCH%')
and StuMaj = 'LAWSC' and FiscalYear IN ('09/10', '10/11', '11/12')
group by S.FiscalYear, AR.AgeRange
order by S.FiscalYear, AR.AgeRange
Replace each student's age range with its maximum (or minimum, if you like) age range that fiscal year, then count them:
;
WITH sourceData AS (
SELECT
StudID,
MaxAgeRangeThisFiscalYear = MAX(AgeRange) OVER
(PARTITION BY StudID, FiscalYear),
FiscalYear
FROM tblStatic
WHERE Campus LIKE 'World%'
AND (enrl_act LIKE 'REG%' OR enrl_act LIKE 'SCH%')
AND StuMaj = 'LAWSC'
AND FiscalYear IN ('09/10', '10/11', '11/12')
)
SELECT
FiscalYear,
AgeRange = MaxAgeRangeThisFiscalYear,
Count = COUNT(DISTINCT StudID)
FROM sourceData
GROUP BY
FiscalYear,
MaxAgeRangeThisFiscalYear
ORDER BY
FiscalYear,
MaxAgeRangeThisFiscalYear

T-SQL Merge records based on criteria

In SQL server 2008, I have below table Score. I want to show Score1 and Score2 together for same student. Name and Email combined uniquely identifies a student (Name or Email may be missing too, like Jack and Maya#moon.com). The expected output shown as T_Combined.
Score
Name Email Score1 Score2
John 'John#pluto.com' 75
Peter 'Peter#pluto.com' 34
Nina 'Nina#pluto.com' 45
Joseph 'Joseph#pluto.com' 76
Tom 'Tom#pluto.com' 43
Sam 'Sam#pluto.com' 76
Nancy 'Nancy#pluto.com' 12
Tina 'Tina#pluto.com' 56
John 'John#mars.com' 98
Peter 'Peter#mars.com' 12
Nina 'Nina#mars.com' 45
Joseph 'Joseph#mars.com' 87
Tom 'Tom#mars.com' 67
Sam 'Sam#mars.com' 99
Nancy 'Nancy#mars.com' 33
Tina 'Tina#mars.com' 23
John 'John#pluto.com' 86
Peter 'Peter#pluto.com' 56
Nina 'Nina#pluto.com' 98
Joseph 'Joseph#pluto.com' 78
Tom 'Tom#pluto.com' 12
Sam 'Sam#pluto.com' 45
Nancy 'Nancy#pluto.com' 76
Tina 'Tina#pluto.com' 78
John 'John#mars.com' 98
Peter 'Peter#mars.com' 45
Nina 'Nina#mars.com' 76
Joseph 'Joseph#mars.com' 12
Tom 'Tom#mars.com' 84
Sam 'Sam#mars.com' 27
Nancy 'Nancy#mars.com' 54
Tina 'Tina#mars.com' 50
Jack 10
'Maya#moon.com' 20
T_Combined
Name Email Score1 Score2
John 'John#pluto.com' 86
Peter 'Peter#pluto.com' 56
Nina 'Nina#pluto.com' 98
Joseph 'Joseph#pluto.com' 78
Tom 'Tom#pluto.com' 43 12
Sam 'Sam#pluto.com' 76 45
Nancy 'Nancy#pluto.com' 12
Tina 'Tina#pluto.com' 56
John 'John#mars.com' 98
Peter 'Peter#mars.com' 12
Nina 'Nina#mars.com' 45 76
Joseph 'Joseph#mars.com' 87 12
Tom 'Tom#mars.com' 67 84
Sam 'Sam#mars.com' 99 27
Nancy 'Nancy#mars.com' 33 54
Tina 'Tina#mars.com' 23 50
Jack 10
'Maya#moon.com' 20
Many thanks
Create table Score (Name varchar(20),Email varchar(20),Score1 int,Score2 int)
insert into Score (Name,Email,Score1)values('John','John#pluto.com',75)
insert into Score (Name,Email,Score1)values('Peter','Peter#pluto.com',34)
insert into Score (Name,Email,Score1)values('Nina','Nina#pluto.com',45)
insert into Score (Name,Email,Score1)values('Joseph','Joseph#pluto.com',76)
insert into Score (Name,Email,Score1)values('Tom','Tom#pluto.com',43)
insert into Score (Name,Email,Score1)values('Sam','Sam#pluto.com',76)
insert into Score (Name,Email,Score1)values('Nancy','Nancy#pluto.com',12)
insert into Score (Name,Email,Score1)values('Tina','Tina#pluto.com',56)
insert into Score (Name,Email,Score1)values('John','John#mars.com',98)
insert into Score (Name,Email,Score1)values('Peter','Peter#mars.com',12)
insert into Score (Name,Email,Score1)values('Nina','Nina#mars.com',45)
insert into Score (Name,Email,Score1)values('Joseph','Joseph#mars.com',87)
insert into Score (Name,Email,Score1)values('Tom','Tom#mars.com',67)
insert into Score (Name,Email,Score1)values('Sam','Sam#mars.com',99)
insert into Score (Name,Email,Score1)values('Nancy','Nancy#mars.com',33)
insert into Score (Name,Email,Score1)values('Tina','Tina#mars.com',23)
insert into Score (Name,Email,Score2)values('John','John#pluto.com',86)
insert into Score (Name,Email,Score2)values('Peter','Peter#pluto.com',56)
insert into Score (Name,Email,Score2)values('Nina','Nina#pluto.com',98)
insert into Score (Name,Email,Score2)values('Joseph','Joseph#pluto.com',78)
insert into Score (Name,Email,Score2)values('Tom','Tom#pluto.com',12)
insert into Score (Name,Email,Score2)values('Sam','Sam#pluto.com',45)
insert into Score (Name,Email,Score2)values('Nancy','Nancy#pluto.com',76)
insert into Score (Name,Email,Score2)values('Tina','Tina#pluto.com',78)
insert into Score (Name,Email,Score2)values('John','John#mars.com',98)
insert into Score (Name,Email,Score2)values('Peter','Peter#mars.com',45)
insert into Score (Name,Email,Score2)values('Nina','Nina#mars.com',76)
insert into Score (Name,Email,Score2)values('Joseph','Joseph#mars.com',12)
insert into Score (Name,Email,Score2)values('Tom','Tom#mars.com',84)
insert into Score (Name,Email,Score2)values('Sam','Sam#mars.com',27)
insert into Score (Name,Email,Score2)values('Nancy','Nancy#mars.com',54)
insert into Score (Name,Email,Score2)values('Tina','Tina#mars.com',50)
insert into Score (Name,Score1)values('Jack',10)
insert into Score (Email,Score2)values('Maya#moon.com',20)
select Name, Email, isnull(SUM(Score1),'') as Score1, isnull(SUM(Score2),'') as Score2
from Score
group by Name, Email