Selecting multiple counted fields in T-SQL query - tsql

I am stuck on a SQL query, where I need to count the number of different values in a field.
My query is:
SELECT
Area
, (SELECT count(TestResult) FROM TestRun WHERE TestResult = 'PASS' AND TestRun.CreatedDate > '2019-11-18 01:00:00') as [Passed]
, (SELECT count(TestResult) FROM TestRun WHERE TestResult = 'FAIL' AND TestRun.CreatedDate > '2019-11-18 01:00:00') as [Failed]
, (SELECT count(TestResult) FROM TestRun WHERE TestResult = 'NOTRUN' AND TestRun.CreatedDate > '2019-11-18 01:00:00') as [NotRun]
FROM TestRun
WHERE dbo.TestRun.CreatedDate > '2019-11-18 01:00:00'
GROUP BY dbo.TestRun.Area, TestRun.TestResult, TestRun.CreatedDate
But The Results I get back look like this, instead of individual values:

You can create your own fields using case and then sum them.
SELECT Area
, SUM(CASE WHEN TestResult = 'PASS' THEN 1 ELSE 0 END) AS Passed
, SUM(CASE WHEN TestResult = 'FAIL' THEN 1 ELSE 0 END) AS Failed
, SUM(CASE WHEN TestResult = 'NOTRUN' THEN 1 ELSE 0 END) AS NotRun
FROM TestRun
WHERE CreatedDate > '2019-11-18 01:00:00'
GROUP BY Area

Related

Aggregate Nested Pgsql query

I'm trying to query a table to get me some results, but the way I'm doing it gives me the error: ERROR: aggregate function calls cannot be nested.
The table looks like this:
ID | canal_c1 | tarifacao | date | ativo_id
The query I'm trying is this:
SELECT
SUM(case when tarifacao = 'ForaPonta' then canal_c1 else 0 end) as ConsForaPonta,
MAX(case when tarifacao = 'ForaPonta' then canal_c1 else 0 end) as DemForaPonta,
ativo_id as ativo_id,
data_leitura_inicio::date as date
FROM
medicao
WHERE
medicao.ativo_id in (45) AND
medicao.tipo_leitura = 'Consumo' AND
medicao.data_leitura_inicio >= '2017-01-01' AND
medicao.data_leitura_inicio < '2017-01-10'
GROUP BY
medicao.ativo_id,
medicao.data_leitura_inicio::date
That gives me result like these:
query result
And that's fine what I need now is the datetime from the DemForaPonta field, in order to do that I trying this, but got that error.
MAX(case when tarifacao = 'ForaPonta' and
canal_c1 = MAX(case when tarifacao = 'ForaPonta' then canal_c1 else 0 end)
then data_leitura_inicio end) as DateDemForaPonta
Do you know how I could achieve this?
Thanks.
Edit:
Here's an example, the query result is the intended result.
example
It would be helpful to have an example with real data. But anyway, you can do what you need with nested selects, among other options.
Nested SELECTs
SELECT *,
MAX(case when tarifacao = 'ForaPonta' and canal_c1 = DemForaPonta then data_leitura_inicio END)
AS DateDemForaPonta
FROM(
SELECT
SUM(case when tarifacao = 'ForaPonta' then canal_c1 else 0 end) as ConsForaPonta,
MAX(case when tarifacao = 'ForaPonta' then canal_c1 else 0 end) as DemForaPonta,
ativo_id as ativo_id,
data_leitura_inicio::date as date
FROM
medicao
WHERE
medicao.ativo_id in (45) AND
medicao.tipo_leitura = 'Consumo' AND
medicao.data_leitura_inicio >= '2017-01-01' AND
medicao.data_leitura_inicio < '2017-01-10'
GROUP BY
medicao.ativo_id,
medicao.data_leitura_inicio::date
)data
Since I don't have data to test it, don't really know if it does the trick, but it should.

Merging several joins into a single select / case

I am currently querying a table 4 times using different criteria and then using left joins as part of a much larger query to return all the data. The larger query does not run particularly quickly and I am fairly certain that my current approach is not efficient.
What I am wondering is if it is possible to somehow use a CASE statement to increment one of the 4 columns. My 4 queries currently are:
SELECT ts.department,
Sum([hours]) AS ChargeableTimeYTD
FROM bwbfiles.sos.timesummary ts
WHERE category = 'C'
AND [year] = '2019'
GROUP BY department
SELECT ts.department,
Sum([hours]) AS ChargeableTimeMTD
FROM bwbfiles.sos.timesummary ts
WHERE category = 'C'
AND [year] = '2019'
AND [period] = 4
GROUP BY department
SELECT ts.department,
Sum([hours]) AS NonChargeableTimeProBono
FROM bwbfiles.sos.timesummary ts
WHERE category = 'NC'
AND ( [act_code] = '001N'
OR [act_code] = '00N6' )
AND [year] = '2019'
GROUP BY department
SELECT ts.department,
Sum([hours]) AS NonChargeableTimeNonProBono
FROM bwbfiles.sos.timesummary ts
WHERE category = 'NC'
AND ( [act_code] <> '001N'
AND [act_code] <> '00N6' )
AND [year] = '2019'
GROUP BY department
The aim would be to end up with a query result with 5 columns
Department, ChargeableTimeYTD, ChargeableTimeMTD, NonChargeableTimeProBono, NonChargeableTimeNonProBono
Or instead of CASE would I remove the group by department from each bit and have a query that produced 3 columns
Department, Hours, Category (where Category is ChargeableTimeYTD/ChargeableTimeMTD etc...etc...) and then pivot that into 5 columns.
Thanks in advance!
This may do what i think you're asking for
SELECT ts.department,
Sum(case when category = 'C' then [hours] else 0 end) AS ChargeableTimeYTD,
Sum(case when category = 'C' and [period] = 4 then [hours] else 0 end) AS ChargeableTimeMTD,
Sum(case when category = 'NC' and ([act_code] = '001N' or [act_code] = '00N6') then [hours] else 0 end) AS NonChargeableTimeProBono,
Sum(case when category = 'NC' and ([act_code] <> '001N' or [act_code] <> '00N6') then [hours] else 0 end) AS NonChargeableTimeNonProBono
FROM bwbfiles.sos.timesummary ts
where [year] = '2019'
and [category] in ('C','NC')
GROUP BY department

Combine Two Queries in one SUM Case

I have two queries with exact same grouping but I dont seem to be able to combine them in a correct way.
Query1:
SELECT
WorkPeriods.Id AS Z_Number,
CONVERT(VARCHAR, (CONVERT(DATE, WorkPeriods.StartDate, 103)), 103) AS Z_Date,
SUM(CASE WHEN Payments.Name = 'Cash' THEN Payments.Amount ELSE 0 END) AS Cash_Payments,
COUNT(CASE WHEN Payments.Name = 'Cash' THEN 1 END) AS No_of_Tickets_Cash,
SUM(CASE WHEN Payments.Name = 'Credit Card' THEN Payments.Amount ELSE 0 END) AS Credit_Card_Payments,
COUNT(CASE WHEN Payments.Name = 'Credit Card' THEN 1 END) AS No_of_Tickets_Credit_Card
FROM
Payments, WorkPeriods
WHERE
Payments.Date BETWEEN WorkPeriods.StartDate AND WorkPeriods.EndDate
GROUP BY
WorkPeriods.Id, WorkPeriods.StartDate
Query 2:
SELECT
WorkPeriods.Id AS Z_Number,
CONVERT(VARCHAR, (CONVERT(DATE, WorkPeriods.StartDate, 103)), 103) AS Z_Date,
SUM(CASE WHEN Orders.CalculatePrice = 0 THEN Orders.Quantity * Orders.Price ELSE 0 END) AS Gifts_Amount,
SUM(CASE WHEN Orders.CalculatePrice = 0 THEN Orders.Quantity ELSE 0 END) AS No_of_Gift_Orders
FROM
Orders, WorkPeriods
WHERE
Orders.CreatedDateTime BETWEEN WorkPeriods.StartDate AND WorkPeriods.EndDate
GROUP BY
WorkPeriods.Id, WorkPeriods.StartDate
Any advice on how to continue? I have already tried merging them using all 3 tables and all sum-count conditions but the result I get is wrong. I need all results to appear on the same row. Attached are query results
You can't just join them all in the one query, as you will get incorrect values as soon as you get multiple orders or payments in the same workperiod.
You could use the current queries as sub queries, and full join them to get the result. By using full join you get any results that are only on one table and not the other.
Select ISNULL(Pay.Z_Number, Ord.Z_Number) As Z_Number,
ISNULL(Pay.Z_Date, Ord.Z_Date) as Z_Date,
Pay.CashPayments,
Pay.No_of_Tickets_Cash,
Ord.Gifts_Amount
--other fields as appropriate
FROM (
--Query 1 here
) AS Pay
FULL OUTER JOIN (
--Query 2 here
) as Ord ON Pay.Z_Number = Ord.Z_Number and Pay.Z_Date = Ord.Z_Date
Another way to do this, is to create one sub query that has the data from both payments and orders in it unioned together, and then sum the resulting list in the outer query.
Below sample query may be helpful
SELECT
MAIN_T.Z_Number
,MAIN_T.Z_Date
,T1.Cash_Payments
,T1.Credit_Card_Payments
,T1.No_of_Tickets_Cash
,T1.No_of_Tickets_Credit_Card
,T2.Gifts_Amount
,T2.No_of_Gift_Orders
FROM
(SELECT DISTINCT
WorkPeriods.Id AS Z_Number,
CONVERT(VARCHAR, (CONVERT(DATE, WorkPeriods.StartDate, 103)), 103) AS Z_Date
FROM
Payments, WorkPeriods
WHERE
Payments.Date BETWEEN WorkPeriods.StartDate AND WorkPeriods.EndDate ) MAIN_T
LEFT JOIN
(SELECT
WorkPeriods.Id AS Z_Number,
CONVERT(VARCHAR, (CONVERT(DATE, WorkPeriods.StartDate, 103)), 103) AS Z_Date,
SUM(CASE WHEN Payments.Name = 'Cash' THEN Payments.Amount ELSE 0 END) AS Cash_Payments,
COUNT(CASE WHEN Payments.Name = 'Cash' THEN 1 END) AS No_of_Tickets_Cash,
SUM(CASE WHEN Payments.Name = 'Credit Card' THEN Payments.Amount ELSE 0 END) AS Credit_Card_Payments,
COUNT(CASE WHEN Payments.Name = 'Credit Card' THEN 1 END) AS No_of_Tickets_Credit_Card
FROM
Payments, WorkPeriods
WHERE
Payments.Date BETWEEN WorkPeriods.StartDate AND WorkPeriods.EndDate
GROUP BY
WorkPeriods.Id, WorkPeriods.StartDate) T1
ON MAIN_T.Z_Number=T1.Z_Number AND MAIN_T.Z_Date=T1.Z_Date
LEFT JOIN
(SELECT
WorkPeriods.Id AS Z_Number,
CONVERT(VARCHAR, (CONVERT(DATE, WorkPeriods.StartDate, 103)), 103) AS Z_Date,
SUM(CASE WHEN Orders.CalculatePrice = 0 THEN Orders.Quantity * Orders.Price ELSE 0 END) AS Gifts_Amount,
SUM(CASE WHEN Orders.CalculatePrice = 0 THEN Orders.Quantity ELSE 0 END) AS No_of_Gift_Orders
FROM
Orders, WorkPeriods
WHERE
Orders.CreatedDateTime BETWEEN WorkPeriods.StartDate AND WorkPeriods.EndDate
GROUP BY
WorkPeriods.Id, WorkPeriods.StartDate) T2
ON MAIN_T.Z_Number=T2.Z_Number AND MAIN_T.Z_Date=T2.Z_Date

SQL Query handing Jaspersoft Studio Read Fields

I have this current query. I have tested it on 2 of the same verions of Jaspersoft Studio. When I hit read fields, one instance of the program gives me the columns I want however the other instance is stuck in a loop. I have checked the error logs and there is nothing for me to troubleshoot.
I had theorized it was the query but why would it work in one instance and not the next.
Where can I look to find out what is happening? What are my options?
SELECT
LOCATIONS.ID AS LOCATIONID,
LOCATIONS.NAME AS LOCATIONNAME,
PRODUCTS.REFERENCE,
PRODUCTS.CODE AS BARCODE,
PRODUCTS.NAME AS ITEM_NAME,
STOCKDIARY.ATTRIBUTESETINSTANCE_ID,
ATTRIBUTESETINSTANCE.DESCRIPTION AS ATTINSTANCEDESC,
PRODUCTS.CATEGORY,
TICKETLINES.UNITS,
CATEGORIES.NAME AS CATEGORYNAME,
STOCKDIARY.DATENEW,
STOCKDIARY.REASON,
SUM(CASE WHEN STOCKDIARY.UNITS <0 AND STOCKDIARY.DATENEW >= $P{start_date} AND (STOCKDIARY.DATENEW) <= $P{end_date} THEN STOCKDIARY.UNITS ELSE 0 END) AS UNITSOUT,
SUM(CASE WHEN STOCKDIARY.UNITS >=0 AND STOCKDIARY.DATENEW >= $P{start_date} AND (STOCKDIARY.DATENEW) <= $P{end_date} THEN STOCKDIARY.UNITS ELSE 0 END) AS UNITSIN,
SUM(CASE WHEN STOCKDIARY.DATENEW <= $P{end_date} THEN STOCKDIARY.UNITS ELSE 0 END) AS STOCKTOTALNEW,
SUM(CASE WHEN STOCKDIARY.DATENEW <= $P{start_date} THEN STOCKDIARY.UNITS ELSE 0 END) AS STARTINGBALANCE,
SUM(TICKETLINES.UNITS) AS UNIT
FROM TICKETLINES,
STOCKDIARY
LEFT JOIN
LOCATIONS ON STOCKDIARY.LOCATION = LOCATIONS.ID
INNER JOIN
PRODUCTS ON PRODUCTS.ID = STOCKDIARY.PRODUCT
LEFT JOIN
CATEGORIES ON PRODUCTS.CATEGORY = CATEGORIES.ID
LEFT JOIN
ATTRIBUTESETINSTANCE ON STOCKDIARY.ATTRIBUTESETINSTANCE_ID = ATTRIBUTESETINSTANCE.ID
WHERE products.name LIKE "%"
GROUP BY LOCATIONS.NAME , PRODUCTS.REFERENCE , STOCKDIARY.ATTRIBUTESETINSTANCE_ID , ATTRIBUTESETINSTANCE.DESCRIPTION
ORDER BY PRODUCTS.NAME

how to select top 10 without duplicates

Using SQL Server 2012
I need to select TOP 10 Producer based on a ProducerCode. But the data is messed up, users were entering same Producers just spelled differently and with the same ProducerCode.
So I just need TOP 10, so if the ProducerCode is repeating, I just want to pick the first one in a list.
How can I achieve that?
Sample of my data
;WITH cte_TopWP --T
AS
(
SELECT distinct ProducerCode, Producer,SUM(premium) as NetWrittenPremium,
SUM(CASE WHEN PolicyType = 'New Business' THEN Premium ELSE 0 END) as NewBusiness1,
SUM(CASE WHEN PolicyType = 'Renewal' THEN Premium ELSE 0 END) as Renewal1,
SUM(CASE WHEN PolicyType = 'Rewrite' THEN Premium ELSE 0 END) as Rewrite1
FROM ProductionReportMetrics
WHERE YEAR(EffectiveDate) = 2016 AND TransactionType = 'Policy' AND CompanyLine = 'Arch Insurance Company'--AND ProducerType = 'Wholesaler'
GROUP BY ProducerCode,Producer
)
,
cte_Counts --C
AS
(
SELECT distinct ProducerCode, ProducerName, COUNT (distinct ControlNo) as Submissions2,
SUM(CASE WHEN QuotedPremium IS NOT NULL THEN 1 ELSE 0 END) as Quoted2,
SUM(CASE WHEN Type = 'New Business' AND Status IN ('Bound','Cancelled','Notice of Cancellation') THEN 1 ELSE 0 END ) as NewBusiness2,
SUM(CASE WHEN Type = 'Renewal' AND Status IN ('Bound','Cancelled','Notice of Cancellation') THEN 1 ELSE 0 END ) as Renewal2,
SUM(CASE WHEN Type = 'Rewrite' AND Status IN ('Bound','Cancelled','Notice of Cancellation') THEN 1 ELSE 0 END ) as Rewrite2,
SUM(CASE WHEN Status = 'Declined' THEN 1 ELSE 0 END ) as Declined2
FROM ClearanceReportMetrics
WHERE YEAR(EffectiveDate)=2016 AND CompanyLine = 'Arch Insurance Company'
GROUP BY ProducerCode,ProducerName
)
SELECT top 10 RANK() OVER (ORDER BY NetWrittenPremium desc) as Rank,
t.ProducerCode,
c.ProducerName as 'Producer',
NetWrittenPremium,
t.NewBusiness1,
t.Renewal1,
t.Rewrite1,
c.[NewBusiness2]+c.[Renewal2]+c.[Rewrite2] as PolicyCount,
c.Submissions2,
c.Quoted2,
c.[NewBusiness2],
c.Renewal2,
c.Rewrite2,
c.Declined2
FROM cte_TopWP t --LEFT OUTER JOIN tblProducers p on t.ProducerCode=p.ProducerCode
LEFT OUTER JOIN cte_Counts c ON t.ProducerCode=c.ProducerCode
You should use ROW_NUMBER to fix your issue.
https://msdn.microsoft.com/en-us/library/ms186734.aspx
A good example of this is the following answer:
https://dba.stackexchange.com/a/22198
Here's the code example from the answer.
SELECT * FROM
(
SELECT acss_lookup.ID AS acss_lookupID,
ROW_NUMBER() OVER
(PARTITION BY your_distinct_column ORDER BY any_column_you_think_is_appropriate)
as num,
acss_lookup.product_lookupID AS acssproduct_lookupID,
acss_lookup.region_lookupID AS acssregion_lookupID,
acss_lookup.document_lookupID AS acssdocument_lookupID,
product.ID AS product_ID,
product.parent_productID AS productparent_product_ID,
product.label AS product_label,
product.displayheading AS product_displayheading,
product.displayorder AS product_displayorder,
product.display AS product_display,
product.ignorenewupdate AS product_ignorenewupdate,
product.directlink AS product_directlink,
product.directlinkURL AS product_directlinkURL,
product.shortdescription AS product_shortdescription,
product.logo AS product_logo,
product.thumbnail AS product_thumbnail,
product.content AS product_content,
product.pdf AS product_pdf,
product.language_lookupID AS product_language_lookupID,
document.ID AS document_ID,
document.shortdescription AS document_shortdescription,
document.language_lookupID AS document_language_lookupID,
document.document_note AS document_document_note,
document.displayheading AS document_displayheading
FROM acss_lookup
INNER JOIN product ON (acss_lookup.product_lookupID = product.ID)
INNER JOIN document ON (acss_lookup.document_lookupID = document.ID)
)a
WHERE a.num = 1
ORDER BY product_displayheading ASC;
You could do this:
SELECT ProducerCode, MIN(Producer) AS Producer, ...
GROUP BY ProducerCode