Number of Groups returned by a query - sql

I am using an SQL query with of form
SELECT...FROM...WHERE...GROUP BY id
I want to know how many groups this query returns. How to do this?

SELECT count(id) FROM....WHERE...GROUP BY id

#Tyler Ferraro answers should solve it.
In case the SQL query is very complicated, you can use a nested query:
SELECT COUNT(*) FROM (SELECT...FROM...WHERE...GROUP BY id)

Try this:
select count(*) from (Your SQL).

Related

How to extract duplicate orders from DB

Using SQL Server 2014. Taking the following recordset:
I need to find a way to extract the ordOrderNum which is duplicates, with a different DeliveryNum. In this example, orders 93400460 and 93400467 would be extract, because they are duplicates. Order 93408170 is ok. How can I do that??!
thanks for your time and help
You can use group by and having:
select ordOrderNum
from mytable
group by ordOrderNum
having min(ordDeliveryNum) <> max(ordDeliveryNum)
Try this:
SELECT
ORDORDERNUM, ORDLINENUM,
COUNT(*) FROM TABLE
GROUP BY
ORDORDERNUM, ORDLINENUM
Having count(*)>1

SQL query missing select statement fetching data error

I am using SQL query fetching some issue data is not fetching black show.
I am sharing this query here. Please help me.
SQL query here
select * from products where hitproduct='0' ORDER BY id DESC and user_id='$user_id'
A SQL query only has one where clause. Presumably, you intend:
select p.*
from products p
where user_id = ? and
hitproduct = 0 -- looks like a number, so I assume it is
order by id desc;
Note the use of ?. This represents a parameter placeholder. Don't munge query strings with parameters values! Learn to use parameters properly.
In your given query and user_id='$user_id' should be before ORDER BY.
select *
from products
where hitproduct='0' and user_id='$user_id'
ORDER BY id DESC

Counting unique text in query

I need to count how many distinct shipping methods there are in a query (the answer is 2). I am trying to use DISTINCT but it doesn't seem to be working the way I thought it would.
SELECT DISTINCT Count(Order.ship_method) AS CountOfship_method
FROM [Order];
Try this instead -
SELECT COUNT(*) as CountOfship_method
FROM
(SELECT DISTINCT Order.ship_method FROM [Order]);

counting rows in select clause with DB2

I would like to query a DB2 table and get all the results of a query in addition to all of the rows returned by the select statement in a separate column.
E.g., if the table contains columns 'id' and 'user_id', assuming 100 rows, the result of the query would appear in this format: (id) | (user_id) | 100.
I do not wish to use a 'group by' clause in the query. (Just in case you are confused about what i am asking) Also, I could not find an example here: http://mysite.verizon.net/Graeme_Birchall/cookbook/DB2V97CK.PDF.
Also, if there is a more efficient way of getting both these results (values + count), I would welcome any ideas. My environment uses zend framework 1.x, which does not have an ODBC adapter for DB2. (See issue http://framework.zend.com/issues/browse/ZF-905.)
If I understand what you are asking for, then the answer should be
select t.*, g.tally
from mytable t,
(select count(*) as tally
from mytable
) as g;
If this is not what you want, then please give an actual example of desired output, supposing there are 3 to 5 records, so that we can see exactly what you want.
You would use window/analytic functions for this:
select t.*, count(*) over() as NumRows
from table t;
This will work for whatever kind of query you have.

TOP function in SQL Server 2005 not giving correct answer

How can I use TOP function in SQL Server 2005 on a single column in a table along with count function?
I am getting only one count for this query, where I have 35 entries that should come.
this is my query
select top(1) room_no, count(room_no) from rooms
Seems like what you want is the following:
select room_no,count(room_no)
from rooms
group by room_no
BTW, I wonder why it would execute without the group by. Should throw an error.
COUNT function is evaluated after TOP. That is why you are only getting one for the count. What you want is something like this
SELECT TOP(1) dbo.Table.Column1, (SELECT COUNT(*) FROM dbo.Table)
FROM dbo.Table
Also as mentioned you really should be ordering by something.
Edit: This also works:
SELECT TOP(1) dbo.Table.Column1, COUNT(*) OVER() AS Total
FROM dbo.Table
Seems to be more efficient as well (only tested on small dataset though).