Get partial NULL when adding 2 SQL (pre 2012) select statements together - mysql

End result values are correct after NULL proving that previous data is summing correctly but showing NULL IN RESULT TABLE.
`(SELECT SUM(Gm1+Gm2+Gm3)
FROM b_Scores L
WHERE Status = "" )
+
(SELECT SUM(Gm1+Gm2)
FROM b_Scores L
WHERE Status = "3A" ) as "RTtl"`
As visible in table, results are controlled by the column "Status"
"3Gm Ttl" values are added together properly but shows up as "NULL".
After Status = "3A" then the results are added and showing correctly
Have tried case statement but cannot format without errors.
For Additional details have included table without code for Status (Absent) = 3A.
Code 3A indicates that Game #3 should not be included in RTotal along with "A" for absent.
Output Table without code for "3A"

Related

Mysql Query Suggestions to get at least master table data

I have two tables for getting push settings
1) Push Type : which defines the type of push and there status (0,1)
2) Push Setting : Having setting for per user
I am running this query :
SELECT `push_type`.*, `push_setting`.`app_id`, `push_setting`.`thumb`,
`push_setting`.`video`, `push_setting`.`text`,
`push_setting`.`notification_status`, `push_setting`.`email`,
`push_setting`.`ios`, `push_setting`.`android`,
`push_setting`.`message`
FROM (`push_type`)
LEFT JOIN `push_setting` ON `push_type`.`id` = `push_setting`.`push_type_id`
WHERE ( (push_setting.app_id is NULL ) or (push_setting.app_id = **130** )) AND `push_type`.`status` = "1"
130 is User id. And because push_settings have entries of 130 , it gives me all the three records .
but if I come with 144 (another user id ) then it do not show anything. i need to have all the 3 active records in every case.
For the First time , it works fine , I mean if there is no entry in push_setting then it gives me 3 records as the join is not successfull.

Add Column in Table with conditional in block WHERE

I was doing a query with MySQL to save all objects returned, but I'd like identify these objects based in statements of the block WHERE, that is, if determined object to satisfy the specific characteristic I'd like create one column and in this column I assignment the value 0 or 1 in the row corresponding the object if it satisfy or not satisfy these characteristic.
This is my script:
SELECT
s.id, al.ID, al.j, al.k, al.r, gal.i
FROM
datas as al
WHERE
AND s.id = al.ID
AND al.j between 1 and 1
AND al.k BETWEEN 15 and 16
AND al.r BETWEEN 67 and 72
The script above is working perfectly and I can to save all objects which it return.
So, I'd like to know if is there a way add in the query above, on block WHERE, the following statement,
( Flags & (dbo.environment('cool') +
dbo.environment('ok') -
dbo.environment('source')) ) = 25
and ((al_pp x al_pp1)-0.5/3=11
and determined the objects that satisfy or not these condition with 0 or 1 in a new column created in Table saved.
I read some tutorials about this and saw some attempts with IF, CASE, ADD COLUMN or WHEN, but none of these solved.
Thanks in advance
MySQL has if function, see here
So you can simply use it in your query:
SELECT IF(( Flags & (dbo.fPhotoFlags('SATURATED') +
dbo.fPhotoFlags('BRIGHT') +
dbo.fPhotoFlags('EDGE')) ) = 0
and petroRad_r < 18
and ((colc_u - colc_g) - (psfMag_u - psfMag_g)) < -0.4
, 1 --// VALUE IF TRUE
, 0 --// VALUE IF FALSE
) as conditional_column, ... rest of your query

SQL Query, Subquery won't return data

Problem: My Query returns NULL
Potential issues: Subquery formatted wrong and only works if some data I want to sort out is in the dataset.
Example of today's code:
SELECT production_order
,SUM(total_working_time_h) - (SELECT SUM(total_working_time_h) FROM {$table} WHERE production_order = '$production_order' AND (station = '出货检验 | OQC' OR production_type = 'Rework')) AS total_working_time_h_edit
,SUM(no_of_defects) AS no_of_defects_during_production
FROM {$table}
WHERE production_order = '$production_order'
This works great as long as I have either "Rework" and/or "'出货检验 | OQC'" logged in my database for this production order. If not, I won't get any data at all but NULL. So my problem is somewhere in my subquery I think:
SELECT SUM(total_working_time_h) FROM {$table} WHERE production_order = '$production_order' AND (station = '出货检验 | OQC' OR production_type = 'Rework')
I've tried adding a "0" to make sure it's get 0 and not NULL without success like follows:
(0 + SELECT SUM(total_working_time_h) FROM {$table} WHERE production_order = '$production_order' AND (station = '出货检验 | OQC' OR production_type = 'Rework')
Example dataset formated as text:
production_order part_nr station total_working_time_h
26135 129-108816B-UL 压接 | Crimping 1.42
26135 129-108816B 线束组装 | Harness Assembling 7.67
26135 129-108816 测试 | Testing 0.83
26135 129-108816B 外观全检 | Appearance inspection 0.83
Gives this output:
production_order,total_working_time_h_edit
26135, NULL
I want this output:
production_order,total_working_time_h_edit
26135, 10,45
If my dataset hold the station I want to sort out or the production_type I want to sort out everything works as it should. But if both of those is missing in the dataset it returns NULL, since what I think is because the subquery returns 0.
An example of different datasets I'm using. If I have the data marked with green cells in the dataset it will work. If not, I get the return NULL as above.
So how to make my subquery to not return NULL?
Managed to solve the question myself.
Problem: Subquery returned NULL instead of 0.
Solution: Added COALESCE(,0) to my sub querys function SUM() like this:
COALESCE(SUM(total_working_time_h),0)
By doing so it returns 0 instead of NULL when the SUM() actually wants to return NULL. That solved my problem.

AS statement in SQL with the value of another cell

I'm having an issue with this SQL query, though I think it's mainly a syntax issue.
"SELECT grantYear, grantAmount AS /?\
FROM granttoassociation
JOIN grantprovider ON granttoassociation.grantProvider = grantprovider.id
WHERE grantReceiver = ? "
I would like the grantAmount column to be returned under the name of the corresponding grantProvider, which can be found under grantProvider.name column in the grantprovider table.
(so in the end there would be one column returned for each different grantProvider.name)
So what should I put instead of the /?\ in my code ? grantProvider.name doesn't seem to be working.
EDIT
SELECT grantYear, grantAmount, grantprovider.name
FROM granttoassociation JOIN grantprovider
ON grantProvider = grantprovider.id
WHERE grantReceiver = 2891
Result:
grantYear grantAmount name
2009 3000 besancon
2010 1000 besancon
2011 0 besancon

SSRS Multiple Field Values Not Passing As Parameters To Drilldown Report

I have not found a suitable answer to my following problem after much searching.
I have a summary report which shows the total number of appointments by month grouped by specialty name and diagnosis.
Specialty_Name Diagnosis Code Feb Mar Apr
Neurology G35X 0 3 4
G379 8 5 7
Total For Spec 8 8 11
Rheumatology H051 4 9 2
M059 6 10 3
Total For Spec 10 19 5
When I click on the field (ie 11 for Neurology Total For Spec, Apr), which is =COUNT((Fields!Appointment_ID.Value), I want to pass each of the Appointment_IDs to a sub query as the parameter to display the associated client details. However, when I prepare the sub query and drill down report in a manner that I have used before I am only getting the result of the first Appointment_ID; not each one.
The sub query report is set up with the parameter #Appointment_ID VARCHAR(MAX) and a WHERE clause:
CAST(App.App_ID AS VARCHAR(MAX)) in (
SELECT * FROM Serve1.dbo.CreateParameterTable(#Appointment_ID,',')
)
The CreateParameterTable is a function on our server which should handle the string from the summary report and does so on other reports.
(
#StringInput NVARCHAR(MAX)
, #SplitBy NCHAR(1)
)
RETURNS #OutputTable TABLE ( [String] NVARCHAR(36) )
AS
BEGIN
DECLARE #String NVARCHAR(36)
WHILE LEN(#StringInput) > 0
BEGIN
SET #String = LEFT(#StringInput,
ISNULL(NULLIF(CHARINDEX(#SplitBy, #StringInput) - 1,
-1), LEN(#StringInput)))
SET #StringInput = SUBSTRING(#StringInput,
ISNULL(NULLIF(CHARINDEX(#SplitBy, #StringInput),
0), LEN(#StringInput))
+ 1, LEN(#StringInput))
INSERT INTO #OutputTable ( [String] )
VALUES ( #String )
END
If I manually type in multiple Appointment_IDs to my drill down report I get the expected result. Therefore it appears that either my Summary Report is not passing out a string or it is not being handled correctly by the function or the sub report does not like the output of the function Which as I have said works on other reports we have written. I'm stumped.
How to begin troubleshooting your specific problem
Because you've said you can get the subreport working by supplying a string of values as desired, then it appears the problem is with the report providing the delimited string. Before you can fix the issue, you need to see the delimited string being passed to the subreport - so add it to the tablix. Use the same function you're using to reference the value in the "Go to Report" action and add it as a tooltip to each count. That way you can see the string being prepared and tweak your logic until it comes out correctly.
How I would solve your problem
What you're trying to accomplish as a multi-valued parameter is, as I see it, unnecessarily complicated.
In your tablix, you have a set way of organizing the results. You have specialty name, diagnosis, and month (presumably of the diagnosis or date served).
That's three parameters you can pass through to a sub-report. Using similar sql logic as exists in your main report, you can isolate those 11 simply by knowing the values of those three fields plus the year. Obviously, you will need to do some work to find diagnosis/service dates that fall into a given month, but that's not too hard:
Where Month(Date_of_Service) = #Month
and Year(Date_of_Service) = #Year
and Specialty_Name = #Specialty
and Diagnosis_Code = #Diagnosis