473,651 Members | 2,485 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(inspectio n_cells.ticket_ id) AS qty,
(SELECT count(inspectio n_cells.ticket_ id) FROM inspection_cell s, movements, items WHERE inspection_cell s.ticket_id = movements.ticke t_id and movements.item_ id = items.item_id and items.item = model and inspection_cell s.air_time in (0,1) and (movements.move ment_date between '01-01-2007' and '01-23-2007')) as qty_cero
FROM movements, items, inspection_cell s
WHERE movements.item_ id = items.item_id and
movements.ticke t_id = inspection_cell s.ticket_id and
(movements.move ment_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 2681
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
4573
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
34108
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 containig subquery ?
7
9209
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 a table (c) re-queries another table using a subquery which references the inserted table (correlated or not)
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 parent query, the subquery is inlined for each one. This means multiple redundant executions of the subquery. I recall there was a way to defeat this optimization involving introducing an extra subquery layer somewhere. But I'm failing to be able...
8
5095
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 WHERE EXISTS (SELECT 'X' FROM B, C WHERE B.COL1 = A.COL1 AND B.COL2 = A.COL2
8
19584
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 couple of tables in my database using INNER JOINS and the WHERE clause to specify the required constraints. However, I also want to read two fields from a *single* record from a table called 'Locations' and then apply one of these field's values...
8
7852
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
8025
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 in 'dbo.sourceTable'. It does, however, certainly exist in 'dbo.myTable'. Breaking the statement down, we have:
1
4153
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 allows me to filter based on variables in a single table. I would like to have a search form where I can select multiple variables (from various linked tables) to filter by, and return results based on this multi-table filter. Allen Browne...
4
8419
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 updatable query' error. I copied this query to SQLServer 2005, it works fine. The table 'Contacts1' is copied table of 'Contacts', except that Contacts1 table has values in CommonsID field, while in 'Contacts', the values are null. Contactid is the...
0
8275
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8802
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8697
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8465
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8579
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6158
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5612
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2699
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 we have to send another system
2
1587
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.