Solved

How to write this query with where condition?

Posted on 2009-05-18
13
215 Views
Last Modified: 2012-05-07
Hi

The following SQL cannot select the result, it did not display any data.

The data are:
66      0      0      20      ICBC    //where transactiontypeid = 20
87      0      66      3      BOC    //where soldstockid = 66
85      0      66      4      BCC
84      0      66      4      ICBC

soldstockid = 66 = id in first column.
2nd row is alltranid = 0.
3rd column is soldstockid.
4th column is transactiontypeid.

transactiontypeid=20 and soldstockid>0 is wrong but i do not know how to write it to show the above 4 rows of data.

select [id]=t.id, [transactiontypeid]=transactiontypeid, [type]=r.type, [clientid]=t.clientid, [nickname]=c.nickname, 
[currency]=t.currency, [ucurrency]=u.currency, [rate], [amount], [countervalue], [trandatetime], [status] 
from alltransaction t inner join client c on t.clientid = c.id inner join currency u on t.currency = u.currency 
inner join transactiontype r on t.transactiontypeid = r.id  
where alltranid = 0 and transactiontypeid=20 and soldstockid>0 and t.clientid = '12' order by [id] asc

Open in new window

0
Comment
Question by:techques
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
  • 3
13 Comments
 
LVL 20

Expert Comment

by:ddayx10
ID: 24419181
Either your trying to relate data in a really complicated way, or you are vastly overcomplicating this:

If the former...good luck! If the latter, try this. Some of the stuff you had written there just wont work!

select
[id],
[transactiontypeid],
[type],
[clientid],
[nickname],
[currency],
[ucurrency],
[rate],
[amount],
[countervalue],
[trandatetime],
[status]
from [NAMEOFYOURTABLE]
where alltranid = 0 and transactiontypeid=20 and soldstockid>0 and clientid = '12'
order by [id] asc
0
 

Author Comment

by:techques
ID: 24419204
It shows nothing, your SQL did not change much.
select 
[id], 
[transactiontypeid], 
[clientid], 
[currency], 
[rate], 
[amount], 
[countervalue], 
[trandatetime], 
[status] 
from alltransaction 
where alltranid = 0 and transactiontypeid=20 and soldstockid>0 and clientid = '12' 
order by [id] asc

Open in new window

0
 
LVL 20

Expert Comment

by:ddayx10
ID: 24419383
Didn't change much? Are you kidding me (that is not a question)!

If it's not giving an error, but not giving any records then...there are no records matching that criteria!
66      0      0      20      ICBC    
87      0      66      3      BOC    
85      0      66      4      BCC
84      0      66      4      ICBC
1st Column = id or clientid    --ignore for now
2nd Comun is alltranid  (=0) --this leaves all rows
3rd column is soldstockid. (>0) --this leaves you with the last 3 rows
4th column is transactiontypeid (=20) --this leaves you with 0 rows as there is no transactiontypeid =20 in bottom 3 rows.

You show 5 columns but you only defined 4 of them. ClientID is not one of the columns you mention although it is in the WHERE clause. ID is what you are specifying in the SELECT portion?

All that aside. With just the data you showed. If I only evaluate the columns with data you defined. There are no results. If there is more data then possibly there should be some rows. I can only go on what you gave me to work with.

dday
0
Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

 
LVL 20

Expert Comment

by:ddayx10
ID: 24419478
Look I'm sorry if I was testy above. I'm probably too tired to be doing this right now. Good luck with your sql statement. Have a nice day :)

dday
0
 
LVL 6

Expert Comment

by:FVER
ID: 24419488
I believe the query below should be close from what you need.
select t.id,
       t.transactiontypeid,
       r.type,
       t.clientid,
       c.nickname,
       t.currency,
       u.currency, --what's the point of this column ? It is identical to the previous one due to join condition between u and t
       [rate],
       [amount],
       [countervalue],
       [trandatetime],
       [status]
  from alltransaction t
       inner join (select id
                     from alltransaction
                    where alltranid = 0
                      and soldstockid>0
                      and transactiontypeid=20
                      and t.clientid = '12') m on (t.id=m.id OR t.soldstockid=m.id)
       left join client c on t.clientid = c.id 
       left join currency u on t.currency = u.currency 
       left join transactiontype r on t.transactiontypeid = r.id
 where t.alltranid = 0
   and t.soldstockid>0
   and t.clientid = '12'
 order by [id] asc

Open in new window

0
 

Author Comment

by:techques
ID: 24419798
Hi FVER

It has error:

The multi-part identifier "t.clientid" could not be bound.


--what's the point of this column ? It is identical to the previous one due to join condition between u and t

YES

0
 

Author Comment

by:techques
ID: 24419827
Hi

I changed your SQL to the following SQL. But, it still shows nothing.
select t.id,
       t.transactiontypeid,
       r.type,
       t.clientid,
       c.nickname,
       t.currency,
       u.currency, 
       [rate],
       [amount],
       [countervalue],
       [trandatetime],
       [status]
  from alltransaction t
       inner join (select id
                     from alltransaction t
                    where t.alltranid = 0
                      and t.soldstockid>0
                      and t.transactiontypeid=20
                      and t.clientid = '12') m on (t.id=m.id OR t.soldstockid=m.id)
       left join client c on t.clientid = c.id 
       left join currency u on t.currency = u.currency 
       left join transactiontype r on t.transactiontypeid = r.id
 where t.alltranid = 0
   and t.soldstockid>0
   and t.clientid = '12'
 order by [id] asc

Open in new window

0
 

Author Comment

by:techques
ID: 24419857
I found that because

select id from alltransaction t where t.alltranid = 0 and t.soldstockid>0 and t.transactiontypeid=20 and t.clientid = '12'

returns null for the id
0
 

Author Comment

by:techques
ID: 24419876
The following SQL can display 3 rows of records:

84      0      66      4      ICBC
85      0      66      4      BCC
87      0      66      3      BOC  

But,    66      0      0      20      ICBC    did not show
select t.id,
       t.transactiontypeid,
       r.type,
       t.clientid,
       c.nickname,
       t.currency,
       u.currency, 
       [rate],
       [amount],
       [countervalue],
       [trandatetime],
       [status]
  from alltransaction t
       inner join (select id
                     from alltransaction e
                     where e.alltranid = 0
                      and e.transactiontypeid=20
                      and e.clientid = '12') m on (t.id=m.id OR t.soldstockid=m.id)
       left join client c on t.clientid = c.id 
       left join currency u on t.currency = u.currency 
       left join transactiontype r on t.transactiontypeid = r.id
 where t.alltranid = 0
   and t.soldstockid>0
   and t.clientid = '12'
 order by [id] asc

Open in new window

0
 
LVL 6

Expert Comment

by:FVER
ID: 24419893
please remove the "t.' in the subquery
select t.id,
       t.transactiontypeid,
       r.type,
       t.clientid,
       c.nickname,
       t.currency,
       u.currency, --what's the point of this column ? It is identical to the previous one due to join condition between u and t
       [rate],
       [amount],
       [countervalue],
       [trandatetime],
       [status]
  from alltransaction t
       inner join (select id
                     from alltransaction
                    where alltranid = 0
                      and soldstockid>0
                      and transactiontypeid=20
                      and clientid = '12') m on (t.id=m.id OR t.soldstockid=m.id)
       left join client c on t.clientid = c.id 
       left join currency u on t.currency = u.currency 
       left join transactiontype r on t.transactiontypeid = r.id
 where t.alltranid = 0
   and t.soldstockid>0
   and t.clientid = '12'
 order by [id] asc

Open in new window

0
 

Author Comment

by:techques
ID: 24419902
after removing 't' from subquery, it shows nothing.

0
 
LVL 6

Accepted Solution

by:
FVER earned 50 total points
ID: 24419905
sorry, I was a little late.
this one should solve the issue

select t.id,
       t.transactiontypeid,
       r.type,
       t.clientid,
       c.nickname,
       t.currency,
       u.currency, 
       [rate],
       [amount],
       [countervalue],
       [trandatetime],
       [status]
  from alltransaction t
       inner join (select id
                     from alltransaction e
                     where e.alltranid = 0
                      and e.transactiontypeid=20
                      and e.clientid = '12') m on (t.id=m.id OR t.soldstockid=m.id)
       left join client c on t.clientid = c.id 
       left join currency u on t.currency = u.currency 
       left join transactiontype r on t.transactiontypeid = r.id
 where t.alltranid = 0
   and t.clientid = '12'
 order by [id] asc

Open in new window

0
 

Author Closing Comment

by:techques
ID: 31582886
Excellent! Thanks for help
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Help with SQL - TOP 10 by date and by group 13 42
Query group by data in SQL Server - cursor? 3 66
Parse this column 6 35
Need help in debugging a UDF results 7 55
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question