Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
I have a problem with my query:
UPDATE [DIM_Category]
SET Category 3 = (SELECT AVG(Category 2)
FROM [DIM_Category] GROUP BY Category)
I want to do like in this exemple and I don't know how.
Category
Category 2
Category 3
OCS
10800
20350
OCS
29600
20350
Netflix
24000
23000
Netflix
22000
23000
As much as I dislike the concept you are doing here the query itself needs to be correlated. One way you can do that easily is with a cte. This will work on the example you posted. I included my code to create a table so I had something work with. You would obviously not need that part. ;)
create table DIM_Category
(
Category varchar(20)
, Category2 int
, Category3 int
);
insert DIM_Category
(
Category
, Category2
) values
('OCS', 10800)
,('OCS', 29600)
,('Netflix', 24000)
,('Netflix', 22000);
with CatAvg as
(
select Category
, CatAverage = AVG(Category2)
from DIM_Category
group by Category
)
update c
set Category3 = ca.CatAverage
from DIM_Category c
join CatAvg ca on ca.Category = c.Category;
select *
from DIM_Category;
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have two tables in sqlite. Let's say table_1 and table_2. table_1 has two columns named(Barcode, Price) , similarly table_2 has two columns named(Barcodes, Price). I want to update table_1.Price. if table_1.Barcode is present in table_2. Barcodes, then take the price of table_2. Price otherwise keep it table_1.Price.
I hope I am very clear. Let me just write it down in a programmatic sense to be very clear.
if(table_1.Barcode == table_2.Barcodes)
table_1.Price = table_2.Price
else
table_1.Price = table_1.Price
I need sqlite query which does this. Is it possible?
If your version of SQLite is 3.33.0+ you can do it with UPDATE...FROM syntax:
UPDATE table_1 AS t1
SET Price = t2.Price
FROM table_2 t2
WHERE t2.Barcode = t1.Barcode;
For older versions of SQLite you need a corelated subquery which returns the price from table_2:
UPDATE table_1
SET Price = COALESCE(
(SELECT t2.Price FROM table_2 t2 WHERE t2.Barcode = table_1.Barcode),
Price
);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a mapping table below and I want to check either ID exists in the corresponding Table or not. For example: check ID 001 in "xxabc" database table and return "Yes" if table has 001 ID present else "No".
ID TableName
--------------------------
001 xxabc
003 xxabc
004 xxpqr
009 xxghi
Try this:
SELECT S.[ID]
,CASE WHEN M.[ID] IS NULL THEN 'No' ELSE 'Yes' END
--,IIF(M.[ID] IS NULL, 'No', 'Yes') -- for SQL Server 2012+
FROM [source_table] S
LEFT JOIN [mapping_table] M
ON S.[ID] = M.[ID]
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a table that contains a list of debts owed by customers. I need to write a SQL query to find which customers have an increasing amount of debt each year. Basically, I am trying to find the "bad apples", the people who continually get debts, in an increasing amount, every year. I suppose I am looking for a trend?
I have created a SQL Fiddle with some sample data. In the example, I don't care about customerId 2174 as they have only had two debts, many years ago, however, customerId 5218 has had an increasing amount of debts over the last 3-4 years.
Ideally, I'd like a list of customerIds that show a "trend" of increasing quantity of debts. i.e. they have 1 in 2015, 5 in 2016, 30 in 2017 etc.
You may try this.
; with cte as
(SELECT customerId
,YEAR(debtDate) AS debtYear
,COUNT(*) AS debtCount
FROM dbo.Debts
----- apply year filter over there if want to select record for particular year range
GROUP BY YEAR(debtDate)
,customerId
)
, CT AS
(
SELECT C.* FROM CTE AS C
CROSS APPLY (
SELECT * FROM CTE AS CC
WHERE C.customerId=CC.customerId
AND C.debtYear>CC.debtYear
AND C.debtCount>=CC.debtCount
) AS D
)
SELECT DISTINCT customerId FROM CT --- IF NEED JUST CUSTOMER NAME
------- OR IF NEED ALL DETAILS ADD debtYear, debtCount COLUMN
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have table like the below which has overlapping weeks from two years 2018 and 2019. I want to identify the first set of 3 consecutive weeks for each customer using TSQL.
The resultset should have the customer_id and the max(order_date) from the first set of 3 consecutive weeks like below.
I created my own data. Try running this (this uses windowing functions which is a 2012 and on feature):
create table i1(i1 int identity (1,1) primary key, idate date, name varchar(25));
insert into i1 values ('1-1-2019','b'), ('1-8-2019','b'), ('1-15-2019','b'),
('1-1-2019','c'), ('1-8-2019','c'), ('1-24-2019','c'),('1-31-2019','c')
;
with ct1 as (select name,idate, datepart(ww,idate) -lag(datepart(ww,idate),1,0) over (partition by name order by idate) diff
,row_number() over (partition by name order by idate) id from i1),
ct2 as (
select name , idate,sum(diff) over ( partition by name order by idate rows between 3 preceding and current row) diff2
from ct1)
select name, idate
from ct2 where diff2=3
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a table like this..
ID City
-----------------
1 Jakarta
1 Bogor
2 Bandung
2 Surabaya
3 Solo
3 Null
I want the query would returns:
ID City
---------------
1 Bogor
2 Surabaya
3 Null
Any ideas? I need your help.. I used some methods such as rank , distinct, max.. but the result is not what I expected..
You can use a CTE with the ROW_NUMBER-function:
WITH CTE AS
(
SELECT Id, City, Rn = ROW_NUMBER() OVER (Partition By ID
Order by Id DESC)
FROM dbo.Cities
)
SELECT Id, City
FROM CTE
WHERE RN = 1
Altough i don't know which column you want to use to determine the "latest" record. Use that in Order by Id DESC.