Group by a count() clause in SQL Server - sql-server

I'm trying to write a SQL query that will return a list of aggregated values; however, I want to group the query by one of the aggregated values (a count):
select t.Field1, count(distinct(t.Field2), SUM(t.Value1)
from MyTable t
group by t.Field1, count(t.Field2)
I've tried putting the count into a subquery, and putting the whole query into a subquery and grouping there. Is there an way to do this that doesn't involve creating a temporary table (I don't have anything against temporary tables per se).
The desired outcome would look like this:
Field1 Count Sum
----------------------------------------------------
CAT1 3 19.5
CAT1 2 100
CAT2 2 62
The data that I'm working with looks like this:
Field1 Field2 Field3 Value1
-----------------------------------------------------
CAT1 1 1 5
CAT1 2 1 2.5
CAT1 3 1 12
CAT1 4 2 50
CAT1 5 2 50
CAT2 6 3 50
CAT2 7 3 12
So, I want a grouping by the number of distinct Field2 values per Field3

If I understand you correctly, then the follow should work.
select Field1 , Count , Sum(Value1)
from
(
select t.Field1, count(*) as Count, SUM(t.Value1) as Value1
from MyTable t
group by t.Field1, t.Field3
)
as t2
group by Field1, Count

Related

List combination in where clause

Here is the scenerio, I have a input data and a table table1
Input Data Table1
Customer Id Campaign ID CustomerId CampaignID
1 1 4 2
1 2 6 3
2 3 1 1
1 3 5 5
4 2 9 8
4 4
5 5
I want to query table1 such that it return only those values from the where clause which are not present in table1. So the result will be as below
Result
Customer Id Campaign ID
1 2
2 3
1 3
4 4
5 5
So the query should be something like
select CustomerId, CampaignID from Table1
where Customer Id in (Input data for customer id) and CampaignId in (Input data for campaign id)
. I know this query is not right, but can someone please help.
Is there a way to filter the values given in where clause based on if they are present in table1?
P.S. table1 primary key (CustomerId, CampaignID)
This will work as for your scenario. But it wont show last result record 5,5 since it does not fulfill your need.
select * from input where (cust_id, camp_id) not in (select cust_id, camp_id from table1)

How to select the value from the table based on category_id USING SQL SERVER

How to select the value from the table based on category_id?
I have a table like this. Please help me.
Table A
ID Name category_id
-------------------
1 A 1
2 A 1
3 B 1
4 C 2
5 C 2
6 D 2
7 E 3
8 E 3
9 F 3
How to get the below mentioned output from table A?
ID Name category_id
--------------------
1 A 1
2 A 1
4 C 2
5 C 2
7 E 3
8 E 3
Give a row number for each row based on group by category_id and sort by ascending order of ID. Then select the rows having row number 1 and 2.
Query
;with cte as (
select [rn] = row_number() over(
partition by [category_id]
order by [ID]
), *
from [your_table_name]
)
select [ID], [Name], [category_id]
from cte
where [rn] < 3;
Kindly run this query It really help You Out.
SELECT tbl.id,tbl.name, tbl.category_id FROM TableA as tbl WHERE
tbl.name IN(SELECT tbl2.name FROM TableA tbl2 GROUP BY tbl2.name HAVING Count(tbl2.name)> 1)
Code select all category_id from TableA which has Name entries more then one. If there is single entry of any name group by category_id then such data will be excluded. In above example questioner want to eliminate those records that have single Name entity like wise category_id 1 has name entries A and B among which A has two entries and B has single entry so he want to eliminate B from result set.

MSSQL selecting distinct ID based on set of parameters

I'm sure this is simple, but it's been a rough day:
I have a table of data like below. I have a list of attribute IDs and need to get the distinct ProductID that matches all the AttributeIDs selected.
ProductID AttributeID
1 2
1 3
1 5
2 2
2 3
2 7
As an example, I want the distinct product ID that matches attributes 2, 3, AND 5.
What's the most optimal way to do this?
using count(distinct ) = n where n is the number of elements in the where AttributeId in () list.
select ProductId
from t
where AttributeId in (2,3,5)
group by ProductId
having count(distinct AttributeId)=3

Slow SQL query with SQL Server

I have two SQL queries to count co-occurrences between id2 values among different id1 values. The sample table looks like
id1 | id2
101 | 1
101 | 2
101 | 3
102 | 2
102 | 3
102 | 4
103 | 15
103 | 3
103 | 4
and the desired output is:
A B Count
1 2 1
1 3 2
2 3 4
1 4 2
2 4 3
3 4 4
1 15 1
2 15 2
3 15 2
4 15 1
Both solutions are pasted below.
-- Solution 1
SELECT bar.id2 AS A, foo.id2 AS B, COUNT(*) AS Count
FROM
(SELECT * FROM TestTab) AS bar,
(SELECT * FROM TestTab) AS foo
WHERE bar.id1 <> foo.id1
AND bar.id2 < foo.id2
GROUP BY bar.id2, foo.id2
-- Solution 2
SELECT bar.id2 AS A, foo.id2 AS B, COUNT(*) AS Count
FROM TestTab AS bar
JOIN TestTab AS foo
ON bar.id1 <> foo.id1
WHERE bar.id2 < foo.id2
GROUP BY bar.id2, foo.id2
Both queries work fine on small tables (i.e., 100 - 1000 rows), but I need to query much larger table (e.g., 100.000 rows). I wonder how to speed up the queries and improve performance. Thanks in advance for any pointers.
- Create table TestTab and insert dummy data
CREATE TABLE TestTab
INSERT INTO TestTab VALUES
(101,1),
(101,2),
(101,3),
(102,2),
(102,3),
(102,4),
(103,15),
(103,3),
(103,4)
I suggest adding an index on id2 to TestTab (if one doesn't already exist) and then try running the following:
select distinct id2 into #id2 from TestTab;
SELECT bar.id2 AS A, foo.id2 AS B, COUNT(*) AS Count
FROM #id2 AS bar
JOIN #id2 AS foo ON bar.id2 < foo.id2
JOIN TestTab AS buz ON bar.id2 = buz.id2
JOIN TestTab AS fuz ON foo.id2 = fuz.id2
WHERE buz.id1 <> fuz.id1
GROUP BY bar.id2, foo.id2;
(If you already have a table with the distinct values of id2 on it, skip creating the temporary table and use that instead.)
Both queries are joins and equivalent.
The first one is an implicit join with additional subselects. It might be slower, if SQL Server doesn't optimize the subselects away.
As others already observed, add indexes to the join condition column id1 and the where clause column id2, if you haven't done so already.

T-SQL select rows by oldest date and unique category

I'm using Microsoft SQL. I have a table that contains information stored by two different categories and a date. For example:
ID Cat1 Cat2 Date/Time Data
1 1 A 11:00 456
2 1 B 11:01 789
3 1 A 11:01 123
4 2 A 11:05 987
5 2 B 11:06 654
6 1 A 11:06 321
I want to extract one line for each unique combination of Cat1 and Cat2 and I need the line with the oldest date. In the above I want ID = 1, 2, 4, and 5.
Thanks
Have a look at row_number() on MSDN.
SELECT *
FROM (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY col1, col2 ORDER BY date_time, id) rn
FROM mytable
) q
WHERE rn = 1
(run the code on SQL Fiddle)
Quassnoi's answer is fine, but I'm a bit uncomfortable with how it handles dups. It seems to return based on insertion order, but I'm not sure if even that can be guaranteed? (see these two fiddles for an example where the result changes based on insertion order: dup at the end, dup at the beginning)
Plus, I kinda like staying with old-school SQL when I can, so I would do it this way (see this fiddle for how it handles dups):
select *
from my_table t1
left join my_table t2
on t1.cat1 = t2.cat1
and t1.cat2 = t2.cat2
and t1.datetime > t2.datetime
where t2.datetime is null

Resources