使用不同表格中的列创建一个表格

时间:2018-11-26 01:45:43

标签: sql

我不确定如何使此代码在每个类别(纵向,横向和抽象)中具有三列。到目前为止,它只有一列,并且所有这三个值都是计算值:

SELECT SUM(Price) AS TotalSales_Portrait
FROM Photo
WHERE TransID IS NOT NULL AND PhotoID IN (
SELECT PhotoID
FROM Models) 

UNION

SELECT SUM(Price) AS TotalSales_Landscape
FROM Photo
WHERE TransID IS NOT NULL AND PhotoID IN (
SELECT PhotoID
FROM Landscape)

UNION

SELECT SUM(Price) AS TotalSales_Abstract
FROM Photo
WHERE TransID IS NOT NULL AND PhotoID IN (
SELECT PhotoID
FROM Abstract)

Query output

任何建议都将受到赞赏

4 个答案:

答案 0 :(得分:1)

谢谢乔治,这解决了我的问题。这是修改您的代码后的最终代码:

select *
from (
SELECT MAX('Portrait') as photo_type,SUM(Price) AS Total_Sales
  FROM Photo
WHERE TransID IS NOT NULL AND PhotoID IN (
SELECT PhotoID
  FROM Models) 
UNION
SELECT MAX('Landscape'),SUM(Price) AS Total_Sales
FROM Photo
WHERE TransID IS NOT NULL AND PhotoID IN (
SELECT PhotoID
FROM Landscape)
UNION
SELECT MAX('Abstract'),SUM(Price) AS Total_Sales
FROM Photo
WHERE TransID IS NOT NULL AND PhotoID IN (
SELECT PhotoID
FROM Abstract)
)x
pivot (sum(Total_Sales) for photo_type in(Landscape,Abstract,Portrait))y

答案 1 :(得分:0)

如果“照片”表中有一个photo type字段,则会更容易:

SELECT SUM(price), photo_type 
FROM Photos
JOIN Models 
ON Models.PhotoID = Photos.PhotoID
JOIN Landscape
ON Landscape.PhotoID = Photos.PhotoID
JOIN Abstract
ON Abstract.PhotoID = Photos.PhotoID
GROUP BY photo_type

如果没有,您可能会做类似的事情:

SELECT SUM(price), type FROM (
  SELECT price, 'Models' as type from Photos 
  WHERE TransID IS NOT NULL AND PhotoID IN (
    SELECT PhotoID
    FROM Models
  )

  UNION

  SELECT price, 'Landscape' as type from Photos 
  WHERE TransID IS NOT NULL AND PhotoID IN (
    SELECT PhotoID
    FROM Landscape
  )

  UNION

  SELECT price, 'Abstract' as type from Abstract
) as photos
GROUP BY type

我还没有测试这些查询,但是它们至少应该接近一个可行的解决方案。

答案 2 :(得分:0)

这可以通过使用以下枢轴来完成

select *
from (
SELECT MAX('Portrait') as photo_type,SUM(Price) AS Total_Sales
  FROM Photo
WHERE TransID IS NOT NULL AND PhotoID IN (
SELECT PhotoID
  FROM Models) 
UNION
SELECT MAX('Landscape'),SUM(Price) AS Total_Sales
FROM Photo
WHERE TransID IS NOT NULL AND PhotoID IN (
SELECT PhotoID
FROM Landscape)
)x
pivot (sum(total_sales) for photo_type in(Portrait,Landscape))y

答案 3 :(得分:0)

我想我只会使用子查询:

SELECT (SELECT SUM(Price) AS TotalSales_Portrait
        FROM Photo
        WHERE TransID IS NOT NULL AND PhotoID IN (SELECT PhotoID FROM Models) 
       ) as TotalSales_Portrait,
       (SELECT SUM(Price) AS TotalSales_Landscape
        FROM Photo
        WHERE TransID IS NOT NULL AND PhotoID IN (SELECT PhotoID FROM Landscape)
       ) as TotalSales_Landscape,
       (SELECT SUM(Price) AS TotalSales_Abstract
        FROM Photo
        WHERE TransID IS NOT NULL AND PhotoID IN (SELECT PhotoID FROM Abstract)
       ) as TotalSales_Abstract;

或者,如果辅助表没有重复项:

SELECT SUM(CASE WHEN m.PhotoId IS NOT NULL THEN price ELSE 0 END),
       SUM(CASE WHEN l.PhotoId IS NOT NULL THEN price ELSE 0 END),
       SUM(CASE WHEN a.PhotoId IS NOT NULL THEN price ELSE 0 END)
FROM Photos p LEFT JOIN
     Models m
     ON m.PhotoID = p.PhotoID LEFT JOIN
     Landscape l
     ON l.PhotoID = p.PhotoID LEFT JOIN
     Abstract a
     ON a.PhotoID = p.PhotoID