### Description

A count of datasets that are currently registered/declared in an Airflow deployment.

### Use case/motivation

Would be nice to see how deployments are adopting datasets.

### Related issues

_No response_

### Are you willing to submit a PR?

- [ ] Yes I am willing to submit a PR!

### Code of Conduct

- [X] I agree to follow this project's [Code of Conduct](https://github.com/apache/airflow/blob/main/CODE_OF_CONDUCT.md)

"""
        Detects datasets that are no longer referenced in any DAG schedule parameters or task outlets and
        sets the dataset is_orphaned flag to True
        """
        orphaned_dataset_query = (
            session.query(DatasetModel)
            .join(
                DagScheduleDatasetReference,
                isouter=True,
            )
            .join(
                TaskOutletDatasetReference,
                isouter=True,
            )
            
            
            .group_by(DatasetModel if session.get_bind().dialect.name == "mssql" else DatasetModel.id)
            .having(
                and_(
                    func.count(DagScheduleDatasetReference.dag_id) == 0,
                    func.count(TaskOutletDatasetReference.dag_id) == 0,
                )
            )
        )
        for dataset in orphaned_dataset_query:
            self.log.info("Orphaning unreferenced dataset '%s'", dataset.uri)
            dataset.is_orphaned = expression.true()