Ask a Question related to Microsoft SQL / MS SQL Server, Design and Development.
-
Mike #1
Query problem.
column 1 column2 column 3 column 4
1 current 1.0 yr1
1 previous 1.2 yr0
2 current 1.3 yr1
2 previous 2.0 yr0
3 previous 1.4 yr0
4. current 2.0 yr1
4. previous 1.5 yr0
.................................................
I want to use a query to return the following, basically, if there is no
"current", then it shouldn't return "previous" .
is it possible?
column 1 column2 column 3 column 4
1 current 1.0 yr1
1 previous 1.2 yr0
2 current 1.3 yr1
2 previous 2.0 yr0
4. current 2.0 yr1
4. previous 1.5 yr0
Mike Guest
-
Query problem, please help.
mysql Ver 12.22 Distrib 4.0.24, for pc-linux-gnu (i386) gives me: The following database Error occured: You have an error in your SQL syntax.... -
***Sql Query problem
Randy Webb wrote: SELECT DISTINCT OrderID FROM trans WHERE Trans_ID = 1 AND OrderID NOT IN (SELECT DISTINCT OrderID FROM trans WHERE Trans_ID =... -
Query of Query problem
Error Executing Database Query. Query Of Queries runtime error. Table named "DATA" was not found in Memory. It is misspelled, or the table is... -
Query on Query and CF casting problem
I am using a custom tag in MX7 that was working fine in 5 that renders a table. The input to the custom tag is a query and it's columns along with... -
query problem
SELECT top5.PRODID, p.PRODNAME FROM ( SELECT TOP 5 SALES = COUNT(*), PRODID FROM SALES GROUP BY PRODID ORDER BY SALES DESC ) AS top5 INNER JOIN... -
Vishal Parkar #2
Re: Query problem.
try this, should work (untested though)
select a.* from table a inner join
(select column1 from table where column2 in('current', 'previous')
group by column1 having count(*) >= 2) b
on a.column1= b.column1
--
-Vishal
"Mike" <pearl_77@hotmail.com> wrote in message
news:#tTy7tyRDHA.940@TK2MSFTNGP11.phx.gbl...> column 1 column2 column 3 column 4
>
> 1 current 1.0 yr1
> 1 previous 1.2 yr0
> 2 current 1.3 yr1
> 2 previous 2.0 yr0
> 3 previous 1.4 yr0
> 4. current 2.0 yr1
> 4. previous 1.5 yr0
> ................................................
>
>
> I want to use a query to return the following, basically, if there is no
> "current", then it shouldn't return "previous" .
> is it possible?
>
>
> column 1 column2 column 3 column 4
>
> 1 current 1.0 yr1
> 1 previous 1.2 yr0
> 2 current 1.3 yr1
> 2 previous 2.0 yr0
> 4. current 2.0 yr1
> 4. previous 1.5 yr0
>
>
Vishal Parkar Guest
-
Anith Sen #3
Re: Query problem.
You can do this in many ways, using self joins, derived tables or even with
subqueries like:
SELECT *
FROM tbl
WHERE EXISTS( SELECT *
FROM tbl t1
INNER JOIN tbl t2
ON t1.col1 = t2.col1
AND t1.col1 = tbl.col1
WHERE t1.col2 = 'current'
AND t2.col2 = 'previous' ) ;
--
- Anith
( Please reply to newsgroups only )
Anith Sen Guest
-
Mike #4
Re: Query problem.
okay... but how about this,
if there is current but no previous, then we should not eliminate
current.
is that possible?
"Anith Sen" <anith@bizdatasolutions.com> wrote in message
news:uUs$U0yRDHA.1624@tk2msftngp13.phx.gbl...with> You can do this in many ways, using self joins, derived tables or even> subqueries like:
>
> SELECT *
> FROM tbl
> WHERE EXISTS( SELECT *
> FROM tbl t1
> INNER JOIN tbl t2
> ON t1.col1 = t2.col1
> AND t1.col1 = tbl.col1
> WHERE t1.col2 = 'current'
> AND t2.col2 = 'previous' ) ;
>
> --
> - Anith
> ( Please reply to newsgroups only )
>
>
Mike Guest
-
Anith Sen #5
Re: Query problem.
Just remove the last condition in the WHERE clause like:
SELECT *
FROM tbl
WHERE EXISTS( SELECT *
FROM tbl t1
INNER JOIN tbl t2
ON t1.col1 = t2.col1
AND t1.col1 = tbl.col1
WHERE t1.col2 = 'current' ) ;
--
- Anith
( Please reply to newsgroups only )
Anith Sen Guest



Reply With Quote

