如何在一行中获取某些数据

时间:2013-12-18 06:30:26

标签: mysql sql sql-server

是否可以在sql中的一行下获取某些数据?

目前我的结果是:

name                        Month_Number    Month   YEAR    Total_Amount Total_Retailfee Total_Transactions
Spar Group                  1               January 2013    46928270.04  269477.0855     100445
Shoprite Checkers Pty Ltd   1               January 2013    83228663.71  290345.517      91282
Pick n Pay (Pty) Ltd        1               January 2013    78518522.5   590395.6488     210117
Pep                         1               January 2013    185792113.3  1847361.334     457374

但我需要这样:

name       Month_Number Month   YEAR Description        
Spar Group 1            January 2013 Total_Amount       46928270.04
                                     Total_Retailfee    269477.0855 
                                     Total_Transactions 100445

3 个答案:

答案 0 :(得分:1)

像这样 -

SELECT
  name, Month_Number, Month, YEAR,
  CONCAT_WS(
    '\r\n',
    CONCAT('Total_Amount=',Total_Amount),
    CONCAT('Total_Retailfee=',Total_Retailfee),
    CONCAT('Total_Transactions=',Total_Transactions)
  ) AS description
FROM
  table;

CONCAT_WS功能。

答案 1 :(得分:1)

如果我理解你,你正在寻找这样的东西:

SELECT column_1, column_3, column_5 FROM myTable WHERE column_1 = "some_value"

这将仅选择该行中的某些值。

答案 2 :(得分:0)

我首先想提一下,如果这是用于在Web界面或报表中进行演示,您可以使用求和进行更简单的分组查询,然后只需将数据移动到需要的位置即可。例如,您可以在[Total_Amount]之后开始一个新的HTML行,并插入4个空单元格,然后插入下一个小计等,从而创建所需的表格结构。

-- it appears this query contains everything you are asking for, but not using the same 'tabular structure.'
SELECT
    name
    ,MONTH([MyDate]) AS [MonthNumber]
    ,DATENAME(MONTH, [MyDate]) AS [MONTH]
    ,YEAR([MyDate]) AS [YEAR]   
    ,SUM(Amount) AS [Total_Amount]
    ,SUM(Retailfee) AS [Total_Retailfee] -- move down one row
    ,SUM(Transactions) AS [Total_Transactions] -- move down two rows
FROM #temp
GROUP BY
     name
    ,MONTH([MyDate])
    ,DATENAME(MONTH, [MyDate])
    ,YEAR([MyDate]

您希望在查询中为数据集中的每个[name]和[MONTH]组合返回三行。如果是这样,这里有一个解决方案,使用设置创建一些示例数据和派生表,以根据需要提取它:

-- I find that providing example data help to get more accurate results for some queries
CREATE TABLE #temp (
     name VARCHAR(20)
    ,MyDate DATE
    ,Amount DECIMAL(8,4)
    ,Retailfee DECIMAL(8,4)
    ,Transactions DECIMAL(8,4)
);

-- no idea what these values actually mean so the data may not make sense.
INSERT #temp VALUES ('Spar Group','01/01/2013',1000.00,163.55,200.00)
INSERT #temp VALUES ('Spar Group','01/02/2013',40.00,0.11,2.00)
INSERT #temp VALUES ('Spar Group','01/03/2013',299.99,35.00,33.00)
INSERT #temp VALUES ('Spar Group','02/03/2013',145.99,0.00,7.00)
INSERT #temp VALUES ('Shoprite','01/01/2013',22.99,0.00,8.00)
INSERT #temp VALUES ('Shoprite','01/02/2013',149.95,16.60,11.50)

SELECT
     name
    ,[MonthNumber]
    ,[MONTH]
    ,[YEAR]
    ,[Category]
    ,[CategoryTotal]
FROM (
    -- select the first row for each [name],[month] group
    SELECT
        name
        ,MONTH([MyDate]) AS [MonthNumber]
        ,DATENAME(MONTH, [MyDate]) AS [MONTH]
        ,YEAR([MyDate]) AS [YEAR]   
        ,'Total_Amount' AS [Category]
        ,SUM(Amount) AS [CategoryTotal]
    FROM #temp
    GROUP BY
         name
        ,MONTH([MyDate])
        ,DATENAME(MONTH, [MyDate])
        ,YEAR([MyDate])

    UNION ALL
    -- select the second row for each [name],[month] group
    SELECT
        name
        ,MONTH([MyDate]) AS [MonthNumber]
        ,DATENAME(MONTH, [MyDate]) AS [MONTH]
        ,YEAR([MyDate]) AS [YEAR]   
        ,'Total_Retailfee' AS [Category]
        ,SUM(Retailfee) AS [CategoryTotal]
    FROM #temp
    GROUP BY
         name
        ,MONTH([MyDate])
        ,DATENAME(MONTH, [MyDate])
        ,YEAR([MyDate])

    UNION ALL
    -- select the third row for each [name],[month] group
    SELECT
        name
        ,MONTH([MyDate]) AS [MonthNumber]
        ,DATENAME(MONTH, [MyDate]) AS [MONTH]
        ,YEAR([MyDate]) AS [YEAR]   
        ,'Total_Transactions' AS [Category]
        ,SUM(Transactions) AS [CategoryTotal]
    FROM #temp
    GROUP BY
         name
        ,MONTH([MyDate])
        ,DATENAME(MONTH, [MyDate])
        ,YEAR([MyDate])
) AS T
ORDER BY
     name, [MONTH], [YEAR]
    ,CASE
        WHEN [Category]='Total_Amount' THEN 1
        WHEN [Category]='Total_Retailfee' THEN 2
        ELSE 3
     END