SQl 查询、子查询格式问题
SQl query, subquery formatting issue
我需要有人帮助我进行一些格式设置。我试图使用子查询和 UNION ALL 等将其放入一行,但没有成功。这似乎很容易,但我只是用这个把头撞在墙上。 table 的布局与数据类似:
Date pTypeCode Location Amt
1/1/2015 C Store1 50.21
1/1/2015 C Store1 125.62
1/1/2015 P Store 1 250.1
1/1/2015 EB Store 1 125.01
1/1/2015 C Store 2 50.25
1/1/2015 C Store 2 100.25
1/1/2015 EB Store 2 125.25
1/3/2015 EB Store 1 35.25
1/3/2015 C Store 1 35.25
1/3/2015 C Store 1 35.25
1/3/2015 P Store 1 35.25
1/3/2015 C Store 2 85.15
1/3/2015 C Store 2 65.25
1/3/2015 P Store 2 65.25
1/3/2015 EB Store 2 65.25
我需要它来获取 pTypeCode 的计数作为每行的列数。我可以通过这样的简单查询轻松获取我想要的数据:
SELECT Date, LOCATION,
pTypeCode,
Count(pTypeCode),
Sum(Amt) AS Prem
FROM [dbo].[CommisionEntry]
WHERE Date >=
(SELECT DATEADD(DAY, 1, EOMONTH(GetDate(), -1)))
AND Date <=
(SELECT EOMONTH(GetDate()))
GROUP BY date, LOCATION,
pTypeCode
但我需要行中的 count(pTypeCode) = P 和 C 和 EB 也像这样:
Date Count(pTypeCode) C Count(pTypeCode) P Count(pTypeCode) EB SUM(Amt)
1/1/2015 2 1 1 550.94
如有任何帮助,我们将不胜感激。
谢谢
如果您没有可用的数据透视表之类的东西(尽管您应该这样做,因为看起来您正在使用 T-SQL),您可以为每个值创建列,如下所示:
select
date
,sum(case pTypeCode when 'C' then 1 else null end) count_c
,sum(case pTypeCode when 'P' then 1 else null end) count_p
,sum(case pTypeCode when 'EB' then 1 else null end) count_eb
,sum(amt) sum_amt
from
CommisionEntry
where date >=
dateadd(day, 1, eomonth(GetDate(), -1))
and date <= eomonth(GetDate())
group by
date
select distinct a.date,
a.Location,
b.C,
c.EB,
d.p,
e.amt
from (select * from test1 group by date,location) as A
left outer join
(select date,count(*) as C, location from test1 where pTypeCode = "C" group by date,location) as b
on a.date = b.date
and a.location = b.location
left outer join
(select date,count(*) as EB, location from test1 where pTypeCode = "EB" group by date,location) as c
on a.date = c.date
and a.location = c.location
left outer join
(select date,count(*) as P, location from test1 where pTypeCode = "P" group by date,location) as d
on a.date = d.date
and a.location = d.location
left outer join
(select date,location,sum(Amt) as Amt, location from test1 group by date, location) as e
on a.date = e.date
and a.location = e.location
order by a.date,a.location;
我需要有人帮助我进行一些格式设置。我试图使用子查询和 UNION ALL 等将其放入一行,但没有成功。这似乎很容易,但我只是用这个把头撞在墙上。 table 的布局与数据类似:
Date pTypeCode Location Amt
1/1/2015 C Store1 50.21
1/1/2015 C Store1 125.62
1/1/2015 P Store 1 250.1
1/1/2015 EB Store 1 125.01
1/1/2015 C Store 2 50.25
1/1/2015 C Store 2 100.25
1/1/2015 EB Store 2 125.25
1/3/2015 EB Store 1 35.25
1/3/2015 C Store 1 35.25
1/3/2015 C Store 1 35.25
1/3/2015 P Store 1 35.25
1/3/2015 C Store 2 85.15
1/3/2015 C Store 2 65.25
1/3/2015 P Store 2 65.25
1/3/2015 EB Store 2 65.25
我需要它来获取 pTypeCode 的计数作为每行的列数。我可以通过这样的简单查询轻松获取我想要的数据:
SELECT Date, LOCATION,
pTypeCode,
Count(pTypeCode),
Sum(Amt) AS Prem
FROM [dbo].[CommisionEntry]
WHERE Date >=
(SELECT DATEADD(DAY, 1, EOMONTH(GetDate(), -1)))
AND Date <=
(SELECT EOMONTH(GetDate()))
GROUP BY date, LOCATION,
pTypeCode
但我需要行中的 count(pTypeCode) = P 和 C 和 EB 也像这样:
Date Count(pTypeCode) C Count(pTypeCode) P Count(pTypeCode) EB SUM(Amt)
1/1/2015 2 1 1 550.94
如有任何帮助,我们将不胜感激。
谢谢
如果您没有可用的数据透视表之类的东西(尽管您应该这样做,因为看起来您正在使用 T-SQL),您可以为每个值创建列,如下所示:
select
date
,sum(case pTypeCode when 'C' then 1 else null end) count_c
,sum(case pTypeCode when 'P' then 1 else null end) count_p
,sum(case pTypeCode when 'EB' then 1 else null end) count_eb
,sum(amt) sum_amt
from
CommisionEntry
where date >=
dateadd(day, 1, eomonth(GetDate(), -1))
and date <= eomonth(GetDate())
group by
date
select distinct a.date,
a.Location,
b.C,
c.EB,
d.p,
e.amt
from (select * from test1 group by date,location) as A
left outer join
(select date,count(*) as C, location from test1 where pTypeCode = "C" group by date,location) as b
on a.date = b.date
and a.location = b.location
left outer join
(select date,count(*) as EB, location from test1 where pTypeCode = "EB" group by date,location) as c
on a.date = c.date
and a.location = c.location
left outer join
(select date,count(*) as P, location from test1 where pTypeCode = "P" group by date,location) as d
on a.date = d.date
and a.location = d.location
left outer join
(select date,location,sum(Amt) as Amt, location from test1 group by date, location) as e
on a.date = e.date
and a.location = e.location
order by a.date,a.location;