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;
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 a purchase order table that has a list of order numbers and associated order lines with different dates. I want to a query to fetch the orders with orderlines that has the most recent date.
I want the fetch to result in something like below
Here is a way to do this using row_number
select * from (
select t.*
,row_number() over(partition by ordernumber,orderlinenumber order by date desc) as rnk
from <your_table> t
)x
where x.rnk=1
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 years ago.
Improve this question
Ok, basically I've got a lot of temp tables I've created and I'm trying to create Validation for the ProvDiff table.
DROP TABLE #ProvDiff;
IF OBJECT_ID ('temp.dbo.#ProvDiff') IS NOT NULL
DROP TABLE #ProvDiff;
SELECT *
INTO #ProvDiff
FROM
(SELECT DISTINCT *
FROM #finalclaimswithflags f
WHERE f.[Pay-To Prov NPI] <> f.[Rendering Prov NPI]) ProvDiff;
SELECT DISTINCT COUNT(DISTINCT ???) AS 'Unique EI NPIS'
FROM #ProvDiff
In my head it seems like the differences should be able to produce a result and I should be able to do a count on that. But for the life of me I can't figure out how to do that. If I do a count on rendering or pay to then those numbers wouldn't necessarily reflect the value for what are above. I know how many of each are produced for above validation.
Any help would be greatly appreciated
Is this what you want?
SELECT COUNT(*)
FROM (SELECT DISTINCT *
FROM #finalclaimswithflags f
WHERE f.[Pay-To Prov NPI] <> f.[Rendering Prov NPI]
) ProvDiff;
I don't see why a temporary table would be used for this.
For better or worse, SQL Server does not support select count(distinct *), so you pretty much need a subquery.
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 5 years ago.
Improve this question
I have table data like that you show in this screenshot:
and I have query like this so I don't want null values in output, means I don't want field like this
You can get the data next to each other using a trick with row_number(), so that you'll assign numbers to both + and - groups, and then group by it:
select
max(case when Parameter = '+' then Description end),
max(case when Parameter = '-' then Description end)
from (
select
*,
row_number() over (partition by Parameter order by Id) as RN
from
#tmp
) X
group by RN
Example
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 8 years ago.
Improve this question
I have a database with 20 columns but on my select I only want the following 3 columns:
Task
UniqueID
MessageID (unique identifier)
How can I get all the distinct UniqueID while returning the above 3 columns.
SELECT DISTINCT(UniqueID), Task, MessageID
doesn't seem to work? :/
DISTINCT is not a function :
SELECT DISTINCT UniqueID
, Task
, MessageID
FROM table
But this will return all unique rows not unique UniqueID. If you want to retrieve only rows with different UniqueID you should use the GROUP BY clause and aggregate functions with fields Task and MessageID to manipulate grouped data.
Try something like:-
SELECT task,
UniqueID,
MessageID,
FROM (SELECT task,
UniqueID,
MessageID,
Row_number() OVER(PARTITION BY UniqueID ORDER BY MessageID) rn
FROM TableName) t
WHERE rn = 1
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I am trying to create a JOIN with 2 tables and having ce first element of the joined table.
My tables look like this :
Product
id
name
Sales
idProduct
prices
date
I want to have the last sales price for each product but the function FIRST doesn't exist in SQL Server.
Someone have an idea ?
Thanks,
You can use a ranking function like ROW_NUMBER:
WITH CTE AS(
SELECT id, name, idProduct, prices, date,
RN = ROW_NUMBER() OVER (PARTITION BY idProduct ORDER BY date DESC)
FROM dbo.Product p INNER JOIN dbo.Sales s on p.id = s.idProduct
)
SELECT * FROM CTE WHERE RN = 1
Ranking Functions (Transact-SQL)
The CTE is a common-table-expression similar to a sub-query but more readable and maintainable.
If it's SQL Server, simply use:
SELECT TOP 1 *
FROM Product p
JOIN Sales s ON p.id = s.idProduct
ORDER BY s.Date DESC