sql 如何在行前加一列
SELECTSUM(casewhenfperiod='1'thenFENDBALelse0end)AS'1501',SUM(casewhenfperiod='2'then...
SELECT
SUM(case when fperiod='1'
then FENDBAL else 0 end ) AS '1501',
SUM(case when fperiod='2'
then FENDBAL else 0 end ) AS '1502',
SUM(case when fperiod='3'
then FENDBAL else 0 end ) AS '1503',
SUM(case when fperiod='4'
then FENDBAL else 0 end ) AS '1504',
SUM(case when fperiod='5'
then FENDBAL else 0 end ) AS '1505',
SUM(case when fperiod='6'
then FENDBAL else 0 end ) AS '1506',
SUM(case when fperiod='7'
then FENDBAL else 0 end ) AS '1507',
SUM(case when fperiod='8'
then FENDBAL else 0 end ) AS '1508',
SUM(case when fperiod='9'
then FENDBAL else 0 end ) AS '1509',
SUM(case when fperiod='10'
then FENDBAL else 0 end ) AS '1510',
SUM(case when fperiod='11'
then FENDBAL else 0 end ) AS '1511',
SUM(case when fperiod='12'
then FENDBAL else 0 end ) AS '1512'
FROM dbO.ICinvBAL A INNER JOIN DBO.T_STOCKPLACE B
ON A.FSTOCKPLACEID = B.FSPID
WHERE fyear='2015' AND B.FNUMBER LIKE '%04.%'
请问怎么在第一行在加一列‘b.name’? 展开
SUM(case when fperiod='1'
then FENDBAL else 0 end ) AS '1501',
SUM(case when fperiod='2'
then FENDBAL else 0 end ) AS '1502',
SUM(case when fperiod='3'
then FENDBAL else 0 end ) AS '1503',
SUM(case when fperiod='4'
then FENDBAL else 0 end ) AS '1504',
SUM(case when fperiod='5'
then FENDBAL else 0 end ) AS '1505',
SUM(case when fperiod='6'
then FENDBAL else 0 end ) AS '1506',
SUM(case when fperiod='7'
then FENDBAL else 0 end ) AS '1507',
SUM(case when fperiod='8'
then FENDBAL else 0 end ) AS '1508',
SUM(case when fperiod='9'
then FENDBAL else 0 end ) AS '1509',
SUM(case when fperiod='10'
then FENDBAL else 0 end ) AS '1510',
SUM(case when fperiod='11'
then FENDBAL else 0 end ) AS '1511',
SUM(case when fperiod='12'
then FENDBAL else 0 end ) AS '1512'
FROM dbO.ICinvBAL A INNER JOIN DBO.T_STOCKPLACE B
ON A.FSTOCKPLACEID = B.FSPID
WHERE fyear='2015' AND B.FNUMBER LIKE '%04.%'
请问怎么在第一行在加一列‘b.name’? 展开
展开全部
SELECT
B.name,
SUM(case when fperiod='1'
then FENDBAL else 0 end ) AS '1501',
SUM(case when fperiod='2'
then FENDBAL else 0 end ) AS '1502',
SUM(case when fperiod='3'
then FENDBAL else 0 end ) AS '1503',
SUM(case when fperiod='4'
then FENDBAL else 0 end ) AS '1504',
SUM(case when fperiod='5'
then FENDBAL else 0 end ) AS '1505',
SUM(case when fperiod='6'
then FENDBAL else 0 end ) AS '1506',
SUM(case when fperiod='7'
then FENDBAL else 0 end ) AS '1507',
SUM(case when fperiod='8'
then FENDBAL else 0 end ) AS '1508',
SUM(case when fperiod='9'
then FENDBAL else 0 end ) AS '1509',
SUM(case when fperiod='10'
then FENDBAL else 0 end ) AS '1510',
SUM(case when fperiod='11'
then FENDBAL else 0 end ) AS '1511',
SUM(case when fperiod='12'
then FENDBAL else 0 end ) AS '1512'
FROM dbO.ICinvBAL A INNER JOIN DBO.T_STOCKPLACE B
ON A.FSTOCKPLACEID = B.FSPID
WHERE fyear='2015' AND B.FNUMBER LIKE '%04.%'
group by B.name
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询