My SQL query returns:
orderhed_pino_c OrderHed_OrderNum OrderDtl_OrderLine calculated_totalsqm
19.0503 50291 1 1.6359
19.0503 50291 1 1.6359
19.0503 50291 2 1.59244
19.0503 50291 2 1.59244
19.0503 50292 1 28.0476
19.0503 50290 1 3.2718
19.0503 50288 1 7.418808
19.0503 50288 1 7.418808
19.0503 50288 1 7.418808
19.0503 50290 3 1
19.0503 50288 1 7.418808
19.0503 50288 1 7.418808
19.0503 50288 1 7.418808
19.0503 50290 4 38.868
19.0503 50288 1 7.418808
19.0503 50288 1 7.418808
19.0503 50288 1 7.418808
In SSRS I have grouped by OrderHed_OrderNum and OrderDtl_OrderLine
Please see the image.
I want to have total calculate_totalsqm for orderhed_ordernum .
but I am getting the total of all rows.
For orderhed_ordernum = 50291 I have two orderdtl_orderline 1 and 2 so total should be 1.6359 + 1.59244 = 3.22834
but SSRS is showing 6.45.
I have dataset query as:
SELECT Sum(t1)
FROM (
SELECT [orderhed_ordernum] AS T2
, Avg([calculated_totalsqm]) AS T1
, [orderdtl_orderline] AS T3
FROM dbo.[baqreportresult_" + parameters!tableguid.value + "]
GROUP BY [orderhed_ordernum], [orderdtl_orderline]
) BB
GROUP BY t2
but I am getting error as:
Program Ice.Services.Lib.RunTask raised an unexpected exception with the following message: RunTask: System.Web.Services.Protocols.SoapException: An error has occurred during report processing. ---> Microsoft.ReportingServices.ReportProcessing.ProcessingAbortedException: An error has occurred during report processing. ---> Microsoft.ReportingServices.ReportProcessing.ReportProcessingException: Query execution failed for dataset 'TotalSQM'. ---> System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'By'.****
If you want to use parameter values to alter the text of your Sql statement (i.e. to provide database, table or field names), then turn the whole Sql statement into an expression. Right-click the dataset, click Properties and click the fx button beside the Sql statement to edit it and turn it into a string expression. Enter the following:
="SELECT Sum(t1) "
&"FROM ( "
&" SELECT [orderhed_ordernum] AS T2 "
&" , Avg([calculated_totalsqm]) AS T1 "
&" , [orderdtl_orderline] AS T3 "
&" FROM dbo.[baqreportresult_" & Parameters!tableguid.value & "] "
&" GROUP BY [orderhed_ordernum], [orderdtl_orderline] "
&") BB "
&"GROUP BY t2 "
So this just encapsulates the entire Sql in quotes, turning it into a string expression, and inserts the parameter value into the Sql statement to calculate the right table name at run time. The Sql statement is built from the string expression at run time, creating your desired table name from your tableguid parameter and then executing that against the database.
Note the spaces at the end of each line which are needed as this will result in one line of Sql.
Note also that string concatentation uses & rather than +.
Related
i have two tables
tb1
tb1_id - store_ids - date
1 - 1,2,3,4 - 2023-01-01
2 - 3,4 - 2023-06-01
tb2
tb2_id - name - date
1 - gold - 2023-01-01
2 - mond - 2023-01-01
3 - burgar - 2023-01-01
4 - glass - 2023-01-01
5 - blackD - 2023-01-01
what i have tried is
sql
SELECT *
FROM `tb2`
JOIN `tb1`
WHERE `tb2_id` IN (`store_ids`)
and i get error
'Warning: #1292 Truncated incorrect INTEGER value: 1,2,3,4'
You can use find_in_set
select * from tb1 join tb2 on find_in_set(tb2_id ,tbl1_id)
But as I mentioned in my earlier comment, it is better to redesign your table
DEMO
You could try below query
SELECT *
FROM `tb2`
JOIN `tb1`
WHERE `store_ids` REGEXP CONCAT('[[:<:]]',`tb2_id`,'[[:>:]]') -- MySQL 5.6
-- For MySQL 8.0, using WHERE `store_id` REGEXP CONCAT('\\b',`tb2_id`,'\\b')
But it's better to not store foreign keys as list of ids separated by comma.
In SQL Server 2008 I find the first available slot in a table with the following stored procedure.
If Exists ( Select * From Methods Where MethodSerno =1 )
Select #SlotCode = Min(MethodSerno) + 1
From Methods
Where MethodSerno + 1 Not In ( Select MethodSerno From Methods )
Is there an equivalent way in MS Access?
Thanks in adavance...
For a table named [Methods] containing rows
MethodSerno
-----------
1
2
4
5
8
10
we can start with a query to find each value that does not have an immediate successor (n+1)
SELECT t1.MethodSerno
FROM
Methods t1
LEFT JOIN
Methods t2
ON t1.MethodSerno + 1 = t2.MethodSerno
WHERE t2.MethodSerno IS NULL
returning
MethodSerno
-----------
2
5
8
10
The "first available" value will simply be (the smallest of those values)+1
SELECT MIN(t1.MethodSerno) + 1 AS NextSerno
FROM
Methods t1
LEFT JOIN
Methods t2
ON t1.MethodSerno + 1 = t2.MethodSerno
WHERE t2.MethodSerno IS NULL
returning
NextSerno
---------
3
I have a MySQL database with a table of survey responses with three important fields (renamed for clarity): SURVEY_TAKER_ID, QUESTION_NUMBER, and RESPONSE. Assuming a 3-question survey as an example, it would look something like this:
SURVEY_TAKER_ID | QUESTION_NUMBER | RESPONSE
----------------------------------------
101 1 Apple
102 1 Orange
103 1 Banana
101 2 Morning
102 2 Evening
103 2 Afternoon
101 3 Red
102 3 Blue
103 3 Yellow
I would like to create a SELECT query that outputs each survey taker's ID and responses in order by question number, e.g.:
101,Apple,Morning,Red
102,Orange,Evening,Blue
103,Banana,Afternoon,Yellow
I know that SQL Server has FOR XML, which can make this easier, but my database is in MySQL, and I must admit that I'm not really all that adept at SQL in the first place. Can anyone please give me a sample query that would produce the output above, or point me to a way to do it?
Many thanks to anyone who can help...
...Jay
SURVEY_TAKER_ID with "Question_Number: RESPONSE" style concating:
SQL Fiddle
SELECT SURVEY_TAKER_ID
,GROUP_CONCAT(CONCAT(QUESTION_NUMBER, ': ', RESPONSE) ORDER BY QUESTION_NUMBER SEPARATOR ', ') AS RESPONSES
FROM Table1
GROUP BY SURVEY_TAKER_ID
ORDER BY SURVEY_TAKER_ID
.
SURVEY_TAKER_ID with "RESPONSE" alone style concating:
SQL Fiddle
SELECT SURVEY_TAKER_ID
,GROUP_CONCAT(RESPONSE ORDER BY QUESTION_NUMBER SEPARATOR ', ') AS RESPONSES
FROM Table1
GROUP BY SURVEY_TAKER_ID
ORDER BY SURVEY_TAKER_ID
Try this:
SELECT CONCAT(SURVEY_TAKER_ID, ',', RESPONSE)
FROM (SELECT SURVEY_TAKER_ID, GROUP_CONCAT(RESPONSE) RESPONSE
FROM (SELECT * FROM SURVEY ORDER BY SURVEY_TAKER_ID, QUESTION_NUMBER) A
GROUP BY SURVEY_TAKER_ID
) A
Is it possible to flatten out a database table for a report using sql (mysql). I can do with with php but it would be nice if I could do it simpler in sql.
example:
1) input: raw data - one line per cust/start & end time
cust start end
8000 0900 1000
8000 1000 1100
8000 1200 1300
9000 0900 1000
9000 1000 1100
2) required output: data flattened out - one line per customer with all start & end times following on same line
cust all related start-end times
---- ------------------------------
8000 0900-1000 1000-1100 1200-1300
9000 0900-1000 1000-1100
This is what you are looking for:
select cust, group_concat(concat(start,'-',end) SEPARATOR ' ')
from mytable
group by cust
See the working fiddle.
A SQL query has to have a fixed number of columns. This poses a problem in your case, because you don't know how many columns you need.
One workaround is to put all the results in one column. This produces a result like '090-1000,1000-1100,1200-1300'. You can do this with group_concat():
select cust,
group_concat(concat(start, '-', end) separator ', ' order by start) as StartEnds
from t
group by cust
I have query in MS Access that produces a correct records result, but Access refuses to run the Query as a delete query?
Can any one help me rewrite this query to run in access.
Delete Table_A.*
FROM (SELECT Table_A.Main_RecID, Table_A.Fld_Unique_ID, Table_A.Actiontaken FROM Table_A
WHERE Table_A.Actiontaken="MainRecord deleted") AS Tmp_B
LEFT JOIN Table_A ON Tmp_B.Main_RecID=Table_A.Main_Recid
WHERE (((Table_A.Actiontaken)<>"MainRecord deleted"));
If the "Delete" is replaced by a select or I ask for a datasheet view the Query produces what I would expect. Which is a list of the records in the table that have the same Main_RecID as records with Actiontaken field = "MainRecord deleted" but do not have their Actiontaken field equal to "MainRecord deleted".
Access responds with the message "Could not delete from specified tables."
I started with this data in Table_A ...
Fld_Unique_ID Main_RecID Actiontaken
1 1 MainRecord deleted
2 1
3 2 MainRecord deleted
4 2 something else
5 3 something else
Note Actiontaken is Null in second row (where Fld_Unique_ID = 2).
Executing the DELETE statement below leaves this data in the table.
Fld_Unique_ID Main_RecID Actiontaken
1 1 MainRecord deleted
3 2 MainRecord deleted
5 3 something else
DELETE
FROM Table_A
WHERE
(
Actiontaken<>'MainRecord deleted'
OR Actiontaken Is Null
)
AND
(
DCount("*", "Table_A",
"Main_RecID = " & Main_RecID
& " AND Actiontaken='MainRecord deleted'") > 0
);
If that's not what you're after, please show us sample data before and after DELETE.