I'm trying to get a table where the number of rows will always have to be 12xN. Where N is the number of different products that exist in an event table.
Basically I try to obtain the evolution of the value from one month to another.
What SQL logic should I use?
table
idfecha
update_id
value
--------
---------
-----
20220105
123
3
20220221
345
2
20220730
567
4
20221011
678
2
20221102
789
5
dimtime
idfecha
label_month
--------
---------
20220101
enero
20220102
enero
20220103
enero
20220104
enero
20220105
enero
20220106
enero
20220107
enero
20220108
enero
…
…
20221229
diciembre
20221230
diciembre
20221231
diciembre
result
product_id
value
----------
------
aaa
3
aaa
3
aaa
3
aaa
3
aaa
3
aaa
3
aaa
4
aaa
4
aaa
4
aaa
2
aaa
2
aaa
2
ccc
0
ccc
2
ccc
2
ccc
2
ccc
2
ccc
2
ccc
2
ccc
2
ccc
2
ccc
2
ccc
5
ccc
5
I made some attempts with:
GROUP BY dimtime.month_label, table.product_id.
and combining it with a left outer join, where left is the dimtime.
However, I do not get what I want and I am very clear about why.
Even so, I do not arrive at the adequate logic to achieve it.
Do I have to use an over partition?
thank you!
I have a table name "CompanyID"
ID | Name
_____________
AAA | Semsung
BBB | NoKeea
CCC | JOKER
DDD | Papa
now I have another table "DETAILS"
Product | Company | Date
_________________________
asdfasdf| AAA | asdfd
asdfasdf| AAA | asdfd
asdfasdf| BBB | asdff
asdfasdf| BBB | asdff
asdfasdf| BBB | asdff
asdfasdf| BBB | asdff
asdfasdf| CCC | asdfd
asdfasdf| CCC | asdfd
asdfasdf| CCC | asdfd
Now I want to get a count of how many AAA,BBB,CCC,DDD are there
ID | TotalCount
_______________
AAA| 2
BBB| 4
CCC| 3
DDD| 0
I tried using conditions like CompanyID.ID = Details.Company
but with that, I also get results like
AAA 2
AAA 0
AAA 0
AAA 0
BBB 4
Please Help Me
You need a LEFT join of the tables and aggregation:
SELECT c.ID, COUNT(d.Company) AS TotalCount
FROM CompanyID AS c LEFT JOIN Details AS d
ON d.Company = c.ID
GROUP BY c.ID
Where as #Forpas answer is enough, you can also try-
SELECT CompanyID.ID, DCount("[Company]","[DETAILS]","[Company]='" & CompanyID.ID & "'") AS [Total Count] FROM CompanyID;
I have a table with an ARR column storing various values. I have to generate row number when my ARR column has value 4.
My table
Sno Data ARR
----------------
1 AAA 0
2 AAA 1
3 AAA 1
4 AAA 2
5 AAA 3
6 AAA 4
7 AAA 5
8 AAA 5
9 AAA 6
10 AAA 4
11 AAA 5
12 AAA 6
Required result:
Sno Data ARR RowNumber
----------------------------
1 AAA 0 0
2 AAA 1 0
3 AAA 1 0
4 AAA 2 0
5 AAA 3 0
6 AAA 4 1
7 AAA 5 2
8 AAA 5 3
9 AAA 6 4
10 AAA 4 1
11 AAA 5 2
12 AAA 6 3
Here at Sno=10 (value 4 in ARR), the rownumber starts again at 1.
I think you want:
select t.sno, t.data, t.arr,
(case when grp >= 1
then row_number() over (partition by grp order by sno)
else 0
end) as rownumber
from (select t.*,
sum(case when arr = 4 then 1 else 0 end) over (order by sno) as grp
from t
) t;
Here is a table:
declare #t table(id int,name varchar(10),count int,quantity int)
insert #t(id,name,count,quantity) values( 1,'aaa',1,100)
insert #t(id,name,count,quantity) values( 3,'bbb',3,200)
insert #t(id,name,count,quantity) values( 2,'ccc',2,50)
insert #t(id,name,count,quantity) values( 6,'ddd',1,300)
insert #t(id,name,count,quantity) values( 5,'eee',5,20)
SELECT * FROM #t
OUTPUT:
id name count quantity
1 aaa 1 100
3 bbb 3 200
2 ccc 2 50
6 ddd 1 300
5 eee 5 20
How to get the following result by sql-server statements, Would a simple cte sql server statement work?
id name count quantity
1 aaa 1 100
3 bbb 1 200
3 bbb 2 200
3 bbb 3 200
2 ccc 1 50
2 ccc 2 50
6 ddd 1 300
5 eee 1 20
5 eee 2 20
5 eee 3 20
5 eee 4 20
5 eee 5 20
Thanks!
Try using SPT_Values table from master database.
SELECT t.id, t.name, c.number as [count], t.quantity
FROM #t t
CROSS APPLY
(SELECT * FROM master.dbo.spt_values where type='P'
and number between 1 and t.[count] )c
Result:
+----+------+-------+----------+
| id | name | count | quantity |
+----+------+-------+----------+
| 1 | aaa | 1 | 100 |
| 3 | bbb | 1 | 200 |
| 3 | bbb | 2 | 200 |
| 3 | bbb | 3 | 200 |
| 2 | ccc | 1 | 50 |
| 2 | ccc | 2 | 50 |
| 6 | ddd | 1 | 300 |
| 5 | eee | 1 | 20 |
| 5 | eee | 2 | 20 |
| 5 | eee | 3 | 20 |
| 5 | eee | 4 | 20 |
| 5 | eee | 5 | 20 |
+----+------+-------+----------+
Thanks to Mr. Mirza's help #Shakeer Mirza, I've coded as this.
declare #t TABLE (id int,name varchar(10),count int,quantity int)
insert #t(id,name,count,quantity) values( 1,'aaa',1,100)
insert #t(id,name,count,quantity) values( 3,'bbb',3,200)
insert #t(id,name,count,quantity) values( 2,'ccc',2,50)
insert #t(id,name,count,quantity) values( 6,'ddd',1,300)
insert #t(id,name,count,quantity) values( 5,'eee',5,20)
--SELECT * FROM #t
DECLARE #maxcount INT
SET #maxcount = 10000;--if count exceed 2048
;WITH CTE AS (
SELECT 1 AS number
UNION ALL
SELECT number + 1
FROM CTE
WHERE number < #maxcount
)
SELECT t.id, t.name, c.number as [count], t.quantity
FROM #t t
CROSS APPLY
(SELECT * FROM CTE where number between 1 and t.[count] )c
ORDER BY t.id,c.number
option (MAXRECURSION 0)-- if recursion exceed 100
i have the example database here,
table products
id | name | code | minimum_stock | stock | maximum_stock
1 AAA AAA 50 75 100
2 BBB BBB 70 50 300
3 CCC CCC 100 200 150
4 DDD DDD 40 25 100
5 EEE EEE 70 10 100
in this case i want to show only data who is stock exceed compared by their minimum_stock or maximum_stock(below or above their minimum maximum stock) and also i want to show datain safe stock position (between minimum and maximum stock),
stock value in here is a result from aggregate sum function
if i want only show data exceed minimum stock, the result output must be like this
id | name | code | **minimum_stock** | **stock** | maximum_stock
2 BBB BBB **70** **50** 300
4 DDD DDD **40** **25** 100
5 EEE EEE **70** **10** 100
and if i want only show data exceed maximum stock, the result output must be like this
id | name | code | minimum_stock | **stock** | **maximum_stock**
3 CCC CCC 100 **200** **150**
and the last one the stock between minimum and maximum stock, it must be like this
id | name | code | **minimum_stock** | **stock** | **maximum_stock**
1 AAA AAA **50** **75** **100**
how can I do that?
Try this
Minimum Stock
Select * from table where stock < minimum_stock
maximum stock
Select * from table where stock > maximum_stock
between minimum and maximum stock
Select * from table where stock < maximum_stock and stock > minimum_stock