Sql Query condition based on column can holded bit - sql

My question is:
select
ProdID, Qualified
from
VarAnn_Data.dbo.tblOwnership
procedures output:
ProdID(num) Qual (Bit)
26 0
26 0
26 1
26 0
26 1
26 0
26 0
27 0
28 0
28 0
28 0
28 0
28 0
28 1
28 1
29 0
29 0
29 1
29 0
29 1
29 0
29 0
Now there are more than one and zero for single prodid. I got result with distinct
CASE 1:
ProdId(num) Quak(bit)
26 0
26 1
result: A
ProdId(num) Quak(bit)
26 0
Result : B
ProdId(num) Quak(bit)
26 1
result : C

Is this what you are looking for?
select ProdID,
min(Qualified),
case when min(Qualified) <> max(Qualified)
then 'YES'
else 'NO'
end as Has_different_results
from VarAnn_Data.dbo.tblOwnership
group by ProdID

Related

SQL show UserCode from the highest value in column

I am making a select from a table that has alums, but those alums can have a "level" being 01, 02, 03, 04, 05 or 99 and if I select a distinct(code) there will be 2 or 3 files of the same "code" with different "level". I want to be able to ONLY get the "code" with the higher "level" anytime.
Example of my code:
select
Level,
count(Code) as Atendee,
count(CASE WHEN CodPlace = 01 THEN (Atendee) ELSE null END ) as AtendeeCho,
count(CASE WHEN CodPlace = 02 THEN (Atendee) ELSE null END ) as AtendeeSB,
count(CASE WHEN CodPlace = 14 THEN (Atendee) ELSE null END ) as AtendeeIca
from #TempoPar
group by Level
order by Level
And here is the result:
Level Atendee AtendeeCho AtendeeSB AtendeeIca
1 3 2 0 1
2 0 0 0 0
3 2 2 0 0
99 1 1 0 0
Problem is that the one who is in AtendeeCho from level 1, 2 and 99 is the same person and he should only be on 99 or 3 if he wasn't on 99.
Thanks in advance!
Data example:
Code ( user) Level Career CodPlace
12345 1 9 01
12345 3 15 01
12346 1 10 14
12347 1 10 01
12345 3 15 01
12347 99 15 01
Now, this is what I require:
From the data sample:
Code ( user) Level Career CodPlace
12345 3 15 01
12346 1 10 14
12347 99 15 01
In the final output with the corrected data it should be:
Level Atendee AtendeeCho AtendeeSB AtendeeIca
1 1 0 0 1
2 0 0 0 0
3 1 1 0 0
99 1 1 0 0

Postgres query for annual sales report by rep. grouped by month

I would like to create an annual sales report table by sales rep, showing all the twelve month. The data I have is more or less like in this example:
id | rep | date | price
----------------------------
1 1 2017-01-01 20
2 1 2017-01-20 44
3 2 2017-02-18 13
4 2 2017-03-08 12
5 2 2017-04-01 88
6 2 2017-09-05 67
7 3 2017-01-31 10
8 3 2017-06-01 74
The result I need would be like this:
Rep Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
----------------------------------------------------
1 64 0 0 0 0 0 0 0 0 0 0 0
2 0 13 12 88 0 0 0 0 67 0 0 0
3 10 0 0 0 0 74 0 0 0 0 0 0
What would be the most efficient way to write this query?
One way:
select rep,
sum(case when extract('month' from date) = 1 then price else 0 end ) as Jan,
sum(case when extract('month' from date) = 2 then price else 0 end ) as Feb
-- another months here
from t
group by rep
One way is to use windowed functions with FILTER:
SELECT DISTINCT
"rep",
COALESCE(SUM(price) FILTER (WHERE extract('month' from "date") = 1) OVER(PARTITION BY "rep"),0) AS Jan,
COALESCE(SUM(price) FILTER (WHERE extract('month' from "date") = 2) OVER(PARTITION BY "rep"),0) AS Feb
--....
FROM ta;
Rextester Demo
Warning!
You probably want to partition by YEAR too to avoid summing JAN 2017 and JAN 2018.

i want to count how many 37 for every ID by using sql

ID jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
13441 37 0 0 0 0 67 0 0 0 0 0 0
13555 37 0 97 0 0 0 0 0 0 0 0 0
15103 67 0 97 0 0 0 0 0 0 0 0 0
16019 37 0 97 0 0 0 0 0 0 37 67 97
12951 67 0 97 0 0 0 0 0 0 0 0 0
16351 37 0 37 67 0 0 0 0 0 0 37 67
13244 37 0 37 67 0 0 0 0 0 0 0 0
Hmmm . . . This requires unpivoting the data, filtering, and re-aggregating. This turns out to be pretty easy using apply:
select t.*, c.cnt37
from t outer apply
(select count(*) as cnt37
from (values (jan), (feb), (mar), (apr), (may), (jun), (jul), (aug), (sep), (oct), (nov), (dec)
) v(val)
where val = 37
) c;
You can use UNPIVOT:
SELECT ID, COUNT(*) AS cnt
FROM (
SELECT *
FROM mytable) AS src
UNPIVOT (col FOR mon IN ([Jan], [Feb], [Mar], [Apr], [May], [Jun],
[Jul], [Aug], [Sep], [Oct], [Nov], [Dec])) AS unpvt
WHERE col = 37
GROUP BY ID

How to get recent data and previous data in SQL

If we have the following data, I want to get the data using the "SomeID" column or The "ApplicationID". So the data returned is basically the Amount in separate columns based on the "SomeID"
ID SomeID ApplicationID Amount
19 8 19 45.18
20 8 20 45.18
21 8 21 225.91
22 8 22 203.32
72 10 19 45.18
73 10 20 45.18
74 10 21 225.91
75 10 22 203.32
I want to return
Last Month repayment This Month repaymnrt Variance
45.18 45.18 0
45.18 45.18 0
225.91 225.91 0
203.32 203.32 0
I think you can do what you want using pivot or conditional aggregatoin:
select applicationId,
sum(case when someId = 8 then Amount else 0 end) as LastMonth,
sum(case when someId = 10 then Amount else 0 end) as ThisMonth,
sum(case when someId = 8 then -Amount
when someId = 10 then Amount
else 0
end) as Diff
from t
group by applicationId;

How to increment a variable inside CASE WHEN clause?

I'm trying to make a new column with a variable that increments given a condition clause.
This is my query:
SELECT
[timestamp],
[load],
[timestamp] % 60 AS module,
group_by_column = CASE WHEN ([timestamp] % 60)= 0 then 1 else 0 end
FROM
table_01
And this is my return:
timestamp load module group_by_column
1432592618 24 38 0
1432592619 32 39 0
1432592620 21 40 0
1432592621 31 41 0
1432592622 28 42 0
1432592640 22 0 1
1432592641 31 1 0
1432592642 39 2 0
1432592643 33 3 0
1432592644 36 4 0
1432592649 32 9 0
1432592698 21 58 0
1432592700 25 0 1
1432592701 20 1 0
1432592702 27 2 0
1432592703 31 3 0
I need a table like this:
timestamp load module group_by_column
1432592618 24 38 0
1432592619 32 39 0
1432592620 21 40 0
1432592621 31 41 0
1432592622 28 42 0
1432592640 22 0 1
1432592641 31 1 1
1432592642 39 2 1
1432592643 33 3 1
1432592644 36 4 1
1432592649 32 9 1
1432592698 21 58 1
1432592700 25 0 2
1432592701 20 1 2
1432592702 27 2 2
1432592703 31 3 2
For Example, every time I found the condition [timestamp] % 60 = 0 is satisfied, I increment in the forth column.
I don't know if this is the best way to solve this problem, I just need an output as described below.
Thanks
In SQL Server 2012+, you would do a cumulative sum:
SELECT [timestamp], [load], [timestamp] % 60 AS module,
SUM(CASE WHEN [timestamp] % 60 = 0 THEN 1 ELSE 0 END) OVER
(ORDER BY timestamp) as group_by_column
FROM table_01;
In SQL Server 2008, I think you can get the same effect using dense_rank():
SELECT [timestamp], [load], [timestamp] % 60 AS module,
DENSE_RANK() OVER (ORDER BY [timestamp] / 60)
FROM table_01;
In case the counting integer should reflect the minutes that have passed since the first event, then the following will work:
SELECT [timestamp], [load], [timestamp] % 60 AS module,
([timestamp] / 60) - (SELECT min([timestamp]) / 60 FROM table_01) grp
FROM table_01;