Select Count on multiple tables in one query - tsql

Can someone help me with this TSQL syntax? I am trying to do
Select Count(Distinct Value) AS OpenPO
FROM OpenPOs
AND
Select Count(Distinct Value) As OpenSho
From OpenSho
etc.
How do I make this into one neat query with results like its all part of a single table or view?

Try:
SELECT (Select Count(Distinct Value) FROM OpenPOs) AS OpenPO,
(Select Count(Distinct Value) From OpenSho) AS OpenSho
You will get as result something like:
OpenPO OpenSho
---------------
3 5

Related

How to use the AS name in a query WHERE clause?

given a query like so:
SELECT
id,
(SELECT COUNT(*)
FROM members
WHERE members.network_id = networks.id) AS mem_count
FROM
networks
WHERE mem_count > 2
With this query, the where clause breaks as it does not know what mem_count is... Why can't I use the as var in the where clause?
Thanks
While bernie suggested correct answer to the question, your query can be simplified to:
SELECT
network_id as id,
count(*)
FROM
members
GROUP BY
network_id
HAVING
count(*) > 2
Which, as an additional bonus, can be faster.
You've got the concept down. You just need the right syntax. You could re-write like this and have the added benefit of making your query ANSI-compliant:
SELECT
id,
m.mem_count
FROM
networks n
JOIN (
SELECT m.network_id,
COUNT(*) AS mem_count
FROM members
GROUP BY m.network_id
) m
ON m.network_id = n.id
AND m.mem_count > 2;
Try:
SELECT
id,
(SELECT COUNT(*) as mem_count
FROM members
WHERE members.network_id = networks.id)
FROM
networks
WHERE mem_count > 2
One way would be.
Select * From (
SELECT
id,
(SELECT COUNT(*)
FROM members
WHERE members.network_id = networks.id) AS mem_count
FROM
networks)) mem_counts
WHERE mem_count > 2
A join as suggested by Bernie would be better though. Basically you confused the parser. You get the same sort of issue with group by or order by when you use AS to alias a column name.

PostgreSQL Select combine 2 WITH query

I made a select using the WITH query something like this:
WITH test AS (select id, sum(value) as value from test group by id)
SELECT name, t.value from names JOIN test t ON (t.id = names.id)
But I need to use 2 WITH queries, is there any way to make it?
WITH
name1 AS (SELECT ...),
name2 AS (SELECT ...)
SELECT ...

Postgres: select the sum of values and then sum this again

I tried a lot but can´t find the right way.
If I select values in Postgres and sum them it looks like this:
SELECT name,sum(size) as total
FROM mytable group by name order by name;
How can I alter this so it also sum all values in total? I think I need a subselect but how?
Try this:
SELECT sum(a.total)
FROM (SELECT sum(size) as total
FROM mytable group by name) a
UPDATE
I'm sorry, I don't read that you want it all in the same query. For this reason the answer of greg it's better. However, other possibility if you have a postgresql version >= 9:
WITH mytableWith (name, sum) as
(SELECT name, sum(size)
FROM mytable
GROUP BY name)
SELECT 'grand total' AS name,
sum(sum) AS sum
FROM mytableWith
UNION ALL
SELECT name, sum
FROM mytableWith
I would use the ROLLUP function on POSTRGESQL:
SELECT name,sum(size) as total
FROM mytable
group by ROLLUP(name )
order by name;
This will give you a grand total of any value that you aggregate and can also be used for aggregating multiple columns.
Hope it helps!
If you want all results with the same SELECT, you could do something like
SELECT
'grand total' AS name,
sum(size) AS sum
FROM
mytable
UNION ALL
SELECT
name,
sum(size) AS sum
FROM
mytable
GROUP BY
name;
Hope it helps…
Well this should help you:
select sum(innerselect.innertotal) as outertotal from
(select sum(size) as innertotal from mytable group by name) as innerselect

T-SQL how to count the number of duplicate rows then print the outcome?

I have a table ProductNumberDuplicates_backups, which has two columns named ProductID and ProductNumber. There are some duplicate ProductNumbers. How can I count the distinct number of products, then print out the outcome like "() products was backup." ? Because this is inside a stored procedure, I have to use a variable #numrecord as the distinct number of rows. I put my codes like this:
set #numrecord= select distinct ProductNumber
from ProductNumberDuplicates_backups where COUNT(*) > 1
group by ProductID
having Count(ProductNumber)>1
Print cast(#numrecord as varchar)+' product(s) were backed up.'
obviously the error was after the = sign as the select can not follow it. I've search for similar cases but they are just select statements. Please help. Many thanks!
Try
select #numrecord= count(distinct ProductNumber)
from ProductNumberDuplicates_backups
Print cast(#numrecord as varchar)+' product(s) were backed up.'
begin tran
create table ProductNumberDuplicates_backups (
ProductNumber int
)
insert ProductNumberDuplicates_backups(ProductNumber)
select 1
union all
select 2
union all
select 1
union all
select 3
union all
select 2
select * from ProductNumberDuplicates_backups
declare #numRecord int
select #numRecord = count(ProductNumber) from
(select ProductNumber, ROW_NUMBER()
over (partition by ProductNumber order by ProductNumber) RowNumber
from ProductNumberDuplicates_backups) p
where p.RowNumber > 1
print cast(#numRecord as varchar) + ' product(s) were backed up.'
rollback

Two different group by clauses in one query?

First time posting here, a newbie to SQl, and I'm not exactly sure how to word this but I'll try my best.
I have a query:
select report_month, employee_id, split_bonus,sum(salary) FROM empsal
where report_month IN('2010-12-01','2010-11-01','2010-07-01','2010-04-01','2010-09-01','2010-10-01','2010-08-01')
AND employee_id IN('100','101','102','103','104','105','106','107')
group by report_month, employee_id, split_bonus;
Now, to the result of this query, I want to add a new column split_bonus_cumulative that is essentially equivalent to adding a sum(split_bonus) in the select clause but for this case, the group buy should only have report_month and employee_id.
Can anyone show me how to do this with a single query? Thanks in advance.
Try:
SELECT
report_month,
employee_id,
SUM(split_bonus),
SUM(salary)
FROM
empsal
WHERE
report_month IN('2010-12-01','2010-11-01','2010-07-01','2010-04-01','2010-09-01','2010-10-01','2010-08-01')
AND
employee_id IN('100','101','102','103','104','105','106','107')
GROUP BY
report_month,
employee_id;
Assuming you're using Postgres, you might also find window functions useful:
http://www.postgresql.org/docs/9.0/static/tutorial-window.html
Unless I'm mistaking, you want something that resembles the following:
select report_month, employee_id, salary, split_bonus,
sum(salary) over w as sum_salary,
sum(split_bonus) over w as sum_bonus
from empsal
where ...
window w as (partition by employee_id);
CTEs are also convenient:
http://www.postgresql.org/docs/9.0/static/queries-with.html
WITH
rows as (
SELECT foo.*
FROM foo
WHERE ...
),
report1 as (
SELECT aggregates
FROM rows
WHERE ...
),
report2 as (
SELECT aggregates
FROM rows
WHERE ...
)
SELECT *
FROM report1, report2, ...