我可以使用 SQL 语句从工作表 A 中提取数据吗?仅使用 Excel 将工作表 B 转储到工作表 C 中

发布于 2024-11-09 18:07:53 字数 1208 浏览 2 评论 0原文

我在 Excel 中有这样的数据

table Cost_per_period
---------------------
ProjectId
FaseID
Period
Percentage

table cost_per_partner_per_fase
-------------------------------
ProjectID
FaseID
PartnerID
Amount

table partners
--------------
PartnerID
name

这是我想要的输出。

                 2012             2013                2014     2015
Project  fase    jan  feb ... dec  Q1    Q2   Q3   Q4  wholeY  wholeY
------------------------------------------------------------------------
A310     1       100k 20k     10k  100k   -   10k  10k 1000k   2000k
A310     2       110k   -     20k   99k   -   40k  50k 5000k   3000k
......

为了合并这些数据,我正在考虑执行一个 SQL 语句,例如

SELECT cp.projectID, cp.faseID
  , case when cp.period between '2012/01/01' and '2012/01/31' 
         THEN sum(cpf.amount)*cp.percentage as jan2012 end
  , case when ..... as feb2012 end
  , case .......
FROM cost_per_period as cp
INNER JOIN cost_per_partner_per_fase as cpf 
  on (cp.postjectid = cpf.projectid) and (cp.faseid = cpf.faseid)
GROUP BY cp.Projectid, cp.faseid
ORDER BY cp.ProjectID, cp.FaseID

我可以仅使用 Excel 执行此操作吗?,我使用的是 excel 2007

I have data like this in Excel

table Cost_per_period
---------------------
ProjectId
FaseID
Period
Percentage

table cost_per_partner_per_fase
-------------------------------
ProjectID
FaseID
PartnerID
Amount

table partners
--------------
PartnerID
name

Here's the output I want.

                 2012             2013                2014     2015
Project  fase    jan  feb ... dec  Q1    Q2   Q3   Q4  wholeY  wholeY
------------------------------------------------------------------------
A310     1       100k 20k     10k  100k   -   10k  10k 1000k   2000k
A310     2       110k   -     20k   99k   -   40k  50k 5000k   3000k
......

To combine this data, I'm thinking of doing a SQL statement like

SELECT cp.projectID, cp.faseID
  , case when cp.period between '2012/01/01' and '2012/01/31' 
         THEN sum(cpf.amount)*cp.percentage as jan2012 end
  , case when ..... as feb2012 end
  , case .......
FROM cost_per_period as cp
INNER JOIN cost_per_partner_per_fase as cpf 
  on (cp.postjectid = cpf.projectid) and (cp.faseid = cpf.faseid)
GROUP BY cp.Projectid, cp.faseid
ORDER BY cp.ProjectID, cp.FaseID

Can I do this using only Excel?, I'm using excel 2007

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酷到爆炸 2024-11-16 18:07:53

以下是针对 Excel 的最简单的查询,可以帮助您入门:

Sub test()
  Dim rs As Object
  Set rs = CreateObject("ADOR.Recordset")
  rs.Open _
      "SELECT CompanyName FROM Customers;", _
        "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=C:\Tempo\db.xls;" & _
        "Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'"
    Sheet3.Range("A1").CopyFromRecordset rs
  End Sub

Here's the simplest of queries against Excel which may get you started:

Sub test()
  Dim rs As Object
  Set rs = CreateObject("ADOR.Recordset")
  rs.Open _
      "SELECT CompanyName FROM Customers;", _
        "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=C:\Tempo\db.xls;" & _
        "Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'"
    Sheet3.Range("A1").CopyFromRecordset rs
  End Sub
苍白女子 2024-11-16 18:07:53

这是取自 Excel 2007 中录制的对我有用的宏。您可以修改数据源和 SQL 查询以满足您的需要。

ActiveWorkbook.Worksheets.Add
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array( _
    "OLEDB;Provider=Microsoft.ACE.OLEDB.12.0;" _
    , "Data Source=C:\RM.xlsm;" _
    , "Jet OLEDB:Engine Type=37"), Destination:=Range("$A$1")).QueryTable
    .CommandType = xlCmdTable
    .CommandText = Array("SELECT * FROM [Sheet1$A1:B30]")

End With

This is taken from a macro recorded in Excel 2007 that works for me. You can modify the data source and the SQL query to suit your need.

ActiveWorkbook.Worksheets.Add
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array( _
    "OLEDB;Provider=Microsoft.ACE.OLEDB.12.0;" _
    , "Data Source=C:\RM.xlsm;" _
    , "Jet OLEDB:Engine Type=37"), Destination:=Range("$A$1")).QueryTable
    .CommandType = xlCmdTable
    .CommandText = Array("SELECT * FROM [Sheet1$A1:B30]")

End With
梦幻的味道 2024-11-16 18:07:53

如果数据已存在于 Excel 中,则无法对其运行 SQL 查询。您只能针对 SQL 数据库运行此类代码(例如,如果您在 Access 中拥有数据)。

但是,您可以在 VBA 中编写等效函数并在 Excel 中运行它。

我还发现这个插件可以让你对 Excel 数据执行 SQL:http://www.querycell.com/
但它不是免费的,我不知道它有多好。

If the data is already in Excel, you can't run an SQL query against it. You'd only be able to run that kind of code against an SQL database (eg if you had the data in Access to start with).

However, you can write an equivalent function in VBA and run that in Excel.

I also found this plug in that would let you execute SQL against Excel data: http://www.querycell.com/
but it's not free and I have no idea how good it is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文