Increment columns value based on duplicity record - sql

I have a question on whether if I can avoid using a cursor.
I have a table with thousands of record like this:
Date | Name | StateData |
-----------+-------+-----------+
22-10-2019 | Tom | OPENED |
22-10-2019 | David | NULL |
22-10-2019 | Tom | NULL |
22-10-2019 | Brand | CLOSED |
22-10-2019 | Tom | NULL |
23-10-2019 | Brand | NULL |
22-10-2019 | Brand | OPENED |
22-10-2019 | Tom | OPENED |
22-10-2019 | Brand | OPENED |
22-10-2019 | Tom | CLOSED |
22-10-2019 | Brand | CLOSED |
What I'd like to achieve is a result, where I can see each name on just 1 row with counting how many times they had which State "StateData".
Date | Name | OPENED | CLOSED | UNUSED |
-----------+------+--------+--------+--------+
22-10-2019 | Tom | 2 | 1 | 2 |
22-10-2019 | David| 0 | 0 | 1 |
22-10-2019 | Brand| 2 | 2 | 1 |
I've tried a select like this
SELECT DISTINCT d.Name, d.[opened], d.[closed], d.[unused], StateData
FROM [dbo].[StateData] d
INNER JOIN (
SELECT DISTINCT Name, [opened], [closed], [unused]
FROM [dbo].[StateData]
GROUP BY Name, [opened], [closed], [unused]
) dp
ON dp.Name = d.Name
;
I know it can be done with creating stored procedure using CURSOR, but I'm not that experienced with cursor.

Use conditional aggregation:
select
Date,
Name,
SUM(CASE WHEN StateDAte = 'Opened' THEN 1 ELSE 0 END) Opened,
SUM(CASE WHEN StateDAte = 'Closed' THEN 1 ELSE 0 END) Closed,
SUM(CASE WHEN StateDAte IS NULL THEN 1 ELSE 0 END) Unused
from mytable
group by Date, Name

You can use sql pivot as follows
;with cte as (select date,name,isnull(statedate,'unused') statedate from mytable)
select p.* from cte
pivot
(count(statedate) for statedate in ([Opened],[Closed], [unused] )) as p

GMB and Kemal,
thanks,
I have tried this
SELECT [Date],
[Name],
COUNT(CASE StateData WHEN 'OPENED' THEN 1 END) AS OPENED,
COUNT(CASE StateData WHEN 'CLOSED' THEN 1 END) AS CLOSED,
COUNT(CASE WHEN StateData IS NULL THEN 1 END) AS UNUSED
FROM YourTable
GROUP BY [Date],
[Name];
but the result is not correct
Name | OPENED | CLOSED | UNUSED |
Tom | 2 | 0 | 57412 |
Brad | 2 | 0 | 57412 |
David | 2 | 0 | 57412 |
the correct result should be this
Date | Name | OPENED | CLOSED | UNUSED |
-----------+------+--------+--------+--------+
22-10-2019 | Tom | 2 | 1 | 2 |
22-10-2019 | David| 0 | 0 | 1 |
22-10-2019 | Brand| 2 | 2 | 1 |
but it seems like its just copy pasting the results.

Related

How to move all non-null values to the top of my column?

I have the following data in my table:
| Id | lIST_1 |
----------------------
| 1 | NULL |
| 2 | JASON |
| 3 | NULL |
| 4 | BANDORAN |
| 5 | NULL |
| 6 | NULL |
| 7 | SMITH |
| 8 | NULL |
How can I write a query to get the output below?
| Id | lIST_1
-----------------------
| 1 | JASON |
| 2 | BANDORAN |
| 3 | SMITH |
| 4 | NULL |
| 5 | NULL |
| 6 | NULL |
| 7 | NULL |
| 8 | NULL |
You can use order by:
select row_number() over (order by (select null)) as id, t.list_1
from t
order by (case when list_1 is not null then 1 else 2 end)
It is unclear why you would want id to change values, but you can use row_number() for that.
EDIT:
If you want to change the id, then you can do:
with toupdate as (
select row_number() over (order by (case when list_id is not null then 1 else 2 end), id
) as new_id,
t.*
from t
)
update toupdate
set id = new_id
where id <> new_id; -- no need to update if the value remains the same

How to retrieve the column value as row value? [duplicate]

This question already has answers here:
TSQL Pivot without aggregate function
(9 answers)
Closed 4 years ago.
How can we retrieve information to be a row information from column values?
For example, we have a table
|-----------|-------------|--------------|
| ID | Field | FieldVlaue |
|-----------|-------------|--------------|
| 1 | Name | Jack |
|-----------|-------------|--------------|
| 1 | Country | Australia |
|-----------|-------------|--------------|
| 1 | PostCode| 0277 |
|-----------|-------------|--------------|
| 2 | Name | John |
|-----------|-------------|--------------|
| 2 | address | Wyard |
|-----------|-------------|--------------|
| 2 | ContactNum | 1234567 |
|-----------|-------------|--------------|
Based on the above table, how can we get a table like below
|-----------|-------------|--------------|-------------|---------|-----------|
| ID | Name | Country | PostCode |Address | ContactNum|
|-----------|-------------|--------------|-------------|---------|-----------|
| 1 | Jack | Australia | 0277 |Null | Null |
|-----------|-------------|--------------|-------------|---------|-----------|
| 2 | John | Null | Null | Wyard |1234567
|-----------|-------------|--------------|-------------|---------|-----------|
use condition aggregation
select id,
max(case when Field='Name' then FieldVlaue end) as Name,
max(case when Field='Country' then FieldVlaue end) as Country,
max(case when Field='PostCode' then FieldVlaue end) as PostCode,
max(case when Field='Address' then FieldVlaue end ) as Address,
max(case when Field='ContactNum' then FieldVlaue end) as ContactNum
from t
group by id

Oracle SQL count and group by multiple fields

I am able to get the data merging two tables to get the following table.
+------------+------+--------+--------+------------+------------+
| Group Name | Type | Manger | Status | ControlOne | ControlTwo |
+------------+------+--------+--------+------------+------------+
| Group A | 1 | 1 | finish | 2 | 2 |
| Group A | 2 | 1 | open | 0 | 2 |
| Group A | 1 | 1 | finish | 0 | 0 |
| Group A | 1 | 2 | finish | 2 | 0 |
| Group B | 1 | 1 | open | 2 | 0 |
| Group B | 1 | 2 | open | 2 | 2 |
| Group B | 2 | 2 | open | 0 | 2 |
| Group B | 2 | 1 | finish | 0 | 0 |
| Group B | 1 | 1 | open | 2 | 0 |
+------------+------+--------+--------+------------+------------+
Now I need to get the total counts based on GroupName/ Type and Manager to have the output for each group in the following format:
+------------+------+-------------------------------------------------+--------------------------------------------+------------------------------+----------------------------+
| Group Name | Type | Manager1Finish | Manager1Open | Manager2Finish | Manager2Open |
+------------+------+-------------------------------------------------+--------------------------------------------+------------------------------+----------------------------+
| Group A | 1 | 2(count of finish by Group A, manager1, type 1) | 0(count of open Manager1, Type 1, Group A) | 1(count of finish Manager 2) | 0(count of open manager 2) |
| Group A | 2 | 0 | 1 | 0 | 0 |
+------------+------+-------------------------------------------------+--------------------------------------------+------------------------------+----------------------------+
Could you please help to how to achieve this?
Try with CASE WHEN:
SELECT GroupName,
TYPE,
COUNT (CASE
WHEN Manager = 1
AND status = 'Finish'
THEN
1
END)
AS Manager1Finish,
COUNT (CASE
WHEN Manager = 1
AND status = 'Open'
THEN
1
END)
AS Manager1Open,
COUNT (CASE
WHEN Manager = 2
AND status = 'Finish'
THEN
1
END)
AS Manager2Finish,
COUNT (CASE
WHEN Manager = 2
AND status = 'Open'
THEN
2
END)
AS Manager2Open
FROM tablename
GROUP BY GroupName, TYPE
select [group], [type],
sum(case when manager=1 and status='finish' then 1 else 0 end) as m1finish,
sum(case when manager=1 and status='open' then 1 else 0 end) as m1open,
sum(...etc...)
from mytable
group by [group],[type]

Complicated SELECT statement in Oracle DB

Can you please help me with one complicated select statement?
I have a table like this:
+----+-----------+-----------+-----------------+
| ID | User_name | Situation | Date_time |
+----+-----------+-----------+-----------------+
| 1 | Alex | 1 | 14.3.18 11:30 |
| 4 | Alex | 2 | 14.3.18 11:35 |
| 6 | Alex | 3 | 14.3.18 12:30 |
| 7 | Johnny | 1 | 15.3.18 10:01 |
| 9 | Johnny | 2 | 15.3.18 10:05 |
| 12 | Johnny | 3 | 15.3.18 10:20 |
| 14 | Alex | 1 | 20.3.18 20:00 |
| 15 | Alex | 2 | 20.3.18 20:25 |
| 17 | Alex | 3 | 20.3.18 21:25 |
+----+-----------+-----------+-----------------+
And I need a select statement, which will give me the following result:
User_name, Date_time_1 (Date_time of situation 1), Date_time_3 (Date_time of situation 3).
*In this case the result will have just 3 rows (2 for Alex and 1 for Johnny). Each row will contain 3 columns as described above.
And sorry for the formatting - I posted that from a mobile. I will add the result table when I will get to PC.*
That's how the output should looks like:
+----+-----------+-------------+-----------------+
| ID | User_name |Date_time_1 | Date_time_3 |
+----+-----------+-------------+-----------------+
| 1 | Alex |14.3.18 11:30| 14.3.18 12:30 |
| 2 | Johnny |15.3.18 10:01| 15.3.18 10:20 |
| 3 | Alex |20.3.18 20:00| 20.3.18 21:25 |
+----+-----------+-------------+-----------------+
You could use conditional aggregation:
SELECT User_name,
MAX(CASE WHEN Situation = 1 THEN Date_time END) AS date_time_1,
MAX(CASE WHEN Situation = 3 THEN Date_time END) AS date_time_3
FROM tab
GROUP BY User_name;
EDIT
In this case the result will have just 3 rows (2 for Alex and 1 for Johnny)
WITH cte AS (
SELECT t.*, SUM(CASE WHEN Situation=1 THEN 1 ELSE 0 END)
OVER(PARTITION BY User_name ORDER BY id) AS s
FROM tab t
)
SELECT User_name,
MAX(CASE WHEN Situation = 1 THEN Date_time END) AS date_time_1,
MAX(CASE WHEN Situation = 3 THEN Date_time END) AS date_time_3
FROM cte
GROUP BY s, User_name;
DBFiddle Demo

How to select multiple count(*) values then group by a specific column

I've used SQL for a while but wouldn't say I'm at an advanced level. I've tinkered with trying to figure this out myself to no avail.
I have two tables - Transaction and TransactionType:
| **TransactionID** | **Name** | **TransactionTypeID** |
| 1 | Tom | 1 |
| 2 | Jim | 1 |
| 3 | Mo | 2 |
| 4 | Tom | 3 |
| 5 | Sarah | 4 |
| 6 | Tom | 1 |
| 7 | Sarah | 1 |
| **TransactionTypeID** | **TransactionType** |
| 1 | A |
| 2 | B |
| 3 | C |
| 4 | D |
The Transaction.TransactionTypeID is a Forein Key linked TransactionType.TransactionTypeID field.
Here's what I'd like to achieve:
I'd like a query (this will be a stored procedure) that returns three columns:
Name - the value of the Transaction.Name column.
NumberOfTypeATransactions - the count of the number of all transactions of type 'A' for that person.
NumberOfNonTypeATransactions - the count of the number of all transactions that are NOT of type A for that person, i.e. all other transaction types.
So, using the above data as an example, the result set would be:
| **Name** | **NumberOfTypeATransactions** | **NumberOfNonTypeATransactions** |
| Tom | 2 | 1 |
| Jim | 1 | 0 |
| Mo | 0 | 1 |
| Sarah | 1 | 1 |
I might also need to return the results based on a date period (which will be based on a 'transaction date' column in the Transaction table but I haven't finalized this requirement yet.
Any help in how I can achieve this would be much appreciated. Apologies of the layout of the tables is a bit odd - haven't worked out how to format them properly yet.
This is just conditional aggregation with a join:
select t.name,
sum(case when tt.TransactionType = 'A' then 1 else 0 end) as num_As,
sum(case when tt.TransactionType <> 'A' then 1 else 0 end) as num_notAs
from transaction t join
transactiontype tt
on tt.TransactionTypeID = t.TransactionTypeID
group by t.name;