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
2 3158
You have to use LEFT JOIN instead of WHERE: -
Select a.[Weeknum], b.[No Severity], c.[Severity 3], d.[Severity 2], e.[Severity 1]
-
From (SELECT Weeknum
-
FROM [BES$]
-
Group by Weeknum)
-
A
-
LEFT JOIN
-
(SELECT Weeknum, Count (*) AS [No Severity]
-
FROM [BES$]
-
Where Severity = 'No Severity'
-
And Team = 'AS400'
-
Group by Weeknum)
-
b ON a.Weeknum = b.Weeknum
-
LEFT JOIN
-
(SELECT Weeknum, Count (*) AS [Severity 3]
-
FROM [BES$]
-
Where Severity = '3'
-
And Team = 'AS400'
-
Group by Weeknum)
-
c ON a.Weeknum = c.Weeknum
-
LEFT JOIN
-
(SELECT Weeknum, Count (*) AS [Severity 2]
-
FROM [BES$]
-
Where Severity = '2'
-
And Team = 'AS400'
-
Group by Weeknum)
-
d ON a.Weeknum = d.Weeknum
-
LEFT JOIN
-
(SELECT Weeknum, Count (*) AS [Severity 1]
-
FROM [BES$]
-
Where Severity = '1'
-
And Team = 'AS400'
-
Group by Weeknum)
-
e ON a.Weeknum = e.Weeknum
-
Order by a.weeknum
-
This will produce the desired result showing NULL. If you want zeros instead of NULL, replace the first line with: -
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]
-
Thank you so much - Galexyus. It works 100%
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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 ...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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,...
| |