Multi-count query (1 Viewer)

JamieRhysEdwards

New member
Local time
Today, 00:24
Joined
Mar 26, 2022
Messages
27
Hi all,

I'm wondering if I am able to create a query which houses all of the counts for a certain topic.

For example, I have assets which are broken down into types:

Asset Count - Count of all the assets within the table.
Desktop Count - Count of only the desktop assets within the table
Laptop Count - Count of only the laptop assets within the table

etc...

Is it possible to have this in a single query wherein I have all the counts in their own rows? Or is it a case that I would need to run a query for each type?

Thanks


EDIT :-

However... For the type, I have a lookup table wherein I have an ID field and then the Type field. so the structure would be as follows:

IDTypePrefix
1DesktopDT
2LaptopLT
 
Last edited:

plog

Banishment Pending
Local time
Yesterday, 18:24
Joined
May 11, 2011
Messages
11,638
As long as you have set up your tables correctly, this is simple.

 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 16:24
Joined
Oct 29, 2018
Messages
21,453
I think you can have all the counts in one row but separate columns. If you want them all in one column but separate rows, you will need multiple queries to combine into one.

Edit: Ahh, too slow... Maybe I'm wrong. Cheers!
 

GPGeorge

Grover Park George
Local time
Yesterday, 16:24
Joined
Nov 25, 2004
Messages
1,829
Hi all,

I'm wondering if I am able to create a query which houses all of the counts for a certain topic.

For example, I have assets which are broken down into types:

Asset Count - Count of all the assets within the table.
Desktop Count - Count of only the desktop assets within the table
Laptop Count - Count of only the laptop assets within the table

etc...

Is it possible to have this in a single query wherein I have all the counts in their own rows? Or is it a case that I would need to run a query for each type?

Thanks
It partly depends on table structure, doesn't it?

However, assuming this is a table with an "AssetType" and an "AssetQuantity" field, then a Union query would do the trick.
 

JamieRhysEdwards

New member
Local time
Today, 00:24
Joined
Mar 26, 2022
Messages
27
Are you able to upload a copy of the database without any Confidential data?
I really wish I could, however I've split my database and the linked tables are on a network drive... meaning it would not be usable in any way if I tried to mess with it
 

mike60smart

Registered User.
Local time
Today, 00:24
Joined
Aug 6, 2017
Messages
1,908
I really wish I could, however I've split my database and the linked tables are on a network drive... meaning it would not be usable in any way if I tried to mess with it
We just need a copy of both the FE and BE. You don't have to change anything.
 

cheekybuddha

AWF VIP
Local time
Today, 00:24
Joined
Jul 21, 2014
Messages
2,272
I've updated OP on how my table is kinda structured
Explicit descriptions of the tables, fields, datatypes and relationships involved would have been better!

Guessing from what you have posted, you can try the following (adapt table/field names as applicable):

To just get counts of different types of assets in the Assets table:
SQL:
SELECT
  AssetTypeID,
  COUNT(*) AS CountOfAsset
FROM Assets
GROUP BY
  AssetTypeID
;

To get a list of the asset types and their counts:
SQL:
SELECT
  at.AssetType,
  a.CountOfAsset
FROM (
  SELECT
    AssetTypeID,
    COUNT(*) AS CountOfAsset
  FROM Assets
  GROUP BY
    AssetTypeID
) a
INNER JOIN AssetTypes at
        ON a.AssetTypeID = at.AssetTypeID
;

A list of totals by asset type:
SQL:
SELECT
  COUNT(*) AS TotalAssets,
  SUM(IIf(t.AssetType = 'Laptop', 1, 0)) AS TotalLaptops,
  SUM(IIf(t.AssetType = 'Desktop', 1, 0)) AS TotalDesktops
FROM (
  SELECT
    a.AssetID,
    at.AssetType
  FROM Assets a
  INNER JOIN AssetTypes at
          ON a.AssetTypeID = at.AssetTypeID
) t
;
Or, since you know the AssetTypeID's, more simply:
SQL:
SELECT
  COUNT(*) AS TotalAssets,
  SUM(IIf(a.AssetTypeID = 1, 1, 0)) AS TotalDesktops,
  SUM(IIf(a.AssetTypeID = 2, 1, 0)) AS TotalLaptops
FROM Assets a
;
 

Gasman

Enthusiastic Amateur
Local time
Today, 00:24
Joined
Sep 21, 2011
Messages
14,231
Join the lookup table with the assets table, then group by asset and count of asset?
 

Users who are viewing this thread

Top Bottom