我是SQL的新手,并尝试获取一个应该返回多天结果的SQL查询。但是它只返回所选日期之一的数据,而不是所有日期。我已经尝试删除“DISTINCT”语句,但之后它只返回多个重复结果,但所有这些都在同一天再次发生。任何suugestions将不胜感激。谢谢!
declare @Check_Date smalldatetime
set @Check_Date = convert(varchar(8), DATEADD( DAY , -3, CURRENT_TIMESTAMP ), 112)
SELECT
DISTINCT [SIS].[dbo].[Stores].[StoreNbr] AS 'Store'
,[SIS].[dbo].[Stores].[StoreName] AS 'Name'
,[SIS].[dbo].[STORE_CALENDAR].[TRXN_DATE] AS 'Date'
,case when [SIS].[dbo].[STORE_POLLING_EXCEPTIONS].[REASON_MISSED] is null then substring([SIS].[dbo].[STORE_POLLING_EXCEPTIONS].[REASON_MISSED],1,49) else 'Not Yet Polled' end as 'Reason Missed'
FROM
[SIS].[dbo].[DAILY_TOTALS], [SIS].[dbo].[Stores], [SIS].[dbo].[STORE_POLLING_EXCEPTIONS], [SIS].[dbo].[STORE_CALENDAR]
WHERE
[SIS].[dbo].[DAILY_TOTALS].[STORE_NUMBER] = [SIS].[dbo].[Stores].[StoreNbr]
AND [SIS].[dbo].[DAILY_TOTALS].[TRXN_DATE] = @Check_Date
AND [SIS].[dbo].[DAILY_TOTALS].[TRXN_DATE] = [SIS].[dbo].[STORE_CALENDAR].[TRXN_DATE]
AND [SIS].[dbo].[DAILY_TOTALS].[NET_SALES_AMOUNT] IS NULL
AND [SIS].[dbo].[Stores].[StoreNbr] IN (
SELECT [SIS].[dbo].[STORE_CALENDAR].[StoreNbr]
FROM [SIS].[dbo].[STORE_CALENDAR]
WHERE [SIS].[dbo].[STORE_CALENDAR].[TRXN_DATE] = @Check_Date
)
AND [SIS].[dbo].[Stores].[StoreNbr] IN (
SELECT [SIS].[dbo].[Stores].[StoreNbr]
FROM [SIS].[dbo].[Stores]
WHERE [SIS].[dbo].[Stores].[ClosingOps] IS NULL
)
AND [SIS].[dbo].[Stores].[StoreNbr] NOT IN (
SELECT [SIS].[dbo].[STORE_POLLING_EXCEPTIONS].[STORE_NUMBER]
FROM [SIS].[dbo].[STORE_POLLING_EXCEPTIONS]
WHERE [SIS].[dbo].[STORE_POLLING_EXCEPTIONS].[TRXN_DATE] = @Check_Date
AND [SIS].[dbo].[STORE_POLLING_EXCEPTIONS].[REASON_MISSED] IS NOT NULL
)
UNION ALL
SELECT
DISTINCT [SIS].[dbo].[Stores].[StoreNbr] AS 'Store'
,[SIS].[dbo].[Stores].[StoreName] AS 'Name'
,[SIS].[dbo].[STORE_CALENDAR].[TRXN_DATE] AS 'Date'
,[SIS].[dbo].[STORE_POLLING_EXCEPTIONS].[REASON_MISSED] AS 'Reason Missed'
FROM
[SIS].[dbo].[DAILY_TOTALS], [SIS].[dbo].[Stores], [SIS].[dbo].[STORE_POLLING_EXCEPTIONS], [SIS].[dbo].[STORE_CALENDAR]
WHERE
[SIS].[dbo].[DAILY_TOTALS].[STORE_NUMBER] = [SIS].[dbo].[Stores].[StoreNbr]
AND [SIS].[dbo].[Stores].[StoreNbr] = [SIS].[dbo].[STORE_POLLING_EXCEPTIONS].[STORE_NUMBER]
AND [SIS].[dbo].[DAILY_TOTALS].[TRXN_DATE] = @Check_Date
AND [SIS].[dbo].[DAILY_TOTALS].[TRXN_DATE] = [SIS].[dbo].[STORE_CALENDAR].[TRXN_DATE]
AND [SIS].[dbo].[STORE_POLLING_EXCEPTIONS].[TRXN_DATE] = @Check_Date
AND [SIS].[dbo].[DAILY_TOTALS].[NET_SALES_AMOUNT] IS NULL
AND [SIS].[dbo].[Stores].[StoreNbr] IN (
SELECT [SIS].[dbo].[STORE_CALENDAR].[StoreNbr]
FROM [SIS].[dbo].[STORE_CALENDAR]
WHERE [SIS].[dbo].[STORE_CALENDAR].[TRXN_DATE] = @Check_Date
)
AND [SIS].[dbo].[Stores].[StoreNbr] IN (
SELECT [SIS].[dbo].[Stores].[StoreNbr]
FROM [SIS].[dbo].[Stores]
WHERE [SIS].[dbo].[Stores].[ClosingOps] IS NULL
)
ORDER BY 'Date', 'Store'
答案 0 :(得分:0)
我尝试了什么以及为了解问题而采取的行动:
总而言之,我们需要知道
至于为什么你没有看到数据:
SELECT
S.[StoreNbr] AS 'Store'
,S.[StoreName] AS 'Name'
,SC.[TRXN_DATE] AS 'Date'
,case when SPE.[REASON_MISSED] is not null then
substring(SPE.[REASON_MISSED],1,49)
else 'Not Yet Polled' end as 'Reason Missed'
FROM [SIS].[dbo].[DAILY_TOTALS] DT
RIGHT JOIN [SIS].[dbo].[Stores] S
ON DT.[STORE_NUMBER] = S.[StoreNbr]
LEFT JOIN [SIS].[dbo].[STORE_CALENDAR] SC
ON DT.[TRXN_DATE] = SC.[TRXN_DATE]
and DT.[TRXN_DATE] = @Check_Date
LEFT JOIN [SIS].[dbo].[STORE_POLLING_EXCEPTIONS] SPE
ON S.[StoreNbr] = SPE.[Store_Number] --This likely wrong but no associations identified.
and SC.[TRXN_DATE] = SPE.[TRXN_DATE] --Perhaps this is needed as well?
WHERE DT.[NET_SALES_AMOUNT] IS NULL
AND S.[ClosingOps] IS NULL
/* What's this do?
AND S.[StoreNbr] NOT IN (
SELECT [SIS].[dbo].[STORE_POLLING_EXCEPTIONS].[STORE_NUMBER]
FROM [SIS].[dbo].[STORE_POLLING_EXCEPTIONS]
WHERE [SIS].[dbo].[STORE_POLLING_EXCEPTIONS].[TRXN_DATE] = @Check_Date
AND [SIS].[dbo].[STORE_POLLING_EXCEPTIONS].[REASON_MISSED] IS NOT NULL
)
* / ---第一部分没有任何关于下一步的思考,其余的问题仍然存在。