Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 multiple data from same table?

Author  Topic 

msugradus
Starting Member

40 Posts

Posted - 2007-06-18 : 15:38:09
I need to get the total reps by total and active status for the last 30 days.

There are two tables: Orders, and Customers. Orders just has the order date while customers has type and status columns.

Total: Customers.Status = any, customers.type = 'distributor'
Active: Customers.Status = 'Active' customers.type = 'distributor'

how would i do a sum on these two for the same report when both of the sums use different values?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2007-06-18 : 15:56:20
Just union the two result sets together.

Tara Kizer
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

msugradus
Starting Member

40 Posts

Posted - 2007-06-18 : 16:43:16
how?

SELECT SUM(CASE WHEN OrderDate >= dateadd(day, - 30, getdate()) THEN 1 ELSE 0 END) AS Total, exigo_data_sync.Customer.State,
dbo.Rank.RankCode
FROM exigo_data_sync.Orders INNER JOIN
exigo_data_sync.Customer ON exigo_data_sync.Orders.CustomerID = exigo_data_sync.Customer.CustomerID INNER JOIN
dbo.Rank ON exigo_data_sync.Customer.RankID = dbo.Rank.RankID
GROUP BY exigo_data_sync.Customer.CustomerType, exigo_data_sync.Customer.CustomerStatus, exigo_data_sync.Customer.State,
dbo.Rank.RankCode
HAVING (exigo_data_sync.Customer.CustomerType = 'Distributor') and (exigo_data_sync.Customer.Customerstatus = 'Active')

ORDER BY exigo_data_sync.Customer.State



SELECT SUM(CASE WHEN OrderDate >= dateadd(day, - 30, getdate()) THEN 1 ELSE 0 END) AS QuarterCount, exigo_data_sync.Customer.State,
dbo.Rank.RankCode
FROM exigo_data_sync.Orders INNER JOIN
exigo_data_sync.Customer ON exigo_data_sync.Orders.CustomerID = exigo_data_sync.Customer.CustomerID INNER JOIN
dbo.Rank ON exigo_data_sync.Customer.RankID = dbo.Rank.RankID
GROUP BY exigo_data_sync.Customer.CustomerType, exigo_data_sync.Customer.CustomerStatus, exigo_data_sync.Customer.State,
dbo.Rank.RankCode
HAVING (exigo_data_sync.Customer.CustomerType = 'Distributor')
ORDER BY exigo_data_sync.Customer.State
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2007-06-18 : 16:45:40
SELECT ...
FROM YourTable
UNION ALL
SELECT ...
FROM YourTable

Query1
UNION ALL
Query2
UNION ALL
Query3
...


Tara Kizer
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-06-18 : 17:07:31
They are not different values. Active is just a subset of Total, thus creating a duplicate row in the resultset for Status = 'Active' record.

This would do the same thing and probably faster, since there is only one SELECT with multiple joins.
SELECT		SUM(CASE WHEN OrderDate >= dateadd(day, - 30, getdate()) THEN 1 ELSE 0 END) AS OrderCount,
exigo_data_sync.Customer.CustomerStatus,
exigo_data_sync.Customer.State,
dbo.Rank.RankCode
FROM exigo_data_sync.Orders
INTO #Temp
INNER JOIN exigo_data_sync.Customer ON exigo_data_sync.Orders.CustomerID = exigo_data_sync.Customer.CustomerID
INNER JOIN dbo.Rank ON exigo_data_sync.Customer.RankID = dbo.Rank.RankID
WHERE exigo_data_sync.Customer.CustomerType = 'Distributor'
GROUP BY exigo_data_sync.Customer.CustomerStatus,
exigo_data_sync.Customer.State,
dbo.Rank.RankCode

SELECT OrderCount,
CustomerStatus,
State,
RankCode
FROM (
SELECT OrderCount,
CustomerStatus,
State,
RankCode
FROM #Temp

UNION ALL

SELECT SUM(OrderCount),
'Active',
State,
RankCode
FROM #Temp
WHERE CustomerStatus = 'Active'
GROUP BY State,
RankCode
)
ORDER BY State

DROP TABLE #Temp

Peter Larsson
Helsingborg, Sweden
Go to Top of Page
   

- Advertisement -