I have a query like this:
Select * from V_Receipt Where F_Exhibition='11000' order by F_Exhibitor_Name
When executed I get duplicate values, how can I resolve this.
You need to use DISTINCT but you will also have to explicitly define each field
SELECT DISTINCT field1, field2, field3 /* etc etc */
FROM V_Receipt
WHERE F_Exhibition = '11000'
ORDER BY F_Exhibitor_Name DESC
with cte
as
(select row_number(partition by feild 1 order by field1) as rnk,* from V_Receipt )
select * from cte where rnk=1;
Related
If I make a COUNT on the fields of mytable with a LIMIT parameter,
SELECT COUNT(field1), COUNT(field2)
FROM mytable
LIMIT 10000
And if I select their fields
SELECT field1, field2
FROM mytable
LIMIT 10000
Are there two requests select and count on the same 10000 lines ?
Because I can add an ORDER BY clause to the second request, but for a COUNT request it is not possible.
Thanks for help
Don't count on it, without an ORDER BY clause to specify the wanted order, there are no guarantees !
Just wrap the first query with another select :
SELECT COUNT(field1),count(field2)
FROM(SELECT field1,field2
FROM mytable
ORDER BY Order_Column
LIMIT 10000) t
SELECT field1, field2
FROM mytable
ORDER BY Order_Column
LIMIT 10000
These queries will process the same rows
Unless and until you add order by to your query, you cannot say anything about the order of the rows returned. You can add order by to a nested query for the select count(...) to solve this problem. Essentially, the second query becomes the nested query for your select count(...)
-- Second query
SELECT field1, field2
FROM mytable
ORDER BY ... LIMIT 10000
-- First query
SELECT COUNT(field1), COUNT(field2) FROM (
SELECT field1, field2
FROM mytable
ORDER BY ... LIMIT 10000
}
I want to select full rows with use of my resulting group by. So that I can inspect rows separately. This is what I came up with but It doesn't return anything and I have no idea why.
SELECT * FROM myTable t ,
( SELECT
prop_1, prop_2 , prop_3
FROM
( SELECT COUNT(*) AS countx, prop_1, prop_2, prop_3 FROM myTable
GROUP BY prop_1, prop_2, prop_3 )
WHERE countx>1 ) subselect
WHERE
t.prop_1 = subselect.prop_1
AND t.prop_2 = subselect.prop_2
AND t.prop_3 = subselect.prop_3 ;
Maybe I should try totally different approach but please explain me why this isn't working
I need you need something like this:
select * from myTable where count(*) over (partition by prop_1, prop_2,prop_3) >1
Or if you need the duplications maybe it is cleaner this way:
select * from myTable t1, myTable t2 where
t1.prop_1 = t2.prop_1 and
t1.prop_2 = t2.prop_2 and
t1.prop_3 = t2.prop_3 and
t1.id <t2.id
Otherwise you can merge your internal 2 queries using having count(*)>1 in the most internal one, instead of the where in the second.
Remove the middle sub-query. Try this:
SELECT * FROM myTable t ,
( SELECT COUNT(*) AS countx, prop_1, prop_2, prop_3 FROM myTable
GROUP BY prop_1, prop_2, prop_3 ) subselect
WHERE
t.prop_1 = subselect.prop_1
AND t.prop_2 = subselect.prop_2
AND t.prop_3 = subselect.prop_3 ;
I want to sort according to date first and then if date is similar then according to id..How to do that in Informix/HSQL query?
SELECT FIELD1, FIELD2 FROM TABLE ORDER BY FIELD1 ASC, FIELD2 ASC
A good tutorial on this SQL ORDER BY
This should work:
SELECT * FROM Table
ORDER BY date, id;
Does
[rest of query] order by date, id
work?
Try this(adjusted to your needs):
SELECT * FROM table ORDER BY datecol ASC, id ASC
select * from (select * from tablename order by col1) AS T order by col2
(It's necessary to give an alias to the virtual table, hence "AS T")
select * from (select * from tablename order by col1) AS T order by col2
This works but is redundant and obtuse. Note the subquery (on "col1") becomes the secondary sort; the primary sort is the encompassing query. Some environments may require the "as T" aliasing of the subquery, but not all.
select * from tablename order by col2, col1
This does the same thing as above, with simple clarity
Or for a more realistic example:
select * from customers order by lastname, firstname
I'm using SQL Server 2005 and I'm trying to achieve something like this:
I want to get the first x rows and the last x rows in the same select statement.
SELECT TOP(5) BOTTOM(5)
Of course BOTTOM does not exist, so I need another solution. I believe there is an easy and elegant solution that I'm not getting. Doing the select again with GROUP BY DESC is not an option.
Using a union is the only thing I can think of to accomplish this
select * from (select top(5) * from logins order by USERNAME ASC) a
union
select * from (select top(5) * from logins order by USERNAME DESC) b
Check the link
SQL SERVER – How to Retrieve TOP and BOTTOM Rows Together using T-SQL
Did you try to using rownumber?
SELECT *
FROM
(SELECT *, ROW_NUMBER() OVER (Order BY columnName) as TopFive
,ROW_NUMBER() OVER (Order BY columnName Desc) as BottomFive
FROM Table
)
WHERE TopFive <=5 or BottomFive <=5
http://www.sqlservercurry.com/2009/02/select-top-n-and-bottom-n-rows-using.html
I think you've two main options:
SELECT TOP 5 ...
FROM ...
ORDER BY ... ASC
UNION
SELECT TOP 5 ...
FROM ...
ORDER BY ... DESC
Or, if you know how many items there are in the table:
SELECT ...
FROM (
SELECT ..., ROW_NUMBER() OVER (ORDER BY ... ASC) AS intRow
FROM ...
) AS T
WHERE intRow BETWEEN 1 AND 5 OR intRow BETWEEN #Number - 5 AND #Number
Is it an option for you to use a union?
E.g.
select top 5 ... order by {specify columns asc}
union
select top 5 ... order by {specify columns desc}
i guess you have to do it using subquery only
select * from table where id in (
(SELECT id ORDER BY columnName LIMIT 5) OR
(SELECT id ORDER BY columnName DESC LIMIT 5)
)
select * from table where id in (
(SELECT TOP(5) id ORDER BY columnName) OR
(SELECT TOP(5) id ORDER BY columnName DESC)
)
EDITED
select * from table where id in (
(SELECT TOP 5 id ORDER BY columnName) OR
(SELECT TOP 5 id ORDER BY columnName DESC)
)
No real difference between this and the union that I'm aware of, but technically it is a single query.
select t.*
from table t
where t.id in (select top 5 t2.id from table t2 order by MyColumn)
or
t.id in (select top 5 t2.id from table t2 order by MyColumn desc);
SELECT *
FROM (
SELECT x, rank() over (order by x asc) as rown
FROM table
) temp
where temp.rown = 1
or temp.rown = (select count(x) from table)
Then you are out - doing the select again IS the only option, unless you want to pull in the complete result set and then throwing away everything in between.
ANY sql I cna think of is the same way - for the bottom you need to know first either how many items you have (materialize everything or use count(*)) or a reverse sort order.
Sorry if that does not suit you, but at the end.... reality does not care, and I do not see any other way to do that.
I had to do this recently for a very large stored procedure; if your query is quite large, and you want to minimize the amount of queries you could declare a #tempTable, insert into that #tempTable then query from that #tempTable,
DECLARE #tempTable TABLE ( columns.. )
INSERT INTO #tempTable
VALUES ( SELECT.. your query here ..)
SELECT TOP(5) columns FROM #tempTable ORDER BY column ASC -- returns first to last
SELECT TOP(5) columns FROM #tempTable ORDER BY column DESC -- returns last to first
I have a table; let it be called table1; with the following fields and data
alt text http://img228.imageshack.us/img228/3827/45939084.png
I need a query that returns the record with the maximum value in Field3 for each group of records having the same value in Field2. So that the query returns:
alt text http://img87.imageshack.us/img87/62/48847706.png
How could this be done using SQL queries ?
This:
WITH q AS
(
SELECT *, ROW_NUMBER() OVER (PARTITION BY field2 ORDER BY field3 DESC) AS rn
FROM table1
)
SELECT *
FROM q
WHERE rn = 1
or this:
SELECT q.*
FROM (
SELECT DISTINCT field2
FROM table1
) qo
CROSS APPLY
(
SELECT TOP 1 *
FROM table1 t
WHERE t.field2 = qo.field2
ORDER BY
t.field3 DESC
) q
Depending on the field2 cardinality, the first or the second query can be more efficient.
See this article for more details:
SQL Server: Selecting records holding group-wise maximum