473,396 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Select Statement.

7
Hello,

Please can someone help me with this select statement? How do I get the statement to keep the week number static? ie if there are no values then it should show up as NULL, zero or just stay blank.

This is the statment :--

Select a.[Weeknum], b.[No Severity], c.[Severity 3], d.[Severity 2], e.[Severity 1]
From (SELECT Weeknum
FROM [BE].[dbo].[BES$]
Group by Weeknum)
A,
(SELECT Weeknum, Count (*) AS [No Severity]
FROM [BE].[dbo].[BES$]
Where Severity = 'No Severity'
And Team = 'AS400'
Group by Weeknum)
b,
(SELECT Weeknum, Count (*) AS [Severity 3]
FROM [BE].[dbo].[BES$]
Where Severity = '3'
And Team = 'AS400'
Group by Weeknum)
c,
(SELECT Weeknum, Count (*) AS [Severity 2]
FROM [BE].[dbo].[BES$]
Where Severity = '2'
And Team = 'AS400'
Group by Weeknum)
d,
(SELECT Weeknum, Count (*) AS [Severity 1]
FROM [BE].[dbo].[BES$]
Where Severity = '1'
And Team = 'AS400'
Group by Weeknum)
e
Where a.[Weeknum]= b.[Weeknum]
and a.[Weeknum]= c.[Weeknum]
and a.[Weeknum]= d.[Weeknum]
and a.[Weeknum]= e.[Weeknum]

Order by weeknum

The result

32-2006 88 29 2 1
37-2006 77 30 6 1

See the result if I change the last column to be the same as column 4 - (to help demonstrate the problem)

31-2006 65 17 5 5
32-2006 88 29 2 2
33-2006 84 27 3 3
34-2006 66 19 6 6
35-2006 90 19 4 4
36-2006 86 17 3 3
37-2006 77 30 6 6
38-2006 89 26 9 9
39-2006 96 20 3 3

The desired result

31-2006 65 17 5 0
32-2006 88 29 2 1
33-2006 84 27 3 0
34-2006 66 19 6 0
35-2006 90 19 4 0
36-2006 86 17 3 0
37-2006 77 30 6 1
38-2006 89 26 9 0
39-2006 96 20 3 0
Oct 2 '06 #1
2 3158
You have to use LEFT JOIN instead of WHERE:
Expand|Select|Wrap|Line Numbers
  1. Select a.[Weeknum], b.[No Severity], c.[Severity 3], d.[Severity 2], e.[Severity 1]
  2. From (SELECT Weeknum
  3. FROM [BES$]
  4. Group by Weeknum)
  5. A
  6. LEFT JOIN
  7. (SELECT Weeknum, Count (*) AS [No Severity]
  8. FROM [BES$]
  9. Where Severity = 'No Severity'
  10. And Team = 'AS400'
  11. Group by Weeknum)
  12. b ON a.Weeknum = b.Weeknum
  13. LEFT JOIN
  14. (SELECT Weeknum, Count (*) AS [Severity 3]
  15. FROM [BES$]
  16. Where Severity = '3'
  17. And Team = 'AS400'
  18. Group by Weeknum)
  19. c ON a.Weeknum = c.Weeknum
  20. LEFT JOIN
  21. (SELECT Weeknum, Count (*) AS [Severity 2]
  22. FROM [BES$]
  23. Where Severity = '2'
  24. And Team = 'AS400'
  25. Group by Weeknum)
  26. d  ON a.Weeknum = d.Weeknum
  27. LEFT JOIN
  28. (SELECT Weeknum, Count (*) AS [Severity 1]
  29. FROM [BES$]
  30. Where Severity = '1'
  31. And Team = 'AS400'
  32. Group by Weeknum)
  33. e ON  a.Weeknum = e.Weeknum
  34. Order by a.weeknum
  35.  
This will produce the desired result showing NULL. If you want zeros instead of NULL, replace the first line with:
Expand|Select|Wrap|Line Numbers
  1. Select a.[Weeknum], ISNULL(b.[No Severity], 0) AS [No Severity], ISNULL(c.[Severity 3], 0) AS [Severity 3], ISNULL(d.[Severity 2], 0) AS [Severity 2], ISNULL(e.[Severity 1], 0) AS [Severity 1]
  2.  
Oct 2 '06 #2
bhanab
7
Thank you so much - Galexyus. It works 100%
Oct 3 '06 #3

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

Similar topics

21
by: John Fabiani | last post by:
Hi, I'm a newbie and I'm attempting to learn howto create a select statement. When I use >>> string1='18 Tadlock Place' >>> cursor.execute("SELECT * FROM mytest where address = %s",string1) All...
0
by: Jan | last post by:
I store sql-commands in a database table. In the first step I get the sql command out of the database table with embedded sql. In the second step I try to execute the command, which i got from the...
3
by: dumbledad | last post by:
Hi All, I'm confused by how to replace a SELECT statement in a SQL statement with a specific value. The table I'm working on is a list of words (a column called "word") with an index int...
4
by: 001 | last post by:
Hello, The select statement needs only 1 second to complete the query. But the update statement spends 30 minutes. Why? SELECT STATEMENT: declare @IDate smalldatetime select @IDate=col001...
3
by: Tcs | last post by:
My backend is DB2 on our AS/400. While I do HAVE DB2 PE for my PC, I haven't loaded it yet. I'm still using MS Access. And no, I don't believe this is an Access question. (But who knows? I...
1
by: Grant McLean | last post by:
Hi First a simple question ... I have a table "access_log" that has foreign keys "app_id" and "app_user_id" that reference the "application_type" and "app_user" tables. When I insert into...
6
by: Bob Stearns | last post by:
I am getting duplicate rows back from a select distinct statement of the form: SELECT DISTINCT 'jhough', '000111', t0.bhid FROM (SELECT lots of good stuff) t0 LEFT OUTER JOIN another_table ...
19
by: Steve | last post by:
ASP error number 13 - Type mismatch with SELECT...FOR UPDATE statement I got ASP error number 13 when I use the SELECT...FOR UPDATE statement as below. However, if I use SELECT statement without...
1
by: microsoft.public.dotnet.languages.vb | last post by:
Hi All, I wanted to know whether this is possible to use multiple variables to use in the select case statement such as follows: select case dWarrExpDateMonth, dRetailDateMonth case...
0
FishVal
by: FishVal | last post by:
Hereby I'm proposing a way of convinient work with properties containing SQL Select statements, particulary RowSource property of ComboBox and ListBox. The usual way is the following. Private...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.