【发布时间】:2022-01-14 00:49:46
【问题描述】:
我使用 SQL Server,并且我编写了以下 SQL 查询,以便显示客户名称、城镇名称、产品、销售数量、销售年份、销售月份和经销商名称以及我在特定月份销售的数量,但是即使一个月没有数据,我也希望查询按月显示。
SELECT dbo.Customerdetl.cust_name, dbo.Towndetl.town_name, dbo.productdetl.prod_desc, dbo.SalesSummary.sales_qty, dbo.SalesSummary.year_numb, dbo.SalesSummary.month_numb,dbo.DistributorDetl.DISTNAME
FROM dbo.SalesSummary INNER JOIN
dbo.productdetl ON dbo.SalesSummary.ProductCode = dbo.productdetl.ProductCode INNER JOIN
dbo.DistributorDetl ON dbo.SalesSummary.DistCode = dbo.DistributorDetl.DistCode INNER JOIN
dbo.Customerdetl ON dbo.SalesSummary.cust_code = dbo.Customerdetl.cust_code INNER JOIN
dbo.Towndetl ON dbo.SalesSummary.town_code = dbo.Towndetl.town_code
WHERE (dbo.SalesSummary.year_numb = 2021) AND (dbo.Customerdetl.cust_name = N'Janani Pharmacy') AND (dbo.productdetl.prod_desc = N'Mycotin')
我得到的结果是:
| Cust_name | Town | Product | Sales_qty | Year | Month | Distributor_name |
|---|---|---|---|---|---|---|
| Janani Pharmacy | HANWELLA | Mycotin | 10 | 2021 | 7 | NEGA |
| Janani Pharmacy | HANWELLA | Mycotin | 10 | 2021 | 4 | NEGA |
我想得到的是:
| Cust_name | Town | Product | Sales_qty | Year | Month | Distributor_name |
|---|---|---|---|---|---|---|
| Janani Pharmacy | HANWELLA | Mycotin | 0 | 2021 | 1 | NEGA |
| Janani Pharmacy | HANWELLA | Mycotin | 0 | 2021 | 2 | NEGA |
| Janani Pharmacy | HANWELLA | Mycotin | 0 | 2021 | 3 | NEGA |
| Janani Pharmacy | HANWELLA | Mycotin | 10 | 2021 | 4 | NEGA |
| Janani Pharmacy | HANWELLA | Mycotin | 0 | 2021 | 5 | NEGA |
| Janani Pharmacy | HANWELLA | Mycotin | 0 | 2021 | 6 | NEGA |
| Janani Pharmacy | HANWELLA | Mycotin | 10 | 2021 | 7 | NEGA |
| Janani Pharmacy | HANWELLA | Mycotin | 0 | 2021 | 8 | NEGA |
| Janani Pharmacy | HANWELLA | Mycotin | 0 | 2021 | 9 | NEGA |
| Janani Pharmacy | HANWELLA | Mycotin | 0 | 2021 | 10 | NEGA |
| Janani Pharmacy | HANWELLA | Mycotin | 0 | 2021 | 11 | NEGA |
| Janani Pharmacy | HANWELLA | Mycotin | 0 | 2021 | 12 | NEGA |
我也尝试过使用左连接,但它不起作用。
【问题讨论】:
-
很常见的问题...谷歌日历表。
标签: sql sql-server tsql