I have a table Product
with
ProductNo ProductDetail UniqueiD(Primarykey)
L1234 ProductA 1
L1234 ProductB 2
L1234 ProductC 3
M1234 ProductD 4
M1234 ProductE 5
So i need a select query that will display distinct product no with ids for displaying in p-listbox.
say
Name code
L1234 1
M1234 2
How do i achieve this?
Thanks
One method is:
select distinct name, dense_rank() over (order by name)
from product;
That said, I would probably use group by:
select name, row_number() over (order by name) as code
from product
group by name;
Related
I have a list of orders, I need to find which ones occur with code 47 more than once with different users. For example:
ORDER_ID CODE USER
111 47 1
111 47 2
222 47 1
333 47 1
333 47 2
444 47 1
The expected result is 111 and 333.
How can I accomplish this?
Regards
I think you want aggregation and having:
select order_id
from orders o
where code = 47
group by order_id
having min(user) <> max(user);
You can also express the having as:
having count(distinct user) >= 2
You can try below -
select order_id from tablename
group by order_id
having count(distinct user)>=1
You can do it via row_number() as well
Select distinct order_id from
(select order_id, code, row_number()
over
( Partition by order_id, code
Order by order_id, code) rn
from
tablename
where user in (1,2)
) where rn>=1
But I guess you already have a user column hence i dont think you require extra manipulation
Select orderid, code from table
Group by orderid, code having
max(distinct user) >=1
In a firebird database with a table "Sales", I need to select the first sale of all customers. See below a sample that show the table and desired result of query.
---------------------------------------
SALES
---------------------------------------
ID CUSTOMERID DTHRSALE
1 25 01/04/16 09:32
2 30 02/04/16 11:22
3 25 05/04/16 08:10
4 31 07/03/16 10:22
5 22 01/02/16 12:30
6 22 10/01/16 08:45
Result: only first sale, based on sale date.
ID CUSTOMERID DTHRSALE
1 25 01/04/16 09:32
2 30 02/04/16 11:22
4 31 07/03/16 10:22
6 22 10/01/16 08:45
I've already tested following code "Select first row in each GROUP BY group?", but it did not work.
In Firebird 2.5 you can do this with the following query; this is a minor modification of the second part of the accepted answer of the question you linked to tailored to your schema and requirements:
select x.id,
x.customerid,
x.dthrsale
from sales x
join (select customerid,
min(dthrsale) as first_sale
from sales
group by customerid) p on p.customerid = x.customerid
and p.first_sale = x.dthrsale
order by x.id
The order by is not necessary, I just added it to make it give the order as shown in your question.
With Firebird 3 you can use the window function ROW_NUMBER which is also described in the linked answer. The linked answer incorrectly said the first solution would work on Firebird 2.1 and higher. I have now edited it.
Search for the sales with no earlier sales:
SELECT S1.*
FROM SALES S1
LEFT JOIN SALES S2 ON S2.CUSTOMERID = S1.CUSTOMERID AND S2.DTHRSALE < S1.DTHRSALE
WHERE S2.ID IS NULL
Define an index over (customerid, dthrsale) to make it fast.
in Firebird 3 , get first row foreach customer by min sales_date :
SELECT id, customer_id, total, sales_date
FROM (
SELECT id, customer_id, total, sales_date
, row_number() OVER(PARTITION BY customer_id ORDER BY sales_date ASC ) AS rn
FROM SALES
) sub
WHERE rn = 1;
İf you want to get other related columns, This is where your self-answer fails.
select customer_id , min(sales_date)
, id, total --what about other colums
from SALES
group by customer_id
So simple as:
select CUSTOMERID min(DTHRSALE) from SALES group by CUSTOMERID
I have data set of call customer, I want to make count () to know:
Total number of calls for each customer
Total duration of call for each customer
Total of locations the customer he where in
This my data:
Phone no. - Duration In minutes - Location
1111 3 88
2222 4 33
3333 4 4
1111 7 55
3333 9 4
3333 7 3
the result of query:
phone no- Total number of records -Total duration of calls- Total of location
1111 2 10 2
2222 1 4 1
3333 3 20 2
This is almost similar to fthiella answer. Try like this
select PhoneNo,
count(*) as TotalNumberOfRecords,
sum(DurationInMinutes) as TotalDurationOfCalls,
count(distinct location) as TotalOfLocations from yourtablename
group by PhoneNo
You can use a GROUP BY query with basic aggregated functions, like COUNT(), SUM() and COUNT(DISTINCT) like this:
select phone_no, count(*), sum(duration), count(distinct location)
from tablename
group by phone_no
answer for your question is
select Phone no,count(Duration In minutes),sum(Duration In minutes),count(distinct Location) from Tablename group by Phone no order by Phone no;
I have made temporary table for testing and it gives same output as you mention. look following query :
declare #TEMP table (phone_no int, duration int, location int)
insert into #temp values(1111,3,88),(2222,4,33),(3333,4,4),(1111,7,55),(3333,9,4),(3333,7,3)
select phone_no, count(*), sum(duration), count(distinct location)
from #TEMP
group by phone_no
you just can consider this query :
select phone_no, count(*), sum(duration), count(distinct location)
from #TEMP
group by phone_no
I want list all the rows by alternative publisher with price ascending, see the example table below.
id publisher price
1 ABC 100.00
2 ABC 150.00
3 ABC 105.00
4 XYZ 135.00
5 XYZ 110.00
6 PQR 105.00
7 PQR 125.00
The expected result would be:
id publisher price
1 ABC 100.00
6 PQR 105.00
5 XYZ 110.00
3 ABC 105.00
7 PQR 125.00
4 XYZ 135.00
2 ABC 150.00
What would be the required SQL?
This should do it:
select id, publisher, price
from (
select id, publisher, price,
row_number() over (partition by publisher order by price) as rn
from publisher
) t
order by rn, publisher, price
The window functions assigns unique numbers for each publisher price. Based on that the outer order by will then first display all rows with rn = 1 which are the rows for each publisher with the lowest price. The second row for each publisher has the second lowest price and so on.
SQLFiddle example: http://sqlfiddle.com/#!4/06ece/2
SELECT id, publisher, price
FROM tbl
ORDER BY row_number() OVER (PARTITION BY publisher ORDER BY price), publisher;
One cannot use the output of window functions in the WHERE or HAVING BY clauses because window functions are applied after those. But one can use window functions in the ORDER BY clause.
SQL Fiddle.
Not sure what your table name is - I have called it publishertable. But the following will order the result by price in ascending order - which is the result you are looking for:
select id, publisher, price from publishertable order by price asc
if I've got it right. You should use ROW_NUMBER() function to range prices inside of each publisher and then order by this range and publisher.
SELECT ID,
Publisher,
Price,
Row_number() OVER (PARTITION BY Publisher ORDER BY Price) as rn
FROM T
ORDER BY RN,Publisher
SQLFiddle demo
I want to select rows that have a distinct Title Column.
Id Title Type
1 Bronze Group
2 Bronze Group
3 Bronze Group
4 Silver Group
5 Silver Group
6 Silver Group
7 Gold Group
8 Gold Group
9 Gold Group
10 Platinum Group
11 Platinum Group
12 Platinum Group
I thought this would be a simple query but i'm struggling! If anyone can help that would be great
SELECT DISTINCT(Title), Id
FROM Package
WHERE Type='Group'
ORDER BY Id ASC
You need to group by the title. And you have to tell the DB which rule to apply when selecting the id for duplicate entries. For instance the smallest id for every unique title:
SELECT Title, min(Id) as minid
FROM Package
WHERE Type='Group'
GROUP BY Title
ORDER BY min(Id) ASC
You have to drop the ID off as it is unique and makes it DISTINCT. Something like this.
SELECT DISTINCT Title
FROM Package
WHERE Type='Group'