我有2个表SALM(Sales table)
提交了BillNo,Date,Amount,CusId
,而CUSMAS(Customer table)
提交了ID,CustomerName
。我想创建一个报告(介于日期之间),报告的顶部放在日期和销售明细( (低于SALM)的价格,则低于该日期的总销售额。
输入项:从日期到日期
我已经尝试过以下查询:
(SELECT DISTINCT SALM.INVDATE AS RES1,'' AS RES2 ,'' AS RES3 FROM SALM
WHERE SALM.INVDATE BETWEEN #01-Jan-2018# AND #01-Mar-2019# ORDER BY
SALM.INVDATE) UNION (SELECT SALM.ORDNO AS RES1, ACCMAS.ACCNAME AS RES2,''
AS RES3 FROM SALM INNER JOIN ACCMAS ON SALM.CUSTCODE = ACCMAS.ID WHERE
SALM.INVDATE BETWEEN #01-Jan-2018# AND #01-Mar-2019# ORDER BY SALM.INVDATE
UNION select SUM(SALM.AMOUNT) AS RES1, sum(SALM.TAX) AS RES2,
sum(SALM.NTVALUE) AS RES3 FROM SALM WHERE SALM.INVDATE
BETWEEN #01-Jan-2018# AND #01-Mar-2019# group by SALM.INVDATE );
但是结果没有达到我所需的格式
报告格式如图所示
答案 0 :(得分:1)
您将只能在一种SQL表达式中执行此操作。
使用T-SQL(MSSSQL)时,可以在一个给定的日期使用此表达式。
SELECT '' AS 'Bill No.', CONCAT('Date: ', '03.04.2018') AS 'Customer Name', '' AS 'Amount'
UNION ALL
SELECT CONVERT(varchar, BillNo), CustomerName, CONVERT(varchar, Amount)
FROM SALM LEFT JOIN CUSMAS ON SALM.CusId = CUSMAS.Id WHERE Date = '3.4.2018'
UNION ALL
SELECT '', 'Cash Total', 'don''t know what you want in herer'
UNION ALL
SELECT '', 'Credit Total', 'don''t know what you want in herer'
UNION ALL
SELECT '', 'Day Total', CONVERT(varchar, SUM(Amount))
FROM SALM LEFT JOIN CUSMAS ON SALM.CusId = CUSMAS.Id WHERE Date = '3.4.2018'
然后,您可以在C#中通过所有可能的DateTimes
循环进行此操作,并对每个DateTime
使用表达式。
当您只想调用一个SQL函数时,则需要创建一个返回表的FUNCTION
。您可以了解有关此here的更多信息。
CREATE FUNCTION GetMyReport(@from DATETIME, @to DATETIME)
RETURNS @result TABLE
(
BillNo VARCHAR,
CustomerName VARCHAR,
Amount VARCHAR
)
BEGIN
--FILL YOUR TABLE HERE
END;