sql query group - sql

SQL query question
I have a query like
select proposal_id, service_id,account_type
from table1
The result is like this:
proposal_id service_id account_type
1 1001 INTERVAL
1 1002 INTERVAL
2 1003 NON INTERVAL
2 1004 NON INTERVAL
3 1005 NON INTERVAL
3 1006 INTERVAL
I want to write a query: for each proposal_id, if all the service have INTERVAL then get 'INTERVAL', if all NON-INTERVAL get 'NON-INTERVAL', if both, get 'Both'
For the example above, it should return
proposal_id account_type
1 INTERVAL
2 NON-INTERVAL
3 BOTH

Data:
declare #table table (id int, sid int, acc nvarchar(20))
insert #table VALUES (1,1001,'INTERVAL'),(1,1002,'INTERVAL'),(2,1003,'NON INTERVAL'),(2,1004,'NON INTERVAL'),
(3,1005,'NON INTERVAL'),(3,1006,'INTERVAL')
Query:
select x.Id
, CASE counter
WHEN 1 THEN x.Account_Type
ELSE 'BOTH'
END AS Account_Type
from (
select Id, Count(DISTINCT(acc)) AS counter, MAX(acc) As Account_Type
from #table
GROUP BY Id
) x
Results
Id Account_Type
----------- --------------------
1 INTERVAL
2 NON INTERVAL
3 BOTH

SELECT
b.proposal_id
,CASE
WHEN s1.proposal_id IS NOT NULL AND s2.proposal_id IS NOT NULL THEN 'BOTH'
WHEN s1.proposal_id IS NOT NULL THEN 'INTERVAL'
WHEN s2.proposal_id IS NOT NULL THEN 'NON-INTERVAL'
ELSE 'UNKNOWN'
END [account_type]
FROM table1 b
LEFT JOIN(
SELECT proposal_id,account_type FROM table1 WHERE account_type = 'INTERVAL'
) s1
ON b.proposal_id = s1.proposal_id
LEFT JOIN (
SELECT proposal_id,account_type FROM table1 WHERE account_type = 'NON-INTERVAL'
)s2
ON b.proposal_id = s2.proposal_id

You could use count distinct to determinate if it is both then use CASE to determinate what to display
SELECT DISTINCT proposal.proposal_id,
CASE cou
WHEN 1 THEN type ELSE 'Both' END as TYPE
FROM proposal
INNER JOIN (SELECT proposal_id, count(distinct type) cou
FROM proposal GROUP BY proposal_id) inn
ON proposal.id = inn.id

select proposal_id,
case when count(distinct account_type) > 1 then 'BOTH'
else max(account_type)
end
from table1
group by proposal_id
You have the fiddler here.

Related

Return row for GROUP BY CASE WHEN IS NULL THEN (...) ELSE (...) even if record does not exist

Let's consider the following scenario.
CREATE TABLE Replicant (Name NVARCHAR(10),Gen INT);
INSERT INTO Replicant VALUES ('tymtam', 2), ('Roy', 6);
SELECT
CASE WHEN Gen < 10 THEN '<10' ELSE '>=10' END as 'Gen',
count(*) as 'Count'
FROM Replicant
GROUP BY CASE WHEN Gen < 10 THEN '<10' ELSE '>=10' END;
The result is a single row:
Gen Count
<10 2
Can I up-sophisticate the query so that I get a zero for the ELSE case?
Gen Count
<10 2
>=10 0
Update 2
My discriminator is 'is null'
SELECT CASE WHEN Gen IS NOT NULL THEN 'Known' ELSE 'Unknown' END as 'Gen', count(*) as 'Count' FROM Replicant
GROUP BY CASE WHEN Gen IS NOT NULL THEN 'Known' ELSE 'Unknown' END;
The result is
Gen Count
Known 2
and I yearn for
Gen Count
Known 2
Unknown 0
Update 1
My context is that I have pairs of queries (metrics) for different generations of replicants:
INSERT INTO [dbo].[Metrics] (...) SELECT
'Metric X for >=10' as 'Name',
COUNT(*) AS 'Count',
(80_char_expression) AS 'Sum',
(80_char_expression) AS 'Min',
(80_char_expression) AS 'Max',
0 AS 'StandardDeviation'
FROM Replicant
WHERE TimestampUtc > DATEADD(WEEK, -1, Current_Timestamp)
AND Gen >= 10
INSERT INTO [dbo].[Metrics] (...) SELECT
'Metric X for <10' as 'Name',
--7 lines repeated from the 1st query
AND Gen < 10
I would prefer to have a single select to insert two rows, even if there are no records.
You can try to use UNOIN ALL make a comparison table for your score then do outer join
Query 1:
SELECT t1.word,
COUNT(Name) 'Count'
FROM
(
SELECT '<10' word,9 maxval,0 minval
UNION ALL
SELECT '>=10' word,2147483646 maxval,10 minval
) t1 LEFT JOIN Replicant on Gen BETWEEN t1.minval AND t1.maxval
GROUP BY t1.word
Results:
| word | Count |
|------|-------|
| <10 | 2 |
| >=10 | 0 |
You can use left join:
SELECT v.Gen, COUNT(r.gen) as cnt
FROM (VALUES (NULL, 10, '<10'),
(10, NULL, '>=10')
) v(lo, hi, gen) LEFT JOIN
Replicant r
ON (r.gen >= v.lo OR v.lo IS NULL) AND
(r.gen < v.hi OR v.hi IS NULL)
GROUP BY v.gen;
You can also use conditional aggregation and unpivoting:
select v.*
from (select sum(case when r.gen < 10 then 1 else 0 end) as gen_1,
sum(case when r.gen >= 10 then 1 else 0 end) as gen_2
from replicant r
) r cross apply
(values (gen_1, '<10'), (gen_2, '>=10')
) v(cnt, gen);

SQL Select item which has same value in all rows

For example, if the below is the table
SupId ItemId Status
1 1 Available
1 2 OOS
2 3 Available
3 4 OOS
3 5 OOS
4 6 OOS
5 7 NULL
I am looking to fetch distinct suppliers whose all items are OOS or NULL.
One solution is to get all the suppliers who has atleast one active item (active suppliers) and then add a clause NOT IN active suppliers to pick non active supplier.
Is there any better way to achieve the same?
One option, using aggregation:
SELECT SupId
FROM yourTable
GROUP BY SupId
HAVING
SUM(CASE WHEN Status = 'OOS' OR Status IS NULL THEN 1 ELSE 0 END) = COUNT(*) AND
(MAX(Status) = 'OOS' OR COUNT(Status) = 0);
This assumes you want suppliers who have only all NULL or all OOS status. If you just want to limit to both these two status values, then use this:
SELECT SupId
FROM yourTable
GROUP BY SupId
HAVING SUM(CASE WHEN Status <> 'OOS' AND Status IS NOT NULL THEN 1 ELSE 0 END) = 0;
Try:
SELECT DISTINCT SupId FROM my_table t
WHERE NOT EXISTS(SELECT 1 FROM my_table
WHERE SupId = t.SupId
AND [Status] IS NOT NULL
AND [Status] <> 'OOS')
SELECT DISTINCT SupId
FROM Table
WHERE SupId <> (
SELECT DISTINCT SupId
FROM Table
WHERE Status NOT IN ('OOS',NULL)
)
I would use NOT EXISTS :
SELECT t.*
FROM table t
WHERE NOT EXISTS (SELECT 1 FROM table t1 WHERE t1.supid = t.supid and t1.status <> 'OOS');
I would use group by and having:
select suppid
from t
group by suppid
having (min(Status) = 'OOS' and max(Status) = 'OOS') or
min(Status) is null;

Get previous distinct number

I've got the following table:
id date status
1 2017-04-20 good
1 2017-04-19 bad
1 2017-04-18 bad
2 2017-04-20 ok
2 2017-04-19 ok
2 2017-04-17 ok
2 2017-04-16 bad
What I need is to get the previous distinct value in status column per id.
Assuming that today is 2017-04-20, the result would look like this:
id previous_status
1 bad
2 bad
Afterwards, I wanna use this information in the case statement:
Case when status = 'good' and previous_status = 'bad', ....
You could you this for your first requirement:
SELECT id, status AS previous_status
FROM table_name
WHERE (id, date_col) IN (
SELECT id, MAX(date_col)
FROM table_name
WHERE DATE(date_col) < "2017-04-20"
);
And this is for second requirement using CASE statement
SELECT t.id, t.status, t1.previous_status,
CASE
WHEN t.status = 'good' AND t1.previous_status = 'bad' THEN 'some_value'
ELSE 'other_value'
END AS case_col
FROM table_name t
INNER JOIN (
SELECT id, status AS previous_status
FROM table_name
WHERE (id, date_col) IN (
SELECT id, MAX(date_col)
FROM table_name
WHERE DATE(date_col) < "2017-04-20"
)
) t1
ON t.id = t1.id
WHERE DATE(t.date_col) = "2017-04-20";
This refer how_to_compare_date_google_big_query

SQL Server case when or enum

I have a table something like:
stuff type price
first_stuff 1 43
second_stuff 2 46
third_stuff 3 24
fourth_stuff 2 12
fifth_stuff NULL 90
And for every type of stuff is assigned a description which is not stored in DB
1 = Bad
2 = Good
3 = Excellent
NULL = Not_Assigned
All I want is to return a table which count each type separately, something like:
Description Count
Bad 1
Good 2
Excellent 1
Not_Assigned 1
DECLARE #t TABLE ([type] INT)
INSERT INTO #t ([type])
VALUES (1),(2),(3),(2),(NULL)
SELECT
[Description] =
CASE t.[type]
WHEN 1 THEN 'Bad'
WHEN 2 THEN 'Good'
WHEN 3 THEN 'Excellent'
ELSE 'Not_Assigned'
END, t.[Count]
FROM (
SELECT [type], [Count] = COUNT(*)
FROM #t
GROUP BY [type]
) t
ORDER BY ISNULL(t.[type], 999)
output -
Description Count
------------ -----------
Bad 1
Good 2
Excellent 1
Not_Assigned 1
;WITH CTE_TYPE
AS (SELECT DESCRIPTION,
VALUE
FROM (VALUES ('BAD',
1),
('GOOD',
2),
('EXCELLENT',
3))V( DESCRIPTION, VALUE )),
CTE_COUNT
AS (SELECT C.DESCRIPTION,
Count(T.TYPE) TYPE_COUNT
FROM YOUR_TABLE T
JOIN CTE_TYPE C
ON T.TYPE = C.VALUE
GROUP BY TYPE,
DESCRIPTION
UNION ALL
SELECT 'NOT_ASSIGNED' AS DESCRIPTION,
Count(*) TYPE_COUNT
FROM YOUR_TABLE
WHERE TYPE IS NULL)
SELECT *
FROM CTE_COUNT
Hope, this helps.
SELECT ISNULL(D.descr, 'Not_Assigned'),
T2.qty
FROM
(SELECT T.type,
COUNT(*) as qty
FROM Table AS T
GROUP BY type) AS T2
LEFT JOIN (SELECT 1 as type, 'Bad' AS descr
UNION ALL
SELECT 2, 'Good'
UNION ALL
SELECT 3, 'Excellent') AS D ON D.type = T2.type
If you are using Sql server 2012+ use this
SELECT
[Description] = coalesce(choose (t.[type],'Bad','Good' ,'Excellent'), 'Not_Assigned'),
t.[Count]
FROM (
SELECT [type], [Count] = COUNT(*)
FROM yourtable
GROUP BY [type]
) t

Joining two tables and while pivoting sql server 2008

I have two tables like so
Table1
SegmentNo PassingPotencies
1 8
2 10
Table2
BatchAvg Total TotalSegments TotalPassed
106.22 20 2 18
I want to join two tables with a simple login. If the Passingpotencies in table 1 is not equal to 10 then the segment is failed and vice versa. The final result should look something like this
TableResult
BatchAvg Total TotalSegments TotalPassed Segment1 Segment2
106.22 20 2 18 Fail Pass
Any help is greatly appreciated. Thanks.
With your current design, this is what you can achieve (something closest).
See a demo here http://sqlfiddle.com/#!3/e86f5/5
select distinct BATCHAVG,
TOTAL,
TOTALSEGMENTS,
TOTALPASSED,
SegmentNo,
case when PASSINGPOTENCIES <> 10 then 'Failed'
else 'Passed' end as SegmentStatus
from
(
select * from table2,table1
) X
Above query will results in
This will work in your actual scenario. See a Demo here: SQLFiddle
First, join both tables:
SELECT
T2.BATCHAVG
, T2.TOTAL
, T2.TOTALSEGMENTS
, T2.TOTALPASSED
, T1.SEGMENTNO
, (CASE WHEN T1.PASSINGPOTENCIES >= 10 THEN 'PASSED' ELSE 'FAILED' END) AS SEGMENT
INTO TABLE3
FROM TABLE1 T1
CROSS JOIN TABLE2 T2
Then, select this table like this. It's some kind of PIVOT:
SELECT
T.BATCHAVG
, T.TOTAL
, T.TOTALSEGMENTS
, T.TOTALPASSED
, MAX(T.SEGMENT1) AS SEGMENT1
, MAX(T.SEGMENT2) AS SEGMENT2
FROM (
SELECT
T1.BATCHAVG
, T1.TOTAL
, T1.TOTALSEGMENTS
, T1.TOTALPASSED
, (CASE WHEN T1.SEGMENTNO = '1' THEN T1.SEGMENT END) AS SEGMENT1
, (CASE WHEN T1.SEGMENTNO = '2' THEN T1.SEGMENT END) AS SEGMENT2
FROM TABLE3 T1
) T
GROUP BY
T.BATCHAVG
, T.TOTAL
, T.TOTALSEGMENTS
, T.TOTALPASSED