Group and sum with SQL Server - sql-server

I am a begginer using this kind of query in SQL Server and I have a big table similar as I'm showing, the trouble that I have is that I don't know how to group and sum at the same time, I need to sum the SUP with every SIG with the code
CODE SIG SUP
R501004002 BCO 30
R501004002 BCO 31.27
R501004008 BCO 34.09
R501004008 BCO 35.94
R501004003 MET 42
R501004020 RIP 42.5
R501004039 BCO 47.44
R501004020 RIP 68.56
The result that I want to show is something like this
CODE BCO MET RIP
R501004002 61.27 0 0
R501004003 0 42 0
R501004008 70.03 0 0
R501004020 0 0 111.06
R501004039 47.44 0 0
I group the code

Assuming only 3 sigs...
SELECT code
, sum(case when sig = 'BCO' then sup else 0 end) as BCO
, sum(case when sig = 'MET' then sup else 0 end) as MET
, sum(case when sig = 'RIP' then sup else 0 end) as RIP
FROM tableName
GROUP BY code
otherwise use a dynamic pivot as exampled here

Related

I want to add a column only flat numbers without decimal

My column data will be like this
1,
2,
1.02,
2.23,
5
I want to add only 1,2,5 and remaining should be print as it is
How can i add that.?
SUM(case when col not like '%,%' then col else 0 end )
I tried with this but remaining value it is making 0
I am not good at explaining i hope u will understand.
Perhap you need the following?
SUM(case when col % 1 = 0 then col else 0 end)

excel match year and month in array

I have an array of dates in column a that i would like to match against on year and month.
A B C D E F G H I J
1 date name Name March April May June July August September
2 13-04-2016 Lars Lars 0 0 0 0 0 0 0
3 04-03-2016 Brian Brian 0 0 0 0 0 0 0
4 01-01-2016 Lars Erik 0 0 0 0 0 0 0
5 10-06-2016 Erik Knut 0 0 0 0 0 0 0
6 31-07-2016 Erik Soren 0 0 0 0 0 0 0
I have tried with $A:$A;"="&DATE(YEAR(TODAY());3;""); where ;3; is the month of march. It evaluates to 0 on all accounts.
So how to adapt to make it count the number of dates matching the Year and Month (D2) for Lars (C2) in A:A?
Anyone.
One way to do this is to convert the date to text, then evaluate that in your formula.
Logic:
If Monthname(Date) = Top Row, then 1, else 0
Formula (placed in cell D2):
=if(text($A1,"mmmm")=D$1,1,0)
I have used the relative references for clicking and dragging.
Put this in D2 (and adapt as needed)
=IF(DATE(YEAR(TODAY()),3,1)=DATE(YEAR(A3),MONTH(A3),1),1,0)
=IF( --Start the if
DATE(YEAR(TODAY()),3,1) -- Current year, 3rd month, 1st day
= --Equals (for if statement)
DATE(YEAR(A3),MONTH(A3),1) -- Year & month from Cell A3 & 1st day
,1,0) -- Print '1' if true else print '0'

How can I create query that (counting values inside Column)

I have 3 companies 1001,1002 ,1003 it could be more and 11 containers with different sizes 1,2,3,4, 5 I want return only the containers that are in the companies that have the same amount or more of specified numbers. for example if I want 2 containers from size 1 and 3 containers from size 2 then only the containers in the company that has 2 or more of size 1 and 3 or more of size 2 should appear let's say that only company 1001 has them then it should appear alone.
I tried different queries and post one here but they recommend me to post a new question with the problem that I'm training to make query for.
(Company info and containers info are in two separate tables)
this is what I get when I remove having (basically all the containers in the city that has been selected)
CoID CoName ContainerID Price size1 size2 size3 size4 size5
6000001 hbjjvCompany 2000002 50 1 0 0 0 0
6000001 hbjjvCompany 2000003 50 1 0 0 0 0
6000002 NCompany 2000004 50 1 0 0 0 0
6000001 hbjjvCompany 2000005 100 0 1 0 0 0
6000002 NCompany 2000007 100 0 1 0 0 0
6000001 hbjjvCompany 2000008 200 0 0 1 0 0
6000001 hbjjvCompany 2000009 200 0 0 1 0 0
6000001 hbjjvCompany 2000010 200 0 0 1 0 0
6000002 NCompany 2000011 200 0 0 1 0 0
6000001 hbjjvCompany 2000012 400 0 0 0 0 1
6000003 ghhaCo 2000014 200 0 1 0 0 0
what should I get is
CoID CoName size1 size2 size3 size4 size5
6000001 hbjjvCompany 2 1 3 0 1
of course I want the containers id and the price but I put it heare like this to make it clear that my query show all the containers even if i removed the ContainerID and price.
I think it's this you're looking for:
CREATE TABLE #YourTable(CoID INT,CoName VARCHAR(100),ContainerID INT,Price DECIMAL(10,4),size1 INT,size2 INT,size3 INT,size4 INT,size5 INT);
INSERT INTO #YourTable VALUES
(6000001,'hbjjvCompany',2000002,50,1,0,0,0,0)
,(6000001,'hbjjvCompany',2000003,50,1,0,0,0,0)
,(6000002,'NCompany',2000004,50,1,0,0,0,0)
,(6000001,'hbjjvCompany',2000005,100,0,1,0,0,0)
,(6000002,'NCompany',2000007,100,0,1,0,0,0)
,(6000001,'hbjjvCompany',2000008,200,0,0,1,0,0)
,(6000001,'hbjjvCompany',2000009,200,0,0,1,0,0)
,(6000001,'hbjjvCompany',2000010,200,0,0,1,0,0)
,(6000002,'NCompany',2000011,200,0,0,1,0,0)
,(6000001,'hbjjvCompany',2000012,400,0,0,0,0,1)
,(6000003,'ghhaCo',2000014,200,0,1,0,0,0);
SELECT CoID
,CoName
,SUM(Price) AS SumPrice
,SUM(size1) AS CountSize1
,SUM(size2) AS CountSize2
,SUM(size3) AS CountSize3
,SUM(size4) AS CountSize4
,SUM(size5) AS CountSize5
FROM #YourTable
GROUP BY CoID,CoName;
--Clean up
DROP TABLE #YourTable;
The result
CoID CoName SumPrice s1 s2 s3 s4 s5
6000003 ghhaCo 200.0000 0 1 0 0 0
6000001 hbjjvCompany 1200.0000 2 1 3 0 1
6000002 NCompany 350.0000 1 1 1 0 0

MS SQL Table of CRON Future Runs

I have a need to create a table/view containing a full and complete list of future CRON executions for a period of time, e.g. from 12 months ago to 12 months in the future.
My source data is in MS SQL 2012 and contains the following sample information;
TASK SCHEDULE SCHEDULESTART SCHEDULEEND
T1 0 0 0 ? * MON 2015-04-08 16:15:09.557 2015-04-20 00:00:00.000
T2 0 0 0 ? * MON 2015-05-22 15:56:48.140 2015-07-27 00:00:00.000
T3 0 0/56 * * * ? 2015-06-25 10:17:07.387 2015-06-25 15:00:00.000
T4 0 10/15 21 3,19 5-9 ? 2015-06-25 10:18:48.077 2015-08-28 10:17:15.000
Unfortunately as MS SQL doesn't support/contain a JVM, I'm limited (I think) to programmatically breaking this out into it's components parts.
I've managed to break out he parts of the expression with the following;
;WITH cte (SCHEDULE,SCHEDULESTART,SCHEDULEEND,SCHED_Attributes)
AS
(
SELECT SCHEDULE,SCHEDULESTART,SCHEDULEEND,
CONVERT(XML,'<Product><Attribute>'
+ REPLACE([SCHEDULE],' ', '</Attribute><Attribute>')
+ '</Attribute></Product>') AS SCHED_Attributes
FROM USCH_TASK
)
SELECT
SCHEDULE,SCHEDULESTART,SCHEDULEEND,
SCHED_Attributes.value('/Product[1]/Attribute[1]','varchar(25)') AS sched_seconds,
SCHED_Attributes.value('/Product[1]/Attribute[2]','varchar(25)') AS sched_minutes,
SCHED_Attributes.value('/Product[1]/Attribute[3]','varchar(25)') AS sched_hours,
SCHED_Attributes.value('/Product[1]/Attribute[4]','varchar(25)') AS sched_day_of_month,
SCHED_Attributes.value('/Product[1]/Attribute[5]','varchar(25)') AS sched_month,
SCHED_Attributes.value('/Product[1]/Attribute[6]','varchar(25)') AS sched_day_of_week,
SCHED_Attributes.value('/Product[1]/Attribute[7]','varchar(25)') AS sched_year
from cte
This results in (for example)
sched_seconds sched_minutes sched_hours sched_day_of_month sched_month sched_day_of_week sched_year
0 0 0 ? * MON NULL
0 0 0 ? * MON NULL
0 0/56 * * * ? NULL
0 10/15 21 3,19 5-9 ? NULL
Main thrust of this question is then how to handle the component parts of this, * and ? are easy enough, ranges (e.g. 5-9 or MON-THU) are pretty OK, but am struggling with how to determine where have specific dates/months (e.g. 3,19) or more complex configurations (such as the last example above or days of month ="1-3,6-7,15")
CASE
WHEN CHARINDEX('*',Prod_Attributes.value('/Product[1]/Attribute[6]','varchar(25)')) > 0 THEN 'Y'
WHEN CHARINDEX('?',Prod_Attributes.value('/Product[1]/Attribute[6]','varchar(25)')) > 0 THEN 'Y'
WHEN CHARINDEX('MON',Prod_Attributes.value('/Product[1]/Attribute[6]','varchar(25)')) > 0 THEN 'Y'
WHEN CHARINDEX('SUN-TUE',Prod_Attributes.value('/Product[1]/Attribute[6]','varchar(25)')) > 0 THEN 'Y'
WHEN CHARINDEX('SUN-WED',Prod_Attributes.value('/Product[1]/Attribute[6]','varchar(25)')) > 0 THEN 'Y'
WHEN CHARINDEX('SUN-THU',Prod_Attributes.value('/Product[1]/Attribute[6]','varchar(25)')) > 0 THEN 'Y'
WHEN CHARINDEX('SUN-FRI',Prod_Attributes.value('/Product[1]/Attribute[6]','varchar(25)')) > 0 THEN 'Y'
WHEN CHARINDEX('SUN-SAT',Prod_Attributes.value('/Product[1]/Attribute[6]','varchar(25)')) > 0 THEN 'Y'
ELSE 'N'
END as DOWMon
However this approach wouldn't work for day 1 of the month as the code
WHEN CHARINDEX('1',Prod_Attributes.value('/Product[1]/Attribute[4]','varchar(25)')) > 0 THEN 'Y'
would also find value 10 through 19, 21 and 31!
Any tips or tricks are gratefully received!
Andy

Can't figure out how sum(1) is working in this query

So I am trying to combine to query's and to do that I need to figure out what is going on in this one. I'm still relatively new to sql server and I am forced to dive right into some complicated qrys and sometimes I get stuck on simple things like this. My problem is that the Sum(1) function is used and I'm not entirely sure how. Meaning I believe it is counting duplicates but I cannot tell based on what information it is doing so.
this is the query
SELECT
qryReinsuranceDPA1.POLICY_NO,
qryReinsuranceDPA1.PHASE_CODE,
qryReinsuranceDPA1.SUB_PHASE_CODE,
qryReinsuranceDPA1.ProdType,
TotalDPA = Sum(case when [SumOfNetDefExtraAdj] Is Null then [SumOfNetDefPremiumAdj] else [SumOfNetDefPremiumAdj] + SumOfNetDefExtraAdj end),
Sum(1) AS Expr1
FROM qryPolicyListforNYDefPRemAsset_Re RIGHT JOIN qryReinsuranceDPA1
ON
qryReinsuranceDPA1.POLICY_NO = qryPolicyListforNYDefPRemAsset_Re.POLICY_NO AND
qryReinsuranceDPA1.PHASE_CODE= qryPolicyListforNYDefPRemAsset_Re.PHASE_CODE AND
qryReinsuranceDPA1.SUB_PHASE_CODE = qryPolicyListforNYDefPRemAsset_Re.SUB_PHASE_CODE
GROUP BY qryReinsuranceDPA1.POLICY_NO,
qryReinsuranceDPA1.PHASE_CODE,
qryReinsuranceDPA1.SUB_PHASE_CODE,
qryReinsuranceDPA1.ProdType
--HAVING (((Sum(1))<>1))
GO
And this is a small sample of what it produces (the actually results number around 77,000)
POLICY_NO PHASE_CODE SUB_PHASE_CODE ProdType TotalDPA Expr1
228433800 0 1 TERM 282.324223 1
228439200 0 1 PERM 53.17048634 1
228439200 6 1 PERM 10.3805065 1
228441500 0 1 PERM 526.6883742 1
228441500 0 2 PERM 10.63320899 1
228441700 0 1 PERM 20.86247317 1
228448100 0 1 PERM 345.2117169 1
228460200 0 1 TERM 302.7574933 1
228464900 0 1 TERM 191.2597906 1
228468000 0 1 PERM 8445.190912 1
228473600 0 1 TERM 339.8413682 **2**
228473800 0 1 TERM 686.1766864 **2**
228477200 0 1 TERM 583.7580207 1
228481200 0 1 TERM 362.9472595 1
228481200 0 2 PERM 4.217792443 1
228482500 0 1 PERM 1894.303507 1
228482500 1 1 TERM 1312.183889 1
228491600 0 1 TERM 325.0796843 **2**
228494400 0 1 PERM 748.2710255 1
228501000 0 1 TERM 47.78070676 1
228501100 0 1 TERM 47.78070676 1
228501300 0 1 PERM 365.5651862 1
228501300 0 2 PERM 12.20547324 1
228501300 1 1 TERM 706.0961491 1
228501300 1 2 PERM 12.46769547 1
228502000 0 1 PERM 6562.164879 1
228502000 0 2 PERM 184.7741277 1
The right most column is the result of the Sum(1) and what I want to know is when and why does it produce a 2.
sum(1) is exactly equivalent to count(*) - it returns a count of all the rows within the group.
It will therefore return a value of 2 when, for a given value for each of POLICY_NO, PHASE_CODE, SUB_PHASE_CODE and ProdType there are two rows in the selected dataset (before grouping).

Resources