How Can I count and show how many Opportunity have Stage 3 but dont have Stage 2?
+-------+-------+
| OppID | Stage |
+-------+-------+
| ABC | 1 |
| ABC | 2 |
| ABC | 3 |
| ABC | 4 |
| CDF | 3 |
| CDF | 4 |
| EFG | 1 |
| EFG | 2 |
| EFG | 3 |
| HIJ | 2 |
| HIJ | 3 |
| LMI | 1 |
| LMI | 2 |
| LMI | 4 |
+-------+-------+
The count result is 1
+-------+-------+
| OppID | Stage |
+-------+-------+
| CDF | 3 |
| CDF | 4 |
+-------+-------+
Got it, you could use NOT EXISTS and COUNT DISTINCT in following:
SELECT COUNT(DISTINCT OppID)
FROM tbl AS t1
WHERE NOT EXISTS (SELECT 1 FROM tbl AS t2 WHERE t1.OppID = t2.OppID and t2.Stage = 2) and t1.Stage = 3
Related
I have two tables 'Product' and 'product_Desc'
+-----------+-------------+
| ProductID | ProductName |
+-----------+-------------+
| 1 | A |
| 2 | B |
+-----------+-------------+
+----+-----------+-------------+-----------+
| Id | ProductID | ProductDec | SortOrder |
+----+-----------+-------------+-----------+
| 1 | 1 | Aero-pink | |
| 2 | 1 | Aero-white | |
| 3 | 1 | Aero-green | |
| 4 | 1 | Aero-Orange | |
| 5 | 2 | Baloon-1 | |
| 6 | 2 | Baloon-2 | |
| 7 | 2 | Baloon-3 | |
+----+-----------+-------------+-----------+
Now, what is the Sql code that can update 'sortOrder' column sequentially for each group of ProductID as shown below:
+----+-----------+-------------+-----------+
| Id | ProductID | ProductDec | SortOrder |
+----+-----------+-------------+-----------+
| 1 | 1 | Aero-pink | 1 |
| 2 | 1 | Aero-white | 2 |
| 3 | 1 | Aero-green | 3 |
| 4 | 1 | Aero-Orange | 4 |
| 5 | 2 | Baloon-1 | 1 |
| 6 | 2 | Baloon-2 | 2 |
| 7 | 2 | Baloon-3 | 3 |
+----+-----------+-------------+-----------+
Please note that these are sample tables, actual tables have thousands of records.
Would appreciate your help on this. Thank you
with cte
as
(
select SortOrder, row_number() over(partition by ProductID order by Id) as newPerProductOrder
from product_Desc
)
update cte
set SortOrder = newPerProductOrder
where (SortOrder <> newPerProductOrder or SortOrder is null)
I have 2 tables, tableStock and tableParts:
tableStock
+----+----------+-------------+
| ID | Num_Part | Description |
+----+----------+-------------+
| 1 | sr37 | plate |
+----+----------+-------------+
| 2 | sr56 | punch |
+----+----------+-------------+
| 3 | sl30 | crimper |
+----+----------+-------------+
| 4 | mp11 | holder |
+----+----------+-------------+
tableParts
+----+----------+-------+
| ID | Location | Stock |
+----+----------+-------+
| 1 | A | 2 |
+----+----------+-------+
| 3 | B | 5 |
+----+----------+-------+
| 5 | C | 2 |
+----+----------+-------+
| 7 | A | 1 |
+----+----------+-------+
And I just want to do this:
+----+----------+-------------+----------+-------+
| ID | Num_Part | Description | Location | Stock |
+----+----------+-------------+----------+-------+
| 1 | sr37 | plate | A | 2 |
+----+----------+-------------+----------+-------+
| 2 | sr56 | punch | NULL | NULL |
+----+----------+-------------+----------+-------+
| 3 | sl30 | crimper | B | 5 |
+----+----------+-------------+----------+-------+
| 4 | mp11 | holder | NULL | NULL |
+----+----------+-------------+----------+-------+
List ALL the rows of the first table and if the second table has the info, in this case 'location' and 'stock', add to the column, if not, just null.
I have been using inner and left join but some rows of the first table disappear because the lack of data in the second one:
select tableStock.ID, tableStock.Num_Part, tableStock.Description, tableParts.Location, tableParts.Stock from tableStock inner join tableParts on tableStock.ID = tableParts.ID;
What can I do?
You can use left join. Here is the demo.
select
s.ID,
Num_Part,
Description,
Location,
Stock
from Stock s
left join Parts p
on s.ID = p.ID
order by
s.ID
output:
| id | num_part | description | location | stock |
| --- | -------- | ----------- | -------- | ----- |
| 1 | sr37 | plate | A | 2 |
| 2 | sr56 | punch | NULL | NULL |
| 3 | sl30 | crimper | B | 5 |
| 4 | mp11 | holder | NULL | NULL |
I have a SQL table as following
--------------------------
| REPO | USER | FOLLOWER |
--------------------------
| A | 1 | 3 |
| A | 2 | 4 |
| A | 3 | 6 |
| B | 2 | 7 |
| B | 4 | 2 |
| C | 5 | 3 |
| C | 2 | 6 |
| C | 6 | 5 |
--------------------------
Now, I want to only those rows where USER follows another USER for
same REPO.
i.e. I want rows where elements in FOLLOWER is also in USER for same
REPO.
OUTPUT should be like...
--------------------------
| REPO | USER | FOLLOWER |
--------------------------
| A | 1 | 3 |
| B | 4 | 2 |
| C | 6 | 5 |
| C | 2 | 6 |
--------------------------
Thank You :)
One simple method uses exists:
select t.*
from t
where exists (select 1 from t t2 where t2.repo = t.repo and t2.follower = t.user);
Shouldn't the output actually be as follows, i.e. 4 rows?
--------------------------
| REPO | USER | FOLLOWER |
--------------------------
| A | 1 | 3 |
| B | 4 | 2 |
| C | 6 | 5 |
| C | 2 | 6 |
--------------------------
Is there a way, in SQL, to create an Id for 'linked groups'?
Source Table
+----+-----+
| Id | Id |
+----+-----+
| 1 | 2 |
| 1 | 3 |
| 2 | 3 |
| 4 | 5 |
+----+-----+
Desired Result Table
+----+-----+-----------+
| Id | Id | Group Id |
+----+-----+-----------+
| 1 | 2 | A |
| 1 | 3 | A |
| 2 | 3 | A |
| 4 | 5 | B |
+----+-----+-----------+
see Diagram
Here I'm having table 'TABLE_1' as
|ID | Name | ACID1 | ACVALUE1 | ACID2 | ACVALUE2 | ACID3 | ACVALUE3 |
|----------------------------------------------------------------------
| 1 |ABC | 10 | 82.50 | 20 | 175.95 | 40 | 125.75 |
| 2 |IJK | 30 | 120.55 | 20 | 68.30 | 50 | 25.45 |
| 3 |LMN | 40 | 62.50 | 10 | 87.25 | 30 | 40.50 |
----------------------------------------------------------------------
Another table is AC_TABLE whose ID is recored in above table as ACID1,ACID2,...
___________________
|ID | Name |
|-------------------
|10 | AC1 |
|20 | AC2 |
|30 | AC3 |
|40 | AC4 |
|50 | AC5 |
-------------------
Now what all i want the result in following format
_____________________________
ID | Name | ACName | ACVALUE
------------------------------
1 | ABC | AC1 | 82.50
1 | ABC | AC2 | 175.95
1 | ABC | AC4 | 125.75
2 | IJK | AC3 | 120.55
2 | IJK | AC2 | 68.30
2 | IJK | AC5 | 25.45
3 | LMN | AC4 | 62.50
3 | LMN | AC1 | 87.25
3 | LMN | AC3 | 40.50
-------------------------------
Please help me to get the desired result.
Use Cross Apply to unpivot multiple columns.
First unpivot the Table_1 using cross apply then join the result with the ac_table table. Try this.
SELECT a.id,
a.name,
b.name AS ACName,
cs.ACValue
FROM Table_1 a
CROSS apply (VALUES (ACVALUE1,ACID1),
(ACVALUE2,ACID2),
(ACVALUE3,ACID3))cs(acvalue, ac)
JOIN ac_table b
ON cs.ac = b.id
SQLFIDDLE DEMO