SQL Server SELECT first [closed] - sql

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

Related

How to create customer-coupon combination [closed]

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;

SQL Server Temp Table to a Select Distinct Count Distinct quetsion [closed]

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.

Why I got Not a single-group group function in sql [closed]

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
select
author.name,
max(count(paper.pid))
from
paper,
author
where
paper.aid=author.aid
group by
author,.name
Max of count does not make sense. Do you mean this:
select top 1 name, paper_count
from
(
select author.name,count(paper.pid) as paper_count
from paper
join author on paper.aid=author.aid
group by author.name
)
order by paper_count desc
*this answer assumes sql server.
or (as Giorgos Betsos points out:)
select top 1 author.name,count(paper.pid) as paper_count
from paper
join author on paper.aid=author.aid
group by author.name
order by paper_count desc
http://sqlfiddle.com/#!6/7ed37/3
(Some SQL platforms won't let you order by the results of an agg function but SQL server does.)
It looks like you have a comma before "name" in your group by statement. Also, max(count()) doesn't really work or make sense. Try the below to get the author with the greatest number of papers:
select *
from(
select author.name,count(paper.pid)
from paper,author
where paper.aid=author.aid
group by author.name
order by count(paper.pid) desc
)
where rownum=1
And here is the sqlfiddle showing this in action: fiddle. Jane has 2 papers and John has 3. John shows in the results.
Alternatively you can do rownum <= x where you want the top X values.
This is an Oracle 11g solution. If using some other brands of sql you'll probably want to use Top 1/Top x I believe.

SQL QUERY for finding set of highest numbers [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
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
Closed 9 years ago.
Improve this question
how to get top 1,00,000 customers name and email id who have booked maximum number of tickets? The table has columns like:
NAME, CUST_ID, JOINING DATE, NO. OF TICKETS BOOKED, EMAIL_ID.
Something like the following should work if you are using Microsoft Sql server (tsql)
Select TOP 120 columns FROM table ORDER BY columns desc
SELECT TOP 100000 NAME, CUST_ID, [JOINING DATE], [NO. OF TICKETS BOOKED], EMAIL_ID
FROM YOUR_TABLE
ORDER BY [NO. OF TICKETS BOOKED] DESC
Are you looking for this ?
if you are working with SQL SERVER

SQL - What syntax to use? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a table named "product_to_category" with 2 column "product_id and category_id".
I have about 500 product ID with 1000 category ID associate with it.
Now I want to add another category ID into all the product ID.
What syntax should I use to make this happen?
Thank you.
Are you looking for this?
INSERT INTO product_to_category(product_id, category_id)
SELECT product_id, 25 -- < new category that you want to add
FROM product_to_category
GROUP BY product_id
That will add category with id 25 to all unique products that you already have in product_to_category. If not all of your products have at least one category defined, then you can select from some product table that I'm sure you have.
Here is SQLFiddle demo
Use something like this:
UPDATE product_to_category
SET category_id=('your_new_category_id')
WHERE product_id = your_product_id;
Remember the Where clause, otherwise this will update all your rows.
The Where clause could also contain a SELECT statement that would select all product_id's for all your products that need to update their category.
Something like:
UPDATE product_to_category
SET category_id=('your_new_category_id')
WHERE product_id = (SELECT product_id .... condition);