I want to query zero returns for null or nonexistent values, in the examples below, see what was the purpose and what was the output I received
SELECT
count(case when coalesce("CUSTOMER",'') = '' then 1 else 0 end) AS "OFFLINE GENERAL"
FROM
tbl_status
WHERE
"AVAILABLE" = '*OFFLINE'
AND "CUSTOMER" IN (
'CLIENT1',
'CLIENT2',
'CLIENT3',
'CLIENT4',
'CLIENT5'
)
GROUP BY
"CUSTOMER"
RETURNED
------
1 | 1
------
2 | 1
------
GOAL
1 | 1
-----
2 | 1
-----
3 | 0
-----
4 | 0
-----
5 | 0
...
Try a CTE with an outer join:
WITH cust AS (
SELECT *
FROM (VALUES
('CLIENT1'),
('CLIENT2'),
('CLIENT3'),
('CLIENT4'),
('CLIENT5')) AS cust(c)
)
SELECT cust.c,
count(*)
FILTER (WHERE coalesce("CUSTOMER",'') = '')
AS "OFFLINE GENERAL"
FROM tbl_status
RIGHT JOIN cust
ON cust.c = tbl_status."CUSTOMER"
WHERE
"AVAILABLE" = '*OFFLINE'
GROUP BY cust.c;
I have a question
state_id state_name city_id city_name
-------- ---------- ------- -----------
1 Karnataka 1 Bangalore
1 Karnataka 2 Mysore
1 Karnataka 3 Hubli
2 Maharastra 1 abc
2 Maharastra 2 xyz
i should require output as below
state_id state_name city_id city_name
-------- ---------- ------- -----------
1 Karnataka 1 Bangalore
1 null 2 Mysore
1 null 3 Hubli
2 Maharastra 1 abc
2 null 2 xyz
Kindly help me out of this
As per above reference,
I have solved this,
Below is the script
SET #rn = 0;
SET #sid = 0;
SELECT state_id,city_id,city_name,#sid1:=#sid,#sid:=state_id,IF(IF(#sid1=#sid,#rn:=#rn+1,#rn:=1) = 1,state_name,NULL) AS state_name FROM t2;
state_id city_id city_name #sid1:=#sid #sid:=state_id state_name
1 1 Bangalore 0 1 Karnataka
1 2 Mysore 1 1 (NULL)
1 3 Hubli 1 1 (NULL)
2 1 abc 1 2 Maharastra
2 2 xyz 2 2 (NULL)
Thanks for the hint given
I have searched high and low for weeks now trying to find a solution to my problem.
As far as I can ascertain, my SQL Server version (2008r2) is a limiting factor on this but, I am positive there is a solution out there.
My problem is as follows:
A have a table with potential contiguous dates in the form of Customer-Status-DateStart-DateEnd-EventID.
I need to merge contiguous dates by customer and status - the status field can shift up and down throughout a customers pathway.
Some example data is as follows:
DECLARE #Tbl TABLE([CustomerID] INT
,[Status] INT
,[DateStart] DATE
,[DateEnd] DATE
,[EventID] INT)
INSERT INTO #Tbl
VALUES (1,1,'20160101','20160104',1)
,(1,1,'20160104','20160108',3)
,(1,2,'20160108','20160110',4)
,(1,1,'20160110','20160113',7)
,(1,3,'20160113','20160113',9)
,(1,3,'20160113',NULL,10)
,(2,1,'20160101',NULL,2)
,(3,2,'20160109','20160110',5)
,(3,1,'20160110','20160112',6)
,(3,1,'20160112','20160114',8)
Desired output:
Customer | Status | DateStart | DateEnd
---------+--------+-----------+-----------
1 | 1 | 2016-01-01| 2016-01-08
1 | 2 | 2016-01-08| 2016-01-10
1 | 1 | 2016-01-10| 2016-01-13
1 | 3 | 2016-01-13| NULL
2 | 1 | 2016-01-01| NULL
3 | 2 | 2016-01-09| 2016-01-10
3 | 1 | 2016-01-10| 2016-01-14
Any ideas / code will be greatly received.
Thanks,
Dan
Try this
DECLARE #Tbl TABLE([CusomerID] INT
,[Status] INT
,[DateStart] DATE
,[DateEnd] DATE
,[EventID] INT)
INSERT INTO #Tbl
VALUES (1,1,'20160101','20160104',1)
,(1,1,'20160104','20160108',3)
,(1,2,'20160108','20160110',4)
,(1,1,'20160110','20160113',7)
,(1,3,'20160113','20160113',9)
,(1,3,'20160113',NULL,10)
,(2,1,'20160101',NULL,2)
,(3,2,'20160109','20160110',5)
,(3,1,'20160110','20160112',6)
,(3,1,'20160112','20160114',8)
;WITH CTE
AS
(
SELECT CusomerID ,
Status ,
DateStart ,
COALESCE(DateEnd, '9999-01-01') AS DateEnd,
EventID,
ROW_NUMBER() OVER (ORDER BY CusomerID, EventID) RowId,
ROW_NUMBER() OVER (PARTITION BY CusomerID, Status ORDER BY EventID) StatusRowId FROM #Tbl
)
SELECT
A.CusomerID ,
A.Status ,
A.DateStart ,
CASE WHEN A.DateEnd = '9999-01-01' THEN NULL
ELSE A.DateEnd END AS DateEnd
FROM
(
SELECT
CTE.CusomerID,
CTE.Status,
MIN(CTE.DateStart) AS DateStart,
MAX(CTE.DateEnd) AS DateEnd
FROM
CTE
GROUP BY
CTE.CusomerID,
CTE.Status,
CTE.StatusRowId -CTE.RowId
) A
ORDER BY A.CusomerID, A.DateStart
Output
CusomerID Status DateStart DateEnd
----------- ----------- ---------- ----------
1 1 2016-01-01 2016-01-08
1 2 2016-01-08 2016-01-10
1 1 2016-01-10 2016-01-13
1 3 2016-01-13 NULL
2 1 2016-01-01 NULL
3 2 2016-01-09 2016-01-10
3 1 2016-01-10 2016-01-14
I have the following table test
iD Name ParentId GroupID
-----------------------------
1 1 Null
2 1 Null
3 1 Null
4 7 Null
5 7 Null
6 7 Null
7 9 Null
How can I modify it to get the column GroupID like this:
iD Name ParentId GroupID
------------------------------
1 1 1
2 1 1
3 1 1
4 7 2
5 7 2
6 7 2
7 9 3
; WITH CTE
AS
(
SELECT iDName, ParentId, GroupID,
DENSE_RANK() OVER (ORDER BY ParentId ASC) RN
FROM Test
)
UPDATE CTE
SET GroupID = RN
Working SQL FIDDLE
UPDATE test SET GroupID = 1 WHERE ParentId = 1
UPDATE test SET GroupID = 2 WHERE ParentId = 7
UPDATE test SET GroupID = 3 WHERE ParentId = 9
I know this must be a common problem, but I'm not sure where to go. This occurs a few times inside a big nasty query, so here is the problem simplified. I just want one row per object, but the null on ColorOther gives me two rows.
I can think of a few things to try wrapping a query around this, but there must be something simpler... I'm off to read up on pivots.
Table structure
CREATE TABLE [dbo].[Colors](
[ColorId] [int] IDENTITY(1,1) NOT NULL,
[ColoredObjectId] [int] NOT NULL,
[ColorCode] [int] NOT NULL,
[ColorOther] [nvarchar](50) NULL,
CONSTRAINT [PK_Colors] PRIMARY KEY CLUSTERED
([ColorId] ASC)
Source data
ColorId ColoredObjectId ColorCode ColorOther
----------- --------------- ----------- ----------
1 1 1 NULL
2 1 2 NULL
3 1 4 purple
4 2 2 NULL
5 2 3 NULL
6 2 4 orange
7 3 1 NULL
8 3 3 NULL
9 3 4 green
Query
SELECT ColoredObjectId ,
ColorOther ,
MAX(CASE WHEN ColorCode = 1 THEN 1 ELSE 0 END) AS Yellow,
MAX(CASE WHEN ColorCode = 2 THEN 1 ELSE 0 END) AS Red ,
MAX(CASE WHEN ColorCode = 3 THEN 1 ELSE 0 END) AS Blue ,
MAX(CASE WHEN ColorCode = 4 THEN 1 ELSE 0 END) AS Other
FROM Colors
GROUP BY ColoredObjectId,
ColorOther
Output
ColoredObjectId ColorOther Yellow Red Blue Other
--------------- ---------- ----------- ----------- ----------- -----------
1 NULL 1 1 0 0
1 purple 0 0 0 1
2 NULL 0 1 1 0
2 orange 0 0 0 1
3 NULL 1 0 1 0
3 green 0 0 0 1
Desired output
ColoredObjectId ColorOther Yellow Red Blue Other
--------------- ---------- ----------- ----------- ----------- -----------
1 purple 1 1 0 1
2 orange 0 1 1 1
3 green 1 0 1 1
I think this should return the desired output.
SELECT ColoredObjectId ,
MAX(ColorOther) AS ColorOther ,
MAX(CASE WHEN ColorCode = 1 THEN 1 ELSE 0 END) AS Yellow,
MAX(CASE WHEN ColorCode = 2 THEN 1 ELSE 0 END) AS Red ,
MAX(CASE WHEN ColorCode = 3 THEN 1 ELSE 0 END) AS Blue ,
MAX(CASE WHEN ColorCode = 4 THEN 1 ELSE 0 END) AS Other
FROM Colors
GROUP BY ColoredObjectId
Try this (I am not at a SQL box, so I can't test it, but it should be pretty close)
SELECT cc.ColoredObjectId ,
xx.ColorOther ,
MAX(CASE WHEN ColorCode = 1 THEN 1 ELSE 0 END) AS Yellow,
MAX(CASE WHEN ColorCode = 2 THEN 1 ELSE 0 END) AS Red ,
MAX(CASE WHEN ColorCode = 3 THEN 1 ELSE 0 END) AS Blue ,
MAX(CASE WHEN ColorCode = 4 THEN 1 ELSE 0 END) AS Other
FROM Colors cc
JOIN (select distinct ColorObjectID,ColorOther FROM colors
where colorOther is not null) xx on xx.colorObjectId=cc.colorobjectID
GROUP BY cc.ColoredObjectId,
xx.ColorOther