简体   繁体   中英

Issue writing Count SQL query and displaying results

I am having an issue converting this query into a Count query, is anyone able to help. I basically want to count the number of records returned for this date, and ideally show it in a table that says Database Name and then total count. EG In this example would be Test then 6

SELECT
    test.dbo.OrderedDocuments.UserDocumentID, 
    OrderedDocuments.OrderGroupID, OrderGroups.TimePlaced 
FROM
    OrderedDocuments 
INNER JOIN 
    OrderGroups ON OrderedDocuments.OrderGroupID = OrderGroups.OrderGroupID
WHERE 
    OrderGroups.TimePlaced > '2018-05-17'

If, you want to display count then use count() function :

For instance :

SELECT count(*) Total_count
FROM  OrderedDocuments 
INNER JOIN OrderGroups 
           ON OrderedDocuments.OrderGroupID = OrderGroups.OrderGroupID
WHERE OrderGroups.TimePlaced >'2018-05-17';

If, you want to get departmental count then use GROUP BY clause to get departmental count

From my understanding (assuming UserDocumentID is the field you want to see the counts of) you would want to do this:

SELECT od.UserDocumentID, COUNT(*) as Total
FROM  OrderedDocuments od
INNER JOIN OrderGroups og
   ON od.OrderGroupID = og.OrderGroupID
WHERE og.TimePlaced >'2018-05-17'
GROUP BY od.UserDocumentID

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM