I am working with a TABLE, need logical help
This query:
SELECT
DATE_FORMAT(tran_date, '%M %Y') AS month_name,
SUM(IF(c.ctype = 4, gl.amount * -1, 0)) AS sales,
GROUP_CONCAT(DISTINCT(d.name)) AS d_name
FROM
gl_trans AS gl, company_chart_masters AS a,
company_chart_types AS t, company_chart_classes AS c, dimensions as d
WHERE
(c.ctype = 4 OR c.ctype = 5) AND
d.id = gl.dimension2_id AND
gl.account = a.account_code AND
a.account_type = t.id AND
t.class_id = c.id AND
DIMENSION2_id = gl.dimension2_id
GROUP BY
month_name
ORDER BY
month_name DESC
Produces this result:
As you can see the output of the query has 3 columns month_name, sale and pos where the pos column is separated with comma. But what I want now is to split the pos in different columns (factor matab, Rawalpindi matab, ...).
My second problem is that I also split the sale column like a split the pos column ans show sale accordingly pos wise and this sale column also shows in different columns after splitting.
Is there any other way to split the RESULT and show each value in the column and same ROW? In the same result?
Expected result:
Month_name Factor_Matab Rawalpindi_Matab
-----------------------------------------------
feb 2020 25000 78236
mar 2020 26366 82367
As I am new at stack overflow so I don't know how to show this expected result in tabular form
For get your expectation you should to use pivot table pattern:
SELECT
month_name,
-- pivot table
SUM(IF(dimension = 'Factor_Matab', sales, 0)) AS Factor_Matab,
SUM(IF(dimension = 'Rawalpindi_Matab', sales, 0)) AS Rawalpindi_Matab
FROM (
-- Select data grouped by month and dimension as tmp
SELECT
DATE_FORMAT(tran_date, '%M %Y') AS month_name,
d.name AS dimension,
SUM(IF(c.ctype = 4, gl.amount * -1, 0)) AS sales
FROM
gl_trans AS gl,
company_chart_masters AS a,
company_chart_types AS t,
company_chart_classes AS c,
dimensions as d
WHERE (c.ctype = 4 OR c.ctype = 5) AND
d.id = gl.dimension2_id AND
gl.account = a.account_code AND
a.account_type = t.id AND
t.class_id = c.id AND
DIMENSION2_id = gl.dimension2_id
GROUP BY month_name, d.name
) tmp
-- group tmp table by month
GROUP BY month_name
ORDER BY month_name DESC;
Related
My Query Here
http://sqlfiddle.com/#!9/8bcf5a/20/0
SELECT i.id
, i.mon
, i.item
, SUM(v.value) value
FROM items i
LEFT
JOIN values v
ON v.item_id = i.id
WHERE mon = 'Y'
GROUP
BY i.id
ORDER
BY v.value DESC;
I get all the rows with mon = Y, But I only want to get the Highest row with mon = x and value = MAX.
So instead of getting
id mon item value
2 Y C1 17
1 Y C1 11
3 Y C1 5
I want to get
id mon item value
2 Y C1 17
I can use LIMIT 1, But the Query would still fetch all the rows, So it is like hiding the problem.
Because I don't want The query to fetch 1000 row If i had them with mon = Y, But only the row with mon = Y and value = MAX
I've tried something like
AND value = MAX(value)
or
AND value = TOP(value)
But ofcource it seems incorrect overall
just add a LIMIT clause to your query as follows:
SELECT
i.`id`,i.`mon` , i.`item`, SUM(v.`value`) AS value
FROM `items` i
LEFT JOIN `values` v ON v.`item_id` = i.`id`
WHERE mon = 'Y'
GROUP BY i.`id`
ORDER BY v.`value` DESC
LIMIT 1;
that will only return the first row (note I just added the last line).
You're grouping the data on the original value on the table instead of the sum of rhe values. Try this:
SELECT i.id
, i.mon
, i.item
, SUM(v.value) totalValue
FROM items i
LEFT
JOIN values v
ON v.item_id = i.id
WHERE mon = 'Y'
GROUP BY i.id, i.mon , i.item
ORDER BY SUM( v.value) DESC
LIMIT 1;
Try this in your clause:
AND value in (select MAX(value) from values)
intead of
AND value = MAX(value)
and include
AND rownum = 1
I am trying to get this query...
SELECT `Num_1`, COUNT(`Num_1`) AS `value_occurrence` FROM numbers WHERE MONTH(`Dates`) = 1 AND YEAR(`Dates`) = 1995 GROUP BY `Num_1` ORDER BY `value_occurrence` DESC
But for multiple columns as in 'Num_1', 'Num_2', 'Num_3', 'Num_4', 'Num_5' and return with the occurrence of each column such as 'num_1_occurrence', 'num_2_occurrence', 'num_3_occurrence', 'num_4_occurrence', 'num_5_occurrence' and all within the date specified.
numbers table
Example output
I had tried using...
SELECT `Num_1`,`Num_2`, `Num_3`,`Num_4`,`Num_5`,COUNT(`Num_1`,`Num_2`,`Num_3`,`Num_4`,`Num_5`) AS `num_1_occurrence`,`num_2_occurrence`,`num_3_occurrence`, `num_4_occurrence`,`num_5_occurrence`FROM numbers WHERE MONTH(`Dates`) = 1 AND YEAR(`Dates`) = 1995
but just threw errors, I have searched extensively for days and have not found the correct way to do it.
I'd do it like this:
SELECT n.num
, MAX(IF(n.q='n1',n.cnt,NULL)) AS num_1_occurrence
, MAX(IF(n.q='n2',n.cnt,NULL)) AS num_2_occurrence
, MAX(IF(n.q='n3',n.cnt,NULL)) AS num_3_occurrence
FROM (
SELECT 'n1' AS q
, n1.Num_1 AS num
, COUNT(n1.Num_1) AS cnt
FROM numbers n1
WHERE n1.Dates >= '1995-01-01'
AND n1.Dates < '1995-01-01' + INTERVAL 1 MONTH
GROUP BY n1.Num_1
UNION ALL
SELECT 'n2' AS q
, n2.Num_2 AS num
, COUNT(n2.Num_2) AS cnt
FROM numbers n2
WHERE n2.Dates >= '1995-01-01'
AND n2.Dates < '1995-01-01' + INTERVAL 1 MONTH
GROUP BY n2.Num_2
UNION ALL
SELECT 'n3' AS q
, n3.Num_3 AS num
, COUNT(n3.Num_3) AS cnt
FROM numbers n3
WHERE n3.Dates >= '1995-01-01'
AND n3.Dates < '1995-01-01' + INTERVAL 1 MONTH
GROUP BY n3.Num_3
) n
GROUP BY n.num
ORDER BY GREATEST(num_1_occurrence,num_2_occurrence,num_3_occurrence) DESC
I'm only doing Num_1 and Num_2 here, but I think it's what you're looking for, or close. This will give you the list in "tall" format, with the original column name, the value in that column, and the count of that value in that column going across...
SELECT 'Num_1' AS field_name, Num_1 AS value, value_count
FROM (SELECT Num_1, COUNT(Num_1) AS value_count
FROM numbers
GROUP BY Num_1) AS num1_counts
WHERE MONTH(`Dates`) = 1 AND YEAR(`Dates`) = 1995
UNION
SELECT 'Num_2' AS field_name, Num_2 AS value, value_count
FROM (SELECT Num_2, COUNT(Num_2) AS value_count
FROM numbers
GROUP BY Num_2) AS num2_counts
WHERE MONTH(`Dates`) = 1 AND YEAR(`Dates`) = 1995
I'm currently working on a report to highlight payment breakages, this is based on a customer paying in June, but then failing to pay in July.
I've currently got it set up to do an except query, to check one month and compare it to the next. Similar to below(syntax my not be correct as I have had to edit certain data).
DECLARE #StartDatePaid AS DATETIME
DECLARE #EndDatePaid AS DATETIME
DECLARE #StartDateMissed AS DATETIME
DECLARE #EndDateMissed AS DATETIME
SET #StartDatePaid = '01-Oct-2013'
SET #EndDatePaid = '31-Oct-2013'
SET #StartDateMissed = '01-Nov-2013'
SET #EndDateMissed = '05-Dec-2013'
SELECT d.StoreNo
, d.CustNo
FROM (
--Paid Range
SELECT c.CustNo, m.StoreNo
FROM dbo.tblCont AS c INNER JOIN
dbo.tblContDep AS cd ON c.ContractNo = cd.ContractNo INNER JOIN
dbo.tblCust AS m ON c.CustNo = m.CustNo INNER JOIN
dbo.tblTrans AS mx ON m.CustNo = mx.CustNo AND cd.AgendaCode = mx.AgendaCode INNER JOIN
dbo.tblCalender AS cl ON mx.DateEvent = cl.Date
WHERE (cd.Payment > 0) AND (m.Closed <> 'Y') AND (cd.AgendaCode <> 'OPCLIPMT')
AND mx.DateEvent BETWEEN #StartDatePaid AND #EndDatePaid
GROUP BY c.CustNo, m.StoreNo, mx.DateEvent
EXCEPT
--Missed Range
SELECT c.CustNo, m.StoreNo
FROM dbo.tblCont AS c INNER JOIN
dbo.tblContDep AS cd ON c.ContractNo = cd.ContractNo INNER JOIN
dbo.tblCust AS m ON c.CustNo = m.CustNo INNER JOIN
dbo.tblTrans AS mx ON m.CustNo = mx.CustNo AND cd.AgendaCode = mx.AgendaCode INNER JOIN
dtLookups.dbo.tblCalender AS cl ON mx.DateEvent = cl.Date
WHERE (cd.Payment > 0) AND (m.Closed <> 'Y') AND (cd.AgendaCode <> 'OPCLIPMT') AND (mx.DateEvent BETWEEN #StartDateMissed AND #EndDateMissed )
GROUP BY c.CustNo, m.StoreNo, mx.DateEvent
) AS d
WHERE d.StoreNo IN (72, 114, 121, 139, 185, 241, 266)
GROUP BY
d.StoreNo, d.CustNo
I will be switching it over to be based on calendar months instead of date ranges, my question is how am I best generating several months of breakages at once. To get a month on Month comparison at once, as it is I can only get it to create one months breakages based on supplied data.
Example of desired output
Month| breakges
June | 201
July | 189
Aug | 250
Open to suggestions on best practice also or ways to improve.
I admit I don't understand your query. Assuming your breakage is the first occurrence of missing payment, not the subsequent ones. You can produce the desired output like this:
-- prepare your source data
with cte1 as
(
select
user_id,
date, -- representing month by the first day
missed -- bool flag if payment was missed in that month
from ...
)
-- add a sequence number to the source data ordered by date
with cte2 as
(
select *,
row_number() over(partition by user_id order by date) rn
from cte1
)
-- select those records where payment was missed but the previous was ok
,cte3 as
(
select user_id, date from cte2 a
where a.missed = 1
and exists (
select * from cte2 b
where b.missed = 0
and b.uid = a.uid
and b.rn = a.rn -1
)
)
select date, count(*) as breakage from cte3 group by date
Here's the report:
This is how I got the percentages for column the '%Change of most recent year".
=((Last(Fields!Quantity.Value,"Child") - First(Fields!Quantity.Value)) / First(Fields!Quantity.Value))`
= ((54675 - 55968)/55968 ) = -2.31%'
= ((54675 - 57849)/57849) = -5.49%'
It will always take the first year '2012' in this case and get the percentages against each other year. If I enter the years 2005,2004,2003,2002,2001 it will always take the first year and do a percentages against each additional year. 2005 to 2004, 2005 to 2003, 2005 to 2002 and so on. I can have as many as 2 column (year) to many columns.
I need to do it for the Total and Subtotal but it won't work because it's in a different scope.
data is = row Child group
Sub Total: = row Parent group
Total: = row Total group
Year = Column Period group
Query use to get result.
SELECT MEMBERSHIP_CODE
, PERIOD, COUNT(DISTINCT ID) AS Distinct_ID
, SUM(QUANTITY) AS Quantity
, '01-Personal' AS Child
, '01-Overall' AS Parent
, 'Total' as Total
FROM vf_Sshot AS vfs
INNER JOIN vProd AS vP ON vfs.PRODUCT_CODE = vP.PRODUCT_CODE
INNER JOIN vMem_Type vMT on vMT.Member_Type = vfs.Member_Type
WHERE (PERIOD IN ( (SELECT Val from dbo.fn_String_To_Table(#Periods,',',1))))
AND (vMT.MEMBER_TYPE NOT IN ('a','b','c'))
AND (vfs.STATUS IN ( 'A', 'D', 'C'))
AND (MEMBERSHIP_CODE NOT IN ('ABC', 'DEF' ))
and vP.PROD_TYPE in ('DUE','MC','SC')
and vMT.Member_Record = '1'
GROUP BY MEMBERSHIP_CODE, PERIOD
Any ideas?
How would I produce this output?
TOTAL: 57,573 58,941 57,573 61,188 57,573 61,175 57,175
This is the easiest way of solving your problem. In your query, identify the sum for the latest period on a separate column (you can transform your query into a CTE, so that you don't have to change your base query a lot):
WITH query AS (
SELECT MEMBERSHIP_CODE
, PERIOD, COUNT(DISTINCT ID) AS Distinct_ID
, SUM(QUANTITY) AS Quantity
, '01-Personal' AS Child
, '01-Overall' AS Parent
, 'Total' as Total
...
UNION
SELECT
...
)
SELECT
A.MEMBERSHIP_CODE,
A.PERIOD,
A.Distinct_ID,
A.Child,
A.Parent,
A.Total,
A.Quantity,
B.Quantity AS LastPeriodQuantity
FROM
query A INNER JOIN
(SELECT *, ROW_NUMBER() OVER(PARTITION BY MEMBERSHIP_CODE, Distinct_ID, Child, Parent ORDER BY PERIOD DESC) as periodOrder FROM query) B ON
A.MEMBERSHIP_CODE = B.MEMBERSHIP_CODE AND
A.DISTINCT_ID = B.DISTINCT_ID AND
A.Parent = B.Parent AND
A.Child = B.Child AND
A.Total = B.Total AND
B.PeriodOrder = 1
And then on all your totals/subtotals/columns you will be accessing a column that is being grouped/filtered by the same rules than your denominator. Your expression can remain, for all cells, something like this:
=(Fields!LastPeriodQuantity.Value - Fields!Quantity.Value) / Fields!Quantity.Value
With below Query I able to see the count(no) of issues for all issueType in JIRA for a given date .
ie.
SELECT count(*), STEP.STEP_ID
FROM (SELECT STEP_ID, ENTRY_ID
FROM OS_CURRENTSTEP
WHERE OS_CURRENTSTEP.START_DATE < '<your date>'
UNION SELECT STEP_ID, ENTRY_ID
FROM OS_HISTORYSTEP
WHERE OS_HISTORYSTEP.START_DATE < '<your date>'
AND OS_HISTORYSTEP.FINISH_DATE > '<your date>' ) As STEP,
(SELECT changeitem.OLDVALUE AS VAL, changegroup.ISSUEID AS ISSID
FROM changegroup, changeitem
WHERE changeitem.FIELD = 'Workflow'
AND changeitem.GROUPID = changegroup.ID
UNION SELECT jiraissue.WORKFLOW_ID AS VAL, jiraissue.id as ISSID
FROM jiraissue) As VALID,
jiraissue as JI
WHERE STEP.ENTRY_ID = VALID.VAL
AND VALID.ISSID = JI.id
AND JI.project = <proj_id>
Group By STEP.STEP_ID;
the result is
Status Count
open 12
closed 13
..... ....
What I'd like to achieve is something like this actually ..where the total count for status open and closed for each day .
Date COUNT(Open) COUNT(Closed)
12-1-2012 12 1
13-1-2012 14 5
The general strategy would be this:
Select from a table of all the days in a month
LEFT OUTER JOIN your table that gets counts for each day
(left outer join being necessary in case there were no entries for that day, you'd want it to show a zero value).
So I think this is roughly what you need (not complete and date-function syntax is probably wrong for your db, but it will get you closer):
SELECT aDate
, COALESCE(SUM(CASE WHEN IssueStatus = 'whateverMeansOpen' THEN 1 END,0)) OpenCount
, COALESCE(SUM(CASE WHEN IssueStatus = 'whateverMeansClosed' THEN 1 END,0)) ClosedCount
FROM
(
SELECT DATEADD(DAY, I, #START_DATE) aDate
FROM
(
SELECT number AS I FROM [SomeTableWithAtLeast31Rows]
where number between 1 and 31
) Numbers
WHERE DATEADD(DAY, I, #START_DATE) < #END_DATE
) DateTimesInInterval
LEFT OUTER JOIN
(
Put your query here. It needs to output two columns, DateTimeOfIssue and IssueStatus
) yourHugeQuery ON yourHugeQuery.DateTimeOfIssue BETWEEN aDate and DATEADD(DAY, 1, aDate)
GROUP BY aDate
ORDER BY aDate