I have written a query in mysql as follows:
SELECT inquiry_id, event_name,MAX(ValueData1) as ValueData1
,MAX(ValueData2) as ValueData2
,MAX(ValueData3) as ValueData3
,MAX(ValueData4) as ValueData4
FROM
(
SELECT inquiry_id,event_id,event_name
,CASE WHEN mailer_id = 1 THEN CONCAT( Edition,' - ',sent_on) END AS ValueData1
,CASE WHEN mailer_id = 2 THEN CONCAT( Edition,' - ',sent_on) END AS ValueData2
,CASE WHEN mailer_id = 3 THEN CONCAT( Edition,' - ',sent_on) END AS ValueData3
,CASE WHEN mailer_id = 4 THEN CONCAT( Edition,' - ',sent_on) END AS ValueData4
FROM crm_support_inquiry
) AS crm_support_inquiry
GROUP BY inquiry_id,event_id,event_name
The output of which is as follows:
When i run the query and try to display the data in grid view i get only two fields in the output i.e. inquiry_id and event_name i.e. first two fields. Remaining fields are not getting displayed. What is to be done? i want full query output to be displayed in the grid view.
Related
I have a SQL query in teradata that returns a results set of ~160m rows in (I guess) a reasonable time: dependent on how good a day the server is having it runs between 10-60 minutes.
I recently got access to space to save it as a table, however using my initial query and the "insert into " command I get error 2646-no more spool.
query structure is
insert into <test_DB.tablename>
with smaller_dataset as
(
select
*
from
(
select
items
,case items
from
<Database.table>
QUALIFY ROW_NUMBER() OVER (PARTITION BY A,B ORDER BY C desc , LAST_UPDATE_DTM DESC) = 1
where 1=1
and other things
) T --irrelevant alias for subquery
QUALIFY ROW_NUMBER() OVER (PARTITION BY A, B ORDER BY C desc) = 1)
, employee_table as
(
select
items
,max(J1.field1) J1_field1
,max(J2.field1) J2_field1
,max(J3.field1) J3_field1
,max(J4.field1) J4_field1
from smaller_dataset S
self joins J1,J2,J3,J4
group by
non-aggregate items
)
select
items
case items
from employee_table
;
How can I break up the return into smaller chunks to prevent this error?
Thanks again for the help everyone. I went with the script below...
SELECT beginning, end,
(SELECT SUM(sale) FROM sales_log WHERE date BETWEEN beginning AND `end` ) AS sales
FROM performance
and I added a salesperson column to both the performance table and sales_log but it winds up crashing DB Browser. What is the issue here? New code below:
SELECT beginning, end, salesperson
(SELECT SUM(sale) FROM sales_log WHERE (date BETWEEN beginning AND end) AND sales_log.salesperson = performance.salesperson ) AS sales
FROM performance
I believe that the following may do what you wish or be the basis for what you wish.
WITH sales_log_cte AS
(
SELECT substr(date,(length(date) -3),4)||'-'||
CASE WHEN length(replace(substr(date,instr(date,'/')+1,2),'/','')) < 2 THEN '0' ELSE '' END
||replace(substr(date,instr(date,'/')+1,2),'/','')||'-'||
CASE WHEN length(substr(date,1,instr(date,'/') -1)) < 2 THEN '0' ELSE '' END||substr(date,1,instr(date,'/') -1) AS date,
CAST(sale AS REAL) AS sale
FROM sales_log
),
performance_cte AS
(
SELECT substr(beginning,(length(beginning) -3),4)||'-'||
CASE WHEN length(replace(substr(beginning,instr(beginning,'/')+1,2),'/','')) < 2 THEN '0' ELSE '' END
||replace(substr(beginning,instr(beginning,'/')+1,2),'/','')||'-'||
CASE WHEN length(substr(beginning,1,instr(beginning,'/') -1)) < 2 THEN '0' ELSE '' END||substr(beginning,1,instr(beginning,'/') -1)
AS beginning,
substr(`end`,(length(`end`) -3),4)||'-'||
CASE WHEN length(replace(substr(`end`,instr(`end`,'/')+1,2),'/','')) < 2 THEN '0' ELSE '' END
||replace(substr(`end`,instr(`end`,'/')+1,2),'/','')||'-'||
CASE WHEN length(substr(`end`,1,instr(`end`,'/') -1)) < 2 THEN '0' ELSE '' END||substr(`end`,1,instr(`end`,'/') -1)
AS `end`
FROM performance
)
SELECT beginning, `end` , (SELECT SUM(sale) FROM sales_log_cte WHERE date BETWEEN beginning AND `end` ) AS sales
FROM performance_cte
;
From your data this results in :-
As can be seen the bulk of the code is converting the dates into a format (i.e. YYYY-MM-DD) that is usable/recognisable by SQLite for the BETWEEN clause.
Date And Time Functions
I don't believe that you want a join between performance (preformance_cte after reformatting the dates) and sales_log (sales_log_cte) as this will be a cartesian product and then sum will sum all the results within the range.
The use of end as a column name is also awkward as it is a KEYWORD requiring it to be enclosed (` grave accents used in the above).
The above works by using 2 CTE's (Common Table Expresssions), which are temporary tables who'd life time is for the query in which they are used.
The first sales_log_cte is simply the sales_log table but with the date reformatted. The second, likewise, is simply the performace table with the dates reformatted.
If the tables already has suitable date formatting then all of the above could simply be :-
SELECT beginning, `end` , (SELECT SUM(sale) FROM sales_log WHERE date BETWEEN beginning AND `end` ) AS sales FROM performance;
I have two tables, vessel_details and vessel_history. Both tables have the same fields. I have to compare the values of all the fields and select the column with the different value and not null. For example the field of ship_name, i have to check if the value of the field ship_name from the vessel_details table is different with the ship_name in vessel_history table. If the value is different and not null, i have to select and display the value from the vessel_history.
Any ideas for the right query?
thanks.
SELECT vessel_details.<your_id>, 'Difference in Column ship_name for ship_name = ' + vessel_history.ship_name
FROM vessel_details
JOIN vessel_history ON vessel_details.<your_id> ON vessel_history.<id_from_vessel_details>
WHERE vessel_details.ship_name <> vessel_history.ship_name
UNION
SELECT vessel_details.<your_id>, 'Difference in Column ship_model for ship_model = ' + vessel_history.ship_model
FROM vessel_details
JOIN vessel_history ON vessel_details.<your_id> ON vessel_history.<id_from_vessel_details>
WHERE vessel_details.ship_model <> vessel_history.ship_model
.
.
.
And so on for all the columns you want to check.
Ist that what you are looking for?
EDIT for one row per item:
SELECT vessel_details.<your_id>,
CASE WHEN vessel_details.ship_name <> vessel_history.ship_name THEN Convert(bit, 1) ELSE Convert(bit, 0) END AS ship_name_different,
CASE WHEN vessel_details.ship_model <> vessel_history.ship_model THEN Convert(bit, 1) ELSE Convert(bit, 0) END AS ship_model_different
FROM vessel_details
JOIN vessel_history ON vessel_details.<your_id> ON vessel_history.<id_from_vessel_details>
WHERE vessel_details.ship_name <> vessel_history.ship_name
OR vessel_details.ship_model <> vessel_history.ship_model
I need to manage hierarchy data storing in my database. But I have a problem now. Please see my example
I have a table called COMMON.TASK_REL
My second table is called Common. task
I suppose need to sort the task_seq and return a result like below:
Task Name || Task_Seq
Item1 1
..Item1.2 1
...Item1.2.1 1
..Item1.1 2
Here is my query
--Common task SQL modify --
WITH ctLevel
AS
(
SELECT
C_TASK_ID AS Child
,P_Task_ID AS Parent
,common_task.TASK_SEQ AS taskOrder
,1 AS [Level]
,CAST(C_TASK_ID AS VARCHAR(MAX)) AS [Order]
,CAST (Replicate('.', 1) + common_task.TASK_NAME AS VARCHAR(25)) AS [Task_Name]
FROM
[COMMON.TASK_REL] as common_task_rel,
[COMMON.TASK] as common_task
WHERE common_task_rel.C_TASK_ID = common_task.TASK_ID
and common_task.[TASK_TYPE] = 'B' AND common_task.[MODULE_CODE] = 'LWRPT'
AND common_task.[STATUS] <> 'D'
UNION ALL
SELECT
C_TASK_ID AS Child
,P_Task_ID AS Parent
,common_task.TASK_SEQ AS taskOrder
,[Level] + 1 AS [Level]
,[Order] + '.' + CAST(C_TASK_ID AS VARCHAR(MAX)) AS [Order]
,CAST (Replicate('.', [Level] + 1) + common_task.TASK_NAME AS VARCHAR(25)) AS [Task_Name]
FROM [COMMON.TASK_REL] as common_task_rel
INNER JOIN ctLevel
ON ( P_Task_ID = Child ) , [COMMON.TASK] as common_task
WHERE common_task_rel.C_TASK_ID = common_task.TASK_ID
and common_task.[TASK_TYPE] = 'B' AND common_task.[MODULE_CODE] = 'LWRPT'
AND common_task.[STATUS] <> 'D'
)
-- Viewing Data
SELECT Child ,Parent ,taskOrder,Level,[Order],Task_Name
FROM ctLevel
GROUP BY Child ,Parent ,taskOrder,Level,[Order],Task_Name
order by [Order];
GO
But my result returns duplicated rows:
Anyone can help me correct my query? Thanks
I believe that your duplicates are coming from your root/anchor query. You should add the following to that query:
AND Task_Seq = 0
Basically, you only want the root to be set up as the beginning of the tree. 301|300 should not be picked up until the recursion section (the part after union all)
If that does not make sense, then I can repaste your query with the modification, but that seemed unnecessary for a small change.
SELECT T.GLTR_COMP_CODE,
T.GLTR_ACCT_CODE,
T.GLTR_DEPT_NO,
M.GLMA_ACCT_NAME ,
CAST(T.GLTR_PSTNG_TYPE AS VARCHAR) + CAST(T.GLTR_PSTNG_NO AS VARCHAR) REF_NO,
CAST(T.GLTR_DOC_CODE AS VARCHAR) + CAST(T.GLTR_OUR_DOC_NO AS VARCHAR) DOC_NO,
T.GLTR_DOC_DATETIME DOC_DATE,
T.GLTR_DOC_NARR NARRATIVE,
T.GLTR_PSTNG_DATETIME,
CASE SIGN(T.GLTR_TRAN_AMT) WHEN + 1 THEN T.GLTR_TRAN_AMT ELSE 0 END DEBIT,
CASE SIGN(T.GLTR_TRAN_AMT) WHEN - 1 THEN T.GLTR_TRAN_AMT ELSE 0 END CREDIT,
T.GLTR_TRAN_AMT AMOUNT,
T.GLTR_FC_CODE FC_CODE,
T.GLTR_FC_AMT FC_AMOUNT
FROM GLAS_GL_TRANSACTIONS T
LEFT OUTER JOIN GLAS_GL_MASTERS M ON M.GLMA_COMP_CODE = T.GLTR_COMP_CODE
AND M.GLMA_ACCT_CODE = T.GLTR_ACCT_CODE
AND M.GLMA_DEPT_NO = T.GLTR_DEPT_NO
WHERE T.GLTR_PSTNG_DATETIME BETWEEN COALESCE(#DATE_FROM, T.GLTR_PSTNG_DATETIME)
AND COALESCE(#DATE_TO, T.GLTR_PSTNG_DATETIME)
AND T.GLTR_COMP_CODE =#COMP_CODE
AND M.GLMA_YEAR = CONVERT(NUMERIC(8, 2), DATEPART(YYYY, #DATE_FROM))
AND M.GLMA_ACCT_CODE BETWEEN COALESCE(#ACCT_CODE_FROM, M.GLMA_ACCT_CODE)
AND COALESCE(#ACCT_CODE_TO, M.GLMA_ACCT_CODE)
ORDER BY T.GLTR_ACCT_CODE, T.GLTR_PSTNG_DATETIME, T.GLTR_DOC_CODE, T.GLTR_OUR_DOC_NO
This is my query in dataset...how can i view this in crystal report ......some of fields above have concatenate how can i display?
As long as you have created the dataset in your report you should be able to add the field to the report and view it.
The one thing I notice is that you do not have alias on your concatenated fields which may cause problems when designing your report.