查询XML RAW

时间:2017-05-16 17:13:51

标签: sql sql-server xml tsql aggregate

我正在使用transact SQL,我得到了这些表......

main.locality - id,locality
main.job - id,jobs
main.person - name,surname,jobid,localityid,salary,gender

我需要获得此输出(XML RAW)PS。 第一行是所有工作的平均工资 第二行是所有女性的平均工资 第三行是所有男性的平均工资 第四行是该工作和女性的平均工资 第五行是该工作和男性的平均工资

<row Average="19827" /> 
<row gender="F" Average="19835" /> 
<row gender="M" Average="19819" /> 
<row job="C# Developer" Average="20941" /> 
<row job="C# Developer" gender="F" Average="20845" />
<row job="C# Developer" gender="M" Average="21027" /> 

有人可以帮我查询吗?

2 个答案:

答案 0 :(得分:1)

我在想:

-- 1. SAMPLE DATA
DECLARE @locality TABLE (localityid int, locality int);
DECLARE @job TABLE (id int, Job varchar(100));
DECLARE @person TABLE 
(
  name varchar(100), 
  surname varchar(100),
  jobid int,
  localityid int,
  salary money,
  gender char(1)
);

INSERT @locality VALUES (1, 10), (2, 20), (3, 35), (4, 1000);
INSERT @job VALUES (1, 'C# Developer'), (2, 'DBA');
INSERT @person VALUES 
('joe', 'blow', 1, 1, 100000, 'M'),
('Sally', 'Doe', 1, 2, 135000, 'F'),
('Sue', 'Smith', 2, 2, 115000, 'F'),
('Kyle', 'Jones', 2, 3, 107500, 'M')
; 

-- 2. SOLUTION:
SELECT gender = NULL, job = NULL, average = AVG(p.salary)
FROM @person p
UNION ALL
SELECT p.gender, job = NULL, average = AVG(p.salary) 
FROM @person p
JOIN @job j ON p.jobid = j.id
GROUP BY p.gender
UNION ALL
SELECT gender = NULL, job = j.job, average = AVG(p.salary) 
FROM @person p
JOIN @job j ON p.jobid = j.id
GROUP BY j.Job
UNION ALL
SELECT p.gender, j.Job, average = AVG(p.salary)
FROM @person p
JOIN @job j ON p.jobid = j.id
GROUP BY p.gender, j.Job
FOR XML RAW;

返回:

<row average="114375.0000" />
<row gender="F" average="125000.0000" />
<row gender="M" average="103750.0000" />
<row job="C# Developer" average="117500.0000" />
<row job="DBA" average="111250.0000" />
<row gender="F" job="C# Developer" average="135000.0000" />
<row gender="M" job="C# Developer" average="100000.0000" />
<row gender="F" job="DBA" average="115000.0000" />
<row gender="M" job="DBA" average="107500.0000" />

答案 1 :(得分:1)

这是a job for ROLLUP

(我借用了Alan Burstein的样本数据 - 谢谢!)

-- 1. SAMPLE DATA
DECLARE @locality TABLE (localityid int, locality int);
DECLARE @job TABLE (id int, Job varchar(100));
DECLARE @person TABLE 
(
  name varchar(100), 
  surname varchar(100),
  jobid int,
  localityid int,
  salary money,
  gender char(1)
);

INSERT @locality VALUES (1, 10), (2, 20), (3, 35), (4, 1000);
INSERT @job VALUES (1, 'C# Developer'), (2, 'DBA');
INSERT @person VALUES 
('joe', 'blow', 1, 1, 100000, 'M'),
('Sally', 'Doe', 1, 2, 135000, 'F'),
('Sue', 'Smith', 2, 2, 115000, 'F'),
('Kyle', 'Jones', 2, 3, 107500, 'M');


SELECT j.Job
      ,p.gender 
      ,AVG(p.salary) AS Average 
FROM @person AS p
INNER JOIN @locality AS l ON p.localityid=l.localityid
INNER JOIN @job AS j ON p.jobid=j.id
GROUP BY ROLLUP (gender,job)
ORDER BY job,gender
FOR XML RAW

结果

<row Average="114375.0000" />
<row gender="F" Average="125000.0000" />
<row gender="M" Average="103750.0000" />
<row Job="C# Developer" gender="F" Average="135000.0000" />
<row Job="C# Developer" gender="M" Average="100000.0000" />
<row Job="DBA" gender="F" Average="115000.0000" />
<row Job="DBA" gender="M" Average="107500.0000" />