Sqlserver
 sql >> डेटाबेस >  >> RDS >> Sqlserver

SQL दो अलग-अलग दिनांक सीमाओं के डेटा को एक परिणाम सेट में संयोजित करें

SELECT
    Period = 'QTD',
    Company,
    SUM(Call) as TotalCallsQTD,
    SUM(Call) - SUM(SoldCall) as FreeCallsQTD,
    SUM(SalableCall) as SalesRelatedCallsQTD,
    SUM(SoldCall) as SoldCallsQTD
FROM
    #tmpAllSales
WHERE
    CalledOn between @StartDate and @EndDate
GROUP BY
    Company
UNION ALL
SELECT
    Period = 'YTD',
    Company,
    SUM(Call) as TotalCallsYTD,
    SUM(Call) - SUM(SoldCall) as FreeCallsYTD,
    SUM(SalableCall) as SalesRelatedCallsYTD,
    SUM(SoldCall) as SoldCallsYTD
FROM
    #tmpAllSales
GROUP BY
    Company
ORDER BY
    Period, Company;

यदि आप उन्हें प्रत्येक कंपनी के लिए एक पंक्ति के रूप में चाहते हैं, तो:

;WITH q AS
(
    SELECT
        Company,
        SUM(Call) as TotalCallsQTD,
        SUM(Call) - SUM(SoldCall) as FreeCallsQTD,
        SUM(SalableCall) as SalesRelatedCallsQTD,
        SUM(SoldCall) as SoldCallsQTD
    FROM
        #tmpAllSales
    WHERE
        CalledOn between @StartDate and @EndDate
    GROUP BY
        Company
), y AS 
(
    SELECT
        Company,
        SUM(Call) as TotalCallsYTD,
        SUM(Call) - SUM(SoldCall) as FreeCallsYTD,
        SUM(SalableCall) as SalesRelatedCallsYTD,
        SUM(SoldCall) as SoldCallsYTD
    FROM
        #tmpAllSales
    GROUP BY
        Company
)
SELECT q.Company, qTotalCallsYTD = q.TotalCallsYTD, etc...,
    yTotalCallsYTD = y.TotalCallsYTD, etc...
FROM q INNER JOIN y
ON q.Company = y.Company
ORDER BY q.Company;



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. मैं ASP.NET का उपयोग करके SQL सर्वर से एक छवि कैसे प्रदर्शित कर सकता हूं?

  2. दो तालिकाओं में शामिल हों, केवल सही तालिका के नवीनतम मान का उपयोग करें

  3. क्या SQL सर्वर में LastIndexOf है?

  4. बड़े डेटासेट से डुप्लिकेट हटाएं (>100Mio पंक्तियाँ)

  5. SQL सर्वर:चर के माध्यम से वर्तमान डेटाबेस बदलें