473,503 Members | 1,654 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Subquery in SQLServer

1 New Member
Hi, I have a query and a problem.

The problem is:

SELECT items.item as model,
COUNT(inspection_cells.ticket_id) AS qty,
(SELECT count(inspection_cells.ticket_id) FROM inspection_cells, movements, items WHERE inspection_cells.ticket_id = movements.ticket_id and movements.item_id = items.item_id and items.item = model and inspection_cells.air_time in (0,1) and (movements.movement_date between '01-01-2007' and '01-23-2007')) as qty_cero
FROM movements, items, inspection_cells
WHERE movements.item_id = items.item_id and
movements.ticket_id = inspection_cells.ticket_id and
(movements.movement_date between '01-01-2007' and '01-23-2007')
GROUP BY items.item
ORDER BY items.item

The "alias" model in first query it's the condition in the where in the second
query, but dont send me any error, but the result of qtycero it's not valid.

Any sugerence..???
Jan 25 '07 #1
1 2674
almaz
168 Recognized Expert New Member
In order to optimize query performance I would suggest you to rewrite the query as shown below:
Expand|Select|Wrap|Line Numbers
  1. SELECT  items.item as model, COUNT(inspection_cells.ticket_id) AS qty,
  2.         sum(case when inspection_cells.air_time in (0, 1) then 1
  3.                  else 0
  4.             end) as qty_cero
  5. FROM    movements inner join items
  6.             on movements.item_id = items.item_id inner join inspection_cells
  7.             on movements.ticket_id = inspection_cells.ticket_id
  8. WHERE   movements.movement_date between '01-01-2007'
  9.                                 and     '01-23-2007'
  10. GROUP BY items.item
  11. ORDER BY items.item
  12.  
Nevertheless, if you'll decide to stay with your current version: column aliases cannot be used to reference the column in any clauses except ORDER BY clause. So you'll have to add an alias for items table (like items as items_main) and reference the column by items_main.item.
Jan 25 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
4563
by: lev | last post by:
CREATE TABLE . ( NULL , , (44) ) ID is non-unique. I want to select all IDs where the last entry for that ID is of type 11.
2
34096
by: MarekD | last post by:
Hi. I'm new in SqlServer programing. Is it possible to do something like this ( It is common construction in oracle ) Select X from( select a+1 as X from tab1 ) without creating view...
7
9176
by: Andrew Mayo | last post by:
Here's a really weird one for any SQL Server gurus out there... We have observed (SQL Server 2000) scenarios where a stored procedure which (a) begins a transaction (b) inserts some rows into...
0
563
by: Greg Stark | last post by:
Postgresql 7.4b2 (approximately, compiled out of CVS) When I have a subquery that has a complex subquery as one of the result columns, and then that result column is used multiple times in the...
8
5081
by: Venkata C | last post by:
Hi! Does anyone here know of a way to goad DB2 into converting a correlated subquery to a non-correlated one? Does DB2 ever do such a conversion? We have a query of the form SELECT .. FROM A...
8
19568
by: Andrew McNab | last post by:
Hi folks, I have a problem with an MS Access SQL query which is being used in an Access Report, and am wondering if anyone can help. Basically, my query (shown below) gets some records from a...
8
7847
by: kingskippus | last post by:
I don't know if this is possible, but I haven't been able to find any information. I have two tables, for example: Table 1 (two columns, id and foo) id foo --- ----- 1 foo_a 2 foo_b
5
8010
by: Anne | last post by:
Hello! Here is the statement in question: --STATEMENT A SELECT * FROM dbo.myTable WHERE colX in (SELECT colX FROM dbo.sourceTable) The problem with Statement A is that 'colX' does not exist...
1
4130
by: jcf378 | last post by:
Hi all-- Does anyone have any insight as to how I might create a search form that allows a user to select criteria based on any related table in the whole database. The search form I have now only...
4
8407
by: anniebai | last post by:
Update contacts set Commonsid = (select Commonsid from contacts1 where contactid = contacts.contactid and Commonsid is not null) where Commonsid is null This query gives 'Operation must use an...
0
7198
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7271
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7319
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6979
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4998
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1498
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
373
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.