Im trying to build by day report at the moment the code im using works but nnot to what i need it to do.
SELECT Notices.Promoter,
Sum(IIf([Notices].[Type]='GRANT PERMIT' Or [Notices].[Type]='GRANT VARIATION' Or [Notices].[Notice Type]='GRANT PAA',1,0)) AS Granted,
Sum(IIf([Notices].[Type]='REFUSE APPLICATION',1,0)) AS Refused,
Sum(IIf([Notices].[Status]='Deemed',1,0)) AS Deemed,
Sum(IIf([Notices].[Error]<>"" And Notices.[Category]<>"Observation" And Notices.[Category]<>"S.74 Overrun",1,0)) AS [Potential Penalty]
FROM Notices
WHERE (((Notices.[Day Of Week])=[TempVars]![DayReport]))
GROUP BY Notices.Promoter;
the tempVar [DayReport] is the day of the week mon,tue,wed.... sun
and the code outputs this
Promoter | Granted | Refused | Deemed | Potential Penalty
Name | 0 | 0 | 0 | 0
Name2 | 3 | 0 | 0 | 0
Name3 | 4 | 2 | 1 | 0
Name4 | 0 | 1 | 1 | 0
Name5 | 1 | 0 | 0 | 0
What i want is it to not show the Promoter that has all 0's in the fields like this
Promoter | Granted | Refused | Deemed | Potential Penalty
Name2 | 3 | 0 | 0 | 0
Name3 | 4 | 2 | 1 | 0
Name4 | 0 | 1 | 1 | 0
Name5 | 1 | 0 | 0 | 0
As im not sure about how to go about this i thought i would put it to the wonderful people of Stackoverflow
SELECT *
FROM
(
SELECT Notices.Promoter,
Sum(IIf([Notices].[Type]='GRANT PERMIT' Or [Notices].[Type]='GRANT VARIATION' Or [Notices].[Notice Type]='GRANT PAA',1,0)) AS Granted,
Sum(IIf([Notices].[Type]='REFUSE APPLICATION',1,0)) AS Refused,
Sum(IIf([Notices].[Status]='Deemed',1,0)) AS Deemed,
Sum(IIf([Notices].[Error]<>"" And Notices.[Category]<>"Observation" And Notices.[Category]<>"S.74 Overrun",1,0)) AS [Potential Penalty]
FROM Notices
WHERE (((Notices.[Day Of Week])=[TempVars]![DayReport]))
GROUP BY Notices.Promoter
) s
WHERE Promoter <> 0
Refused <> 0 AND
Deemed <> 0 AND
[Potential Penalty] <> 0
UPDATE 1
SELECT Notices.Promoter,
Sum(IIf([Notices].[Type]='GRANT PERMIT' Or [Notices].[Type]='GRANT VARIATION' Or [Notices].[Notice Type]='GRANT PAA',1,0)) AS Granted,
Sum(IIf([Notices].[Type]='REFUSE APPLICATION',1,0)) AS Refused,
Sum(IIf([Notices].[Status]='Deemed',1,0)) AS Deemed,
Sum(IIf([Notices].[Error]<>"" And Notices.[Category]<>"Observation" And Notices.[Category]<>"S.74 Overrun",1,0)) AS [Potential Penalty]
FROM Notices
WHERE (((Notices.[Day Of Week])=[TempVars]![DayReport]))
GROUP BY Notices.Promoter
HAVING Sum(IIf([Notices].[Type]='GRANT PERMIT' Or [Notices].[Type]='GRANT VARIATION' Or [Notices].[Notice Type]='GRANT PAA',1,0)) <> 0 AND
Sum(IIf([Notices].[Type]='REFUSE APPLICATION',1,0)) <> 0 AND
Sum(IIf([Notices].[Status]='Deemed',1,0)) <> 0 AND
Sum(IIf([Notices].[Error]<>"" And Notices.[Category]<>"Observation" And Notices.[Category]<>"S.74 Overrun",1,0)) <> 0
Related
I want to flag only the first duplicate ID-VL combination in the dataset shown below. Column FirstOccurence is what I want the end result to be.
ID VL FirstOccurence
1 a 1
1 b 1
2 a 1
2 a 0
3 a 1
3 a 0
4 a 1
4 a 0
5 a 1
5 b 1
5 a 0
There is currently not a unique index available in the original table.
Is there any way to do this with for instance the LAG-functionality? I cannot find any examples online that result in the flagging of duplicates. Any suggestions are much appreciated!
Kind regards,
Igor
One method is with ROW_NUMBER() along with a CASE expression:
SELECT
ID
,VL
,CASE ROW_NUMBER() OVER(PARTITION BY ID, VL ORDER BY ID, VL) WHEN 1 THEN 1 ELSE 0 END AS FirstOccurance
FROM dbo.example
ORDER BY
ID
,VL
,FirstOccurance;
Results:
+----+----+----------------+
| ID | VL | FirstOccurance |
+----+----+----------------+
| 1 | a | 1 |
| 1 | b | 1 |
| 2 | a | 0 |
| 2 | a | 1 |
| 3 | a | 0 |
| 3 | a | 1 |
| 4 | a | 0 |
| 4 | a | 1 |
| 5 | a | 0 |
| 5 | a | 1 |
| 5 | b | 1 |
+----+----+----------------+
Note that this result order differs from your end result. If there are one or more columns present in the table that provide the same ordering as the results in you question, specify that in the ORDER BY clause instead.
I have a table
/---------------------------------------\
|Region | Product | 1 | 2 | 3 | 4 |
|-------|---------|---|-----|-----|-----|
| A | ABC | 0 | 120 | 421 | 520 |
| B | ABC | 0 | 0 | 0 | 670 |
| C | DEF | 0 | 0 | 0 | 125 |
| D | PQR | 0 | 0 | 780 | 560 |
| E | PQR | 0 | 0 | 0 | 340 |
| F | XYZ | 0 | 0 | 0 | 780 |
| G | XYZ | 0 | 0 | 0 | 900 |
\---------------------------------------/
In this table, I need to find the name of products that were launched in quarter 4.
The result that query should give is DEF and XYZ
I will be grateful if someone could help
You need to group by product and aggregate (sum) the values for each quarter per product, regardless of region:
select
Product
from #table
group by Product
having sum([4]) > 0
and sum([3]) = 0
and sum([2]) = 0
and sum([1]) = 0
With sample data to illustrate:
create table #table
(
Region varchar(1),
Product varchar(3),
[1] int,
[2] int,
[3] int,
[4] int
)
insert into #table
values
('A','ABC',0,120,421,520),
('B','ABC',0,0,0,670),
('C','DEF',0,0,0,125),
('D','PQR',0,0,780,560),
('E','PQR',0,0,0,340),
('F','XYZ',0,0,0,780),
('G','XYZ',0,0,0,900)
select
Product
from #table
group by Product
having sum([4]) > 0
and sum([3]) = 0
and sum([2]) = 0
and sum([1]) = 0
drop table #table
Output:
/---------\
| Product |
|---------|
| DEF |
| XYZ |
\---------/
try this
select *
from yourTableName a
where a.field4 > 0
and a.field3 = 0
and a.field2 = 0
and a.field1 = 0
and a.product not in (select b.product
from yourTableName b
where b.field3 >0
or b.field2>0
or b.field1>0)
and if you just want the product use below
select a.product
from yourTableName a
where a.field4 > 0
and a.field3 = 0
and a.field2 = 0
and a.field1 = 0
and a.product not in (select b.product
from yourTableName b
where b.field3 >0
or b.field2>0
or b.field1>0)
here field4 as quarter 4
field3 as quarter 3 and so on.
I want to transform a Data set of labels to a binary representation via a SQL query, i.e. the following table:
|---------------------------|
| Example | Label |
|---------------------------|
| 1 | Health |
| 1 | Business |
| 1 | Science |
| 2 | Sports |
| 2 | Business |
|---------------------------|
Transforms into a new table:
|---------------------------|-----------|-----------|-----------|
| Example | Business | Health | Science | Sports |
|---------------------------|-----------|-----------|-----------|
| 1 | 1 | 1 | 1 | 0 |
| 2 | 1 | 0 | 0 | 1 |
|-----------|---------------|-----------|-----------|-----------|
via some SQL query. What would be said SQL query?
select example, sum(case when label='Business' then 1 else 0 end) 'Business'
,sum(case when label='Health' then 1 else 0 end) 'Health'
,sum(case when label='Science' then 1 else 0 end) 'Science'
,sum(case when label='Sports' then 1 else 0 end) 'Sports'
From MyTable
group by example
How can I transpose a SQL Server row of data to multiple columns? I have data something like show below
[STOCK NO] [PN1] [PN2] [PN3] [PN4] [PN4] [PN5] [PN6] [PN7] [PN8] [PN9] [PN10] [PN11] [PN12] [Qty]
700349L 600446 201743 100208 300219 400533 100280 100218 100222 100220 100221 1
I would like to see something like this
[STOCK NO] [bomRev] [bomEntry] [partId] [qty] [cmnt] [srcLoc] [dType] [lead] [lineNbr]
700349L A 1 600446 1 `TEST` TEST 0 0 1
700349L A 2 201743 1 `TEST` TEST 0 0 2
700349L A 3 100208 1 `TEST` TEST 0 0 3
700349L A 4 300219 1 `TEST` TEST 0 0 4
700349L A 5 400533 1 `TEST` TEST 0 0 5
700349L A 6 100218 1 `TEST` TEST 0 0 6
700349L A 7 100222 1 `TEST` TEST 0 0 7
700349L A 8 100220 1 `TEST` TEST 0 0 8
700349L A 9 100221 1 `TEST` TEST 0 0 9
Then I want to use insert from select statement as shown below but I like to transpose the row to multiple columns
INSERT INTO [DT]([bomItem], [bomRev], [bomEntry], [partId], [qty], [cmnt],[srcLoc], [dType], [lead], [lineNbr])
SELECT
[STOCK NO], 'A', [bomEntry], [partid], [qty], 'TEST', 'TEST', '0', '0', [lineNbr]
FROM
[ST]
This uses the columns and example data prior to additional edits in the question.
using cross apply() to unpivot your columns and row_number() to number only those rows that have a value:
--INSERT INTO [DT]([bomItem], [bomRev], [bomEntry], [partId], [qty], [cmnt],[srcLoc], [dType], [lead], [lineNbr])
select
st.[CALC STOCK NO]
, bomRev = 'A'
, bomEntry = row_number() over (order by u.ordinal)
, u.Partid
, st.Qty
, cmnt = 'Test'
, srcLoc = 'Test'
, dType = '0'
, lead = '0'
, lineNbr = row_number() over (order by u.ordinal)
from st
cross apply (values
(1,[BOM WHEEL PN])
,(2,[BOM TIRE PN])
,(3,[BOM VALVE PN])
,(4,[BOM - SECONDARY DISC PN])
,(5,[BOM - DISC])
,(6,[BOM - RIM])
,(7,[BUY WHEEL])
,(8,[COLOR PN])
,(9,[WHL BOM PART 1 PN])
,(10,[WHL BOM PART 2 PN])
,(11,[WHL BOM PART 3 PN])
,(12,[WHL BOM PART 4 PN])
,(13,[WHL BOM PART 5 PN])
) u (ordinal, partId)
where nullif(u.partId,'') is not null
rextester demo: http://rextester.com/XNGB57562
returns:
+---------------+--------+----------+--------+-----+------+--------+-------+------+---------+
| CALC STOCK NO | bomRev | bomEntry | Partid | Qty | cmnt | srcLoc | dType | lead | lineNbr |
+---------------+--------+----------+--------+-----+------+--------+-------+------+---------+
| 700349L | A | 1 | 600446 | 1 | Test | Test | 0 | 0 | 1 |
| 700349L | A | 2 | 201743 | 1 | Test | Test | 0 | 0 | 2 |
| 700349L | A | 3 | 100208 | 1 | Test | Test | 0 | 0 | 3 |
| 700349L | A | 4 | 300219 | 1 | Test | Test | 0 | 0 | 4 |
| 700349L | A | 5 | 400533 | 1 | Test | Test | 0 | 0 | 5 |
| 700349L | A | 6 | 100280 | 1 | Test | Test | 0 | 0 | 6 |
| 700349L | A | 7 | 100218 | 1 | Test | Test | 0 | 0 | 7 |
| 700349L | A | 8 | 100222 | 1 | Test | Test | 0 | 0 | 8 |
| 700349L | A | 9 | 100220 | 1 | Test | Test | 0 | 0 | 9 |
| 700349L | A | 10 | 100221 | 1 | Test | Test | 0 | 0 | 10 |
+---------------+--------+----------+--------+-----+------+--------+-------+------+---------+
Pivot should work for you.
Check out this link: https://technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx
If you post enough dummy data someone would be able to help you along with the query. You can utilize something like http://rextester.com/ to share dummy data.
Good Luck!
I have following table in my SQL Server database:
| ID | Class | CId | PId
| 7865 | Add Class for Prop | 1043 | 1
| 82 | Advanced Carpet Spotting Advanced Carpet Spotting | 1043 | 1
| 82 | Advanced Carpet Spotting Advanced Carpet Spotting | 1042 | 1
| 7863 | aTesting | 1042 | 0
| 7218 | aUnique | 1042 | 0
| 85 | Body Mechanics | 1042 | 1
| 88 | Carpet Bonnet Cleaning | 1044 | 0
| 89 | Carpet Shampooing/Extraction | 1044 | 1
| 7829 | Class 10 | 1044 | 0
I have multiple CId and PId
If distinct CId have their corresponding PId = 1 Then,
Set CId = 1 Otherwise 0
I want output like this
| CId | Status | Count
| 1042 | 0 | 4
| 1043 | 1 | 2
| 1044 | 0 | 3
I can get required output by using multiple queries but I want more optimized one .
Please suggest a solution. Thank you.
select cid,
case when count(case when pid = 1 then 1 end) > 0
then 1
else 0
end as status,
count(*) as count
from your_table
group by cid
If PId is always 1 and 0, you can do the following.
SELECT
CID,
AVG([Status] * 1) AS [Status],
COUNT(*) AS [Count]
FROM
YourTable
GROUP BY
CID
Use a CASE expression to check whether the sum of PId is equal to count of PId.
Query
select [CId],
case when sum([Cid]) = count([CId]) then 1
else 0 end as [Status],
count([CId]) as [Count]
from [your_table_name]
group by [CId];
Also PId should have only 1 and 0 values.