需要在访问数据库上转移数据

时间:2017-05-23 23:16:38

标签: sql ms-access

假设我有这样的表结构:

表Pizza_Sales

server_name ~^(?<sub>.+)\.domain\.com$;

location /images {
    # You can put this code in a snippet file and than include
    # it with e.g. `include assets.conf;` so you can use more
    # folders with longest prefix match blocks, which are more
    # efficient than regex blocks.

    # Tell the browser to cache this for one day
    expires 1d;

    # Just give the resource
    try_files $uri =404;
}

location /index.php {
    # PHP stuff goes here
}

location / {
    rewrite ^ /index.php?sub=$sub&path=$uri last;
}

Table Pizza

 Customer_ID  | Total_Spent
 123456            45.50
 346432            60.31
 867432            100.67

表名

ID|   Customer_ID | Topping   | Percent
 1      123456       pepper.    50%
 2      123456       onions     50%
 3      346432       pineap.    25%
 4      346432       mushro.    25%
 5      346432       pepper.    25%
 6      346432       sausag.    25%
 7      867432       bacon      100%

所有客户ID都加入了。说我想要一份报告,上面写着他们的姓名,客户ID,他们吃的浇头以及他们花了多少钱。所以结果看起来像这样:

  Customer_ID     | Name
       123456       Jon    
       346432       Steve 
       867432       Mike

我怎么能这样做?另请注意,在2个或更多顶部的情况下,任何具有较小ID的内容,即该客户ID组的第一个记录将始终首先跟随任何第二个等等。我实际上理解如何做除了分组的配件之外的所有事情。我坚持的是什么。看起来像一个数据透视表或什么是我正在寻找的dunno如何将其应用于Access SQL。

1 个答案:

答案 0 :(得分:1)

使用域函数DCount考虑带有运行计数的MS Access'唯一crosstab查询:

TRANSFORM Max(main.topping) AS MaxOfTopping
SELECT main.Customer_ID, main.[Total_Spent] As [Total]
FROM 
   (SELECT t.ID, t.Customer_ID, t.Topping, t.[Percent], s.[Total_Spent],
          'T_' & DCount("*", "Toppings", "Customer_ID = " & t.Customer_ID & " 
                        AND Topping <='" & t.Topping & "' 
                        AND [Percent]<=" & t.[Percent]) As [Rank]
    FROM Pizza As t
    INNER JOIN Pizza_Sales As s ON t.Customer_ID = s.Customer_ID) As main  

GROUP BY main.Customer_ID, main.[Total_Spent]
PIVOT main.Rank;

-- Customer_ID  Total   T_1     T_2     T_3     T_4
-- 123456       45.5    onions  pepper.     
-- 346432       60.31   mushro. pepper. pineap. sausag.
-- 867432       100.67  bacon