简体   繁体   English

SQL Server:查询多个表以创建预格式化的“模板” csv,以导出到Excel

[英]SQL Server: query multiple tables to create a pre-formated “template” csv for export into Excel

I am trying to create the SQL necessary to accomplish the following. 我正在尝试创建完成以下任务所需的SQL。 As an aside, I am using server side Report Builder against a hosted SQL Server database so am limited in what I can do. 顺便说一句,我针对托管的SQL Server数据库使用服务器端Report Builder,因此我只能做些事情。 There are 3 tables. 有3张桌子。 A salesperson table (sales), an items table (items), and a transaction table (tx). 销售员表(销售),项目表(项目)和交易表(tx)。

Here is an example: 这是一个例子:

Sales Person table (sales) 销售人员表(销售)

Person A
Person B

Items ID table (items) 项目ID表(项目)

  10   
  20
  30
 100
 200
 300
1000
2000
3000

Transaction table (tx) 交易表(TX)

100 (person A)
300 (person B)
300 (person A)
200 (person B)

Desired result in Report Builder: 报表生成器中的所需结果:

Person A
Item 100: 1
Item 200: 0 (NULL)
Item 300: 1

-- NEW PAGE --

Person B:
Item 100: 0 (NULL)
Item 200: 1
Item 300: 1

My problem: here is the SQL I came up with. 我的问题:这是我想出的SQL。 I need to be able to generate a consistent result set, regardless of whether an item was sold or not by a particular salesperson for easier import into Excel. 我需要能够生成一致的结果集,而不管某个销售人员是否出售了某件商品,以便更轻松地导入Excel。 In addition, I am only looking for items whose code is between 100 and 300 and within a specified date range. 另外,我只查找代码在100到300之间且在指定日期范围内的项目。 My SQL is ignoring the date range and item code range. 我的SQL忽略了日期范围和项目代码范围。 I originally had these instructions in a WHERE clause but it returned only those lines that were in both tables and I lost the placeholder for any itemcode where the value was null (acting as an INNER join). 我最初在WHERE子句中具有这些指令,但它仅返回两个表中的那些行,并且丢失了任何值为null的itemcode(充当INNER连接)的占位符。 Within Report Builder I will be counting how many of each items were sold by salesperson. 在报表制作工具中,我将统计销售员售出的每种商品的数量。

SELECT  
    tx.date, sales.salesperson, items.itemcode
FROM 
    tx
LEFT OUTER JOIN 
    itemcode ON (tx.itemcode = items.itemcode) 
             AND (date BETWEEN "10/1/2017" AND "12/31/2017") 
             AND (itemcode BETWEEN "100" AND "300")
INNER JOIN 
    sales ON (tx.salesID = sales.salesID)
ORDER BY 
    itemcode ASC

Many thanks for any and all insight into my challenge! 非常感谢您对我的挑战的任何见解!

If you want all sales people and all items, then you can generate the rows using a cross join . 如果您需要所有销售人员和所有项目,则可以使用cross join生成行。 You can bring in the available data using left join or exists : 您可以使用left joinexists来引入可用数据:

select s.person, i.itemcode,
       (case when exists (select 1
                          from tx
                          where tx.salesid = s.salesid and tx.itemcode = i.itemcode
                         )
             then 1 else 0
        end) as has_sold
from sales s cross join
     items i 
where i.itemcode between 100 and 300
order by s.saledid, i.itemcode;

If you want the count of items, use left join and group by : 如果要计数,请使用left joingroup by

select s.person, i.itemcode, count(tx.salesid) as num_sold
from sales s cross join
     items i left join
     tx
     on tx.salesid = s.salesid and tx.itemcode = i.itemcode
where i.itemcode between 100 and 300
order by s.saledid, i.itemcode;

Here's an example that uses both cross join (to get all combinations of sales and items) and left join (to get the transactions for given dates) 这是一个使用cross join (获取销售和商品的所有组合)和left join (获取给定日期的交易)的示例

SELECT  
    tx.date, sales.salesperson, items.itemcode
FROM 
    Items
CROSS JOIN 
    sales
LEFT OUTER JOIN 
     tx ON (items.itemcode = tx.itemcode) 
     AND date BETWEEN '10/1/2017' AND '12/31/2017'
     AND (tx.salesID = sales.salesID)
WHERE
    (items.itemcode BETWEEN '100' AND '300')
ORDER BY 
    itemcode ASC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM