0

有人可以请帮助我。我正在尝试做一些看起来非常简单和容易的事情。但我似乎无法弄清楚。

我试图将我的数据显示为数据透视表而不是“正常呈现”这就是现在的样子。

Store, Product, amount
store1, potatoes, 10
store2, strawberry, 20

但我想要的是:

Product    Store1  Store2  Total
potatoes   10                   10
strawberry         20        20

这是我使用的代码:


Dim rsMenu1
    Set rsMenu1 = Server.CreateObject("ADODB.Recordset")
    rsMenu1.ActiveConnection = ConnectString
    dim sql
    sql = "SELECT store.storeOms, datepart(wk,voorraad.datum) as week, products.prodOms,    SUM       (voorraad.besteld) AS besteld FROM products" 
    sql = sql & " INNER JOIN voorraad ON products.prodId = voorraad.prodId INNER JOIN store "
    sql = sql & " ON voorraad.storeId = store.storeId "
    sql = sql & " WHERE datepart(wk,datum) =" & request.querystring("q")
    sql = sql & " AND voorraad.besteld > 0"
    sql = sql & " GROUP BY store.storeOms, voorraad.datum, products.prodOms"

    response.Write(sql)

    rsMenu1.Source = sql
    rsMenu1.Open()
    %>

<table width="200" border="0" class="table table-hover">

  <tr>
    <th>Winkel</th>
    <th>Product</th>
    <th>Aantal besteld</th>
  </tr>
  <%do until rsMenu1.EOF%>
  <tr>
     <td><%=rsMenu1.Fields.Item("storeOms").Value%></td>
     <td><%=rsMenu1.Fields.Item("prodOms").Value%></td>
     <td><%=rsMenu1.Fields.Item("besteld").Value%></td>

  </tr>
   <%rsMenu1.Movenext 
    Loop%>
4

1 回答 1

1

您应该能够直接使用 SQL 对数据进行透视。您可以将聚合函数与类似以下的 CASE 表达式一起使用:

select product,
  sum(case when store = 'store1' then amount else 0 end) Store1,
  sum(case when store = 'store2' then amount else 0 end) Store2,
  sum(amount) Total
from yd
group by product;

请参阅SQL Fiddle with Demo

然后将此添加到您现有的查询中,代码将类似于:

SELECT products.prodOms,
  sum(case when store.storeOms = 'store1' then voorraad.besteld else 0 end) Store1,
  sum(case when store.storeOms = 'store2' then voorraad.besteld else 0 end) Store2,
  SUM(voorraad.besteld) AS Total,
  datepart(wk,voorraad.datum) as week
FROM products
INNER JOIN voorraad 
  ON products.prodId = voorraad.prodId 
INNER JOIN store
  ON voorraad.storeId = store.storeId
WHERE datepart(wk,datum) =yourDate
  AND voorraad.besteld > 0
GROUP BY products.prodOms, datepart(wk,voorraad.datum)

由于您使用的是 SQL Server 2005+,因此您可以使用 PIVOT 函数,如果您有未知数量的存储,则可以在存储过程中使用动态 SQL 来获取结果。基本语法将是:

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX),
    @yourDate as datetime

select @cols = STUFF((SELECT distinct ',' + QUOTENAME(storeOms) 
                    from store
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT products.prodOms,' + @cols + ', Total , week 
             from 
             (
                select products.prodOms, store.storeOms, 
                    voorraad.besteld, datepart(wk,voorraad.datum) as week,
                    sum(voorraad.besteld) over(partition by products.prodOms, datepart(wk,voorraad.datum)) Total
                FROM products
                INNER JOIN voorraad 
                  ON products.prodId = voorraad.prodId 
                INNER JOIN store
                  ON voorraad.storeId = store.storeId
                WHERE datepart(wk,datum) = '''+convert(varchar(10), yourDate, 120)+'''
                  AND voorraad.besteld > 0
            ) x
            pivot 
            (
                sum(besteld)
                for storeOms in (' + @cols + ')
            ) p '

execute(@query)
于 2013-06-02T14:10:34.450 回答