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 hospital data set where each patient is admitted with a diagnosis.
I want to display each patient once with the diagnosis each patient is admitted with with most times.
right now each patient that have multiple diagnoses appear in multiple rows.
the dataset is similar to this
$ Subject_id diagnosis
1112 SEIZURE
1112 GASTROINTESTINAL BLEED
1112 SEIZURE
1113 FEVER
how do I collapse and display the multiple diagnosis to one row and show by the most frequently used diagnosis?
You can use window functions and aggregation:
select sd.*
from (select subject_id, diagnosis, count(*) as cnt,
row_number() over (partition by subject_id order by count(*) desc) as seqnum
from t
group by subject_d, diagnosis
) sd
where seqnum = 1;
Note: If there are ties for the most common diagnosis, this arbitrarily returns one row. If you want all rows in the event of ties, then use rank() instead of row_number().
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 4 days ago.
Improve this question
I need to display ranking along with city based on city selection.
Suppose if someone select Nagpur, we need to show the predecessor city, selected city and followed by successor city with their respective ranking.
Criteria
If someone selects Mumbai then, No Data, Mumbai-1, Delhi-2
If someone selects Delhi then, Mumbai-1, Delhi-2, Lucknow-3
If someone selects Nagpur then, Lucknow-3, Nagpur-3, Pune-3 (based on city's sorting order alphabetically, if there are multiple cities having same ranking)
If someone selects Kanpur then, Patna-7, kanpur-8, No Data.
The data is in this format:
Can someone help to achieve this? I am using SQL Server 2016
It may not be the best solution, but it will be useful for now. I recommend that you always add an id to the tables and the names of the fields can be easily identified to which table they belong
DROP TABLE #TableTempRankingCity
DECLARE #Id INT;
SELECT ROW_NUMBER() OVER(
ORDER BY [City]) AS fila, *
INTO #TableTempRankingCity
FROM RankingCiudades
ORDER BY Rank,City
--SELECT * FROM #TableTempRankingCity
SELECT #Id = fila
FROM #TableTempRankingCity
WHERE City = 'Lucknow'
SELECT
City
,Rank
FROM #TableTempRankingCity
WHERE fila IN (#Id-1,#Id+1,#Id+2)
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 1 year ago.
Improve this question
I am try to understant how to make a table that tells me in which specific day were written the most mentions about the persons in my table.
SELECT
person,
COUNT(1) AS count_mentions,
COUNT(DISTINCT current_date) AS mention_per_date,
FROM
`aesthetic-honor-311413.big_data_alon_peled_2021.israel_media_person`
GROUP BY
person
ORDER BY
current_date asc
LIMIT
10;
EXPECTED RESULT:
person mention_per_date
Tomer 24
Shalev 18
Yosef 15
Eran 15
Gal 11
(Fictive names and numbers)
I am try to understant how to make a table that tells me in which specific day were written the most mentions about the persons in my table.
For this question, I would expect a query like this:
SELECT person, date, COUNT(*)
FROM `aesthetic-honor-311413.big_data_alon_peled_2021.israel_media_person`
GROUP BY person, date
QUALIFY ROW_NUMBER() OVER (PARTITION BY person ORDER BY COUNT(*) DESC) = 1;
This returns the date that most frequently occurs for each person. You don't mention the name of the date column, so this just uses date.
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 1 year ago.
Improve this question
Here is my error message:
This is the text
Oracle requires parentheses for the subqueries:
(select TeamName As Name, points As points
from standing
group by TeamName, points
having Length(TeamName) >= 3 and points > 5
order by points DESC, TeamName ASC
Fetch first 2 rows only
) union
(select TeamName As Name, points As points
from standing
group by TeamName, points
having Length(TeamName) >= 3 and points > 5
order by points ASC
Fetch first 1 rows only
)
The parentheses are needed when the subqueries have either order by or fetch clauses. They are needed to distinguish between applying those clauses to an individual subquery or to the results of the entire union.
Also, you might want union all instead of union -- union incurs overhead for removing duplicates.
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'm trying in SQL Server, without success to create a customer-coupon combination. the raw data looks like that:
raw data
The table goes on until 100 rows, it's a combination of 10 different customers, and 10 different coupons.
The desired result should look like this:
desired result
My attempts so far wasn't close, I was using row_number and this next script:
WITH cte AS
(
SELECT
customer_id, coupon,
ROW_NUMBER() OVER (PARTITION BY coupon ORDER BY customer_id) rnk
FROM #temp
)
SELECT *
FROM cte
WHERE rnk = 1
enter image description here
Thanks!
My best guess, if I am reading between the lines correctly is to use a couple of DENSE_RANK's. This isn't tested though, as images of data don't help us help you as we can't copy the text/data out of them:
SELECT DISTINCT
sq1.CustomerID
sq2.coupon
FROM (SELECT YT.CustomerID,
DENSE_RANK() OVER (ORDER BY YT.CustomerID) AS DR
FROM dbo.YourTable YT) sq1
JOIN (SELECT YT.Coupon,
DENSE_RANK() OVER (ORDER BY YT.Coupon) AS DR
FROM dbo.YourTable YT) sq2 ON sq1.DR = sq2.DR;
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 9 years ago.
Improve this question
cust_id acct_id trxn_amt trxn_cnt
1 66685638 10,028,717,398 199.75 5
2 66685638 10,028,849,377 76.16 2
Say I have a table of customers with multiple account IDs and I want to create a new column for that's the summation of all of the transaction amounts for each customer (199.75+76.16 for cust_id=66685638) as well as another column thats the % of total spend for each account (for first account its 199.75/(76.15+199.75)). Each customer may have anywhere from 2-4 acct_ids.
Thanks so much.
what about:
select cust_id,
sum(trxn_amt) as total_amount,
trxn_amt / sum(trxn_amt) as pct
from customers
group by cust_id
order by cust_id;
or if you want to see each individual row from the customers table:
select cust_id,
acct_id,
trxn_amt,
sum(trxn_amt) as over (partition by cust_id) as total_amount,
trxn_amt / sum(trxn_amt) as over (partition by cust_id) as pct
from customers
order by cust_id;