473,774 Members | 2,252 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Select Statement.

7 New Member
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 3179
galexyus
15 New Member
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 New Member
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
5260
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 works as expected. But >>> numb=10 >>> cursor.execute("SELECT * FROM mytest where clientID = %d",numb) Traceback (innermost last): File "<stdin>", line 1, in ?
0
10212
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 database table, using dynamic sql. Executing 'EXEC SQL DESCRIBE SELECT LIST FOR S INTO select_dp;' the error code -2149 is returned That means "Specified partition does not exist". Does anybody know if it is a database problem or a problem of
3
5728
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 pointing to the sentence they come from (a column called "regret"). I also have a table of stop words (called "GenericStopWords") that contains the words I do not want to consider. That table has a single column called "word". I started off using a...
4
2255
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 from USDay select * from USDay A
3
6472
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 COULD be wrong... :) I've tried the access group...twice...and all I get is "Access doesn't like ".", which I know, or that my query names are too long, as there's a limit to the length of the SQL statement(s). But this works when I don't try to...
1
3689
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 "access_log", the referential integrity triggers generate these queries: SELECT 1 FROM ONLY "public"."application_type" x
6
13764
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 t1 ON relevant_stuff WHERE (lots of conditions) After re-reading the relevant pat ofVol 1 of the SQL Reference I am unablee to see how this is possible.
19
8384
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 FOR UPDATE, it is fine and no error. I also tried Set objRs = objConn.Execute("SELECT * FROM EMP UPDATE OF EMPNO"), but it still couldn't help. any ideas? I tried to search in the web but couldn't find similar
1
21687
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 "01" : dWarrExpDateMonth="Jan" : dRetailDateMonth="Jan" case "02" : dWarrExpDateMonth="Feb" : dRetailDateMonth="Feb" End Select
0
5630
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 Sub Combo1_AfterUpdate() Me.Combo2.RowSource = "SELECT ... FROM ... WHERE Table2.keyID=" & Me.Combo1 & ";" Combo2.Requery
0
9621
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
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
10267
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
10106
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...
0
6717
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();...
0
5355
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3611
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2852
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.