I have a table with 3 columns: "Dept Number" and "Class Number" and "Description". If the user enter values 21-1 and 24-12 where the left term represents the department number and right term represents the class number, I want to search the table for those items based on its dept number and class number (Here, dept# = 21 and class# = 1) and displays its description. How can I write the SQL query for that?
Note: the user can enter more than 1 value. So, I am thinking about making 2 lists: 1 for dept# and other for class#. But, I am not sure how to search the database using these lists
Here is a code example for SQL Server and Oracle.
SQL Server:
SELECT *
FROM yourTable
WHERE dept = CAST(SUBSTRING(N'21-1',1,CHARINDEX(N'-',N'21-1')-1) as int)
AND class = CAST(SUBSTRING(N'21-1',CHARINDEX(N'-',N'21-1')+1, LEN(N'21-1')) as int)
Oracle
SELECT *
FROM yourTable
WHERE dept = CAST(SUBSTR(N'21-1',1,CHARINDEX(N'-',N'21-1')-1) as int)
AND class = CAST(SUBSTR(N'21-1',CHARINDEX(N'-',N'21-1')+1, LENGTH(N'21-1')) as int)
Please tag your question correctly. I hope this helps you.
Related
I have the following query:
SELECT "IMPORTACIONCOLUMN3", "role", COUNT("IMPORTACIONCOLUMN1")
FROM MYTABLE
WHERE ID = 9
GROUP BY "IMPORTACIONCOLUMN3", "role"
This gives me the following result:
I would like to achieve the following:
The "unique" values are grouped together (i.e. Instead of having 4 values of "Robot 1" these are grouped togehter in just 1 cell summing the count values.
The second group by or subquery has to be the same count, but with role instead of "IMPORTACIONCOLUM3"
Is it possible (for the second picture) to "link" the values either by index or adding an extra column to reference them (i.e. There's two "Solicitante" with a count value of "52" but it refers to "Robot 1" and other to "Solicitante" with count value of "58" links to "Robot 2"
The second image represent visually what I'm trying to explain.
I have been trying on my own but only have reached the following:
select "IMPORTACIONCOLUMN3", count("IMPORTACIONCOLUMN1")
from
(
select "IMPORTACIONCOLUMN1", count("role"), "IMPORTACIONCOLUMN3"
from MYTABLE
WHERE ID = 9
group by "IMPORTACIONCOLUMN1", "IMPORTACIONCOLUMN3"
) as tmp
group by "IMPORTACIONCOLUMN3"
But it is not yet the result I am looking for.
Thanks in advance for your help and tips!
EDIT:
Explaining my desired output in detail
Each one of "Robot 1, 2, 3" have roles such as "Solicitante", "Gerente", etc. with different values.
i.e. The first row "Humano" value "243" is the sum of "Agente de Compras - 95", "Gerente Financiero - 37", "Gerente Solicitante - 45", "Proovedor - 31", "Solicitante - 60".
I am linking these by the column "GRAFICOCOLUMNARECURSIVOID" with contains the index of whatever "Robot" these "roles" are from.
I would like to achieve a query containing subquerys that allows me to have this output.
Try this for question number 1:
SELECT "IMPORTACIONCOLUMN3", COUNT("IMPORTACIONCOLUMN1")
FROM MYTABLE
WHERE ID = 9
GROUP BY "IMPORTACIONCOLUMN3"
the problem is Role: Robot 1 have 4 roles
and this for question 2:
SELECT "role", COUNT("IMPORTACIONCOLUMN1")
FROM MYTABLE
WHERE ID = 9
GROUP BY "role"
Question 3, I don't understand what you are asking for. Please make an example.
I don't know know how to explain it better so let me give you my example.
This is the code I have:
SELECT nome,idc,nome_local,idade
FROM ciclista, local,parente
WHERE idade < 18
AND nome_local LIKE '%Covil%'
AND ciclista.idl_residencia=local.idl
AND ciclista.idc=parente.idc_b
AND parente.tipo LIKE 'm%'
I need to display another name (nome from ciclista where ciclista.idc=parente.ida_a).
Basically I want to display the parent's name infront of the results.
You can use the same table twice just by giving it another name. For getting the sons you name the table c1, for getting the parents you name it c2, then just apply the condition that you mentioned (ciclista.idc = parente.ida_a, e.g., c2.idc = parente.ida_a). The names can be whatever you want as long as they allowed names
SELECT c1.nome, c1.idc, nome_local, c1.idade, c2.nome
FROM ciclista c1, ciclista c2, local, parente
WHERE c1.idade < 18
AND nome_local LIKE '%Covil%'
AND c1.idl_residencia = local.idl
AND c1.idc = parente.idc_b
AND c2.idc = parente.ida_a
AND parente.tipo LIKE 'm%'
Wondering if anyone can help me, I am currently learning, SRSS Reports and have been tasked with a Pay & Display vehicle registration report, for staff and student carparks, to show if they are registered for P&D. I have the report laid out as a 3 table column; Name, Vehicle Number and Vehicle Registration. I have from the below code set out in my datasets the Vehicle Number and Vehicle Registrations pulling through, with a parameter to filter out if is staff or student pay and display. However I am having difficulty with pulling the Staff and Student names into one name column as they are through two different datasets. Can anyone be of help at all please? TIA
**-- Pay & Display Carpark Details Dataset**
SELECT * FROM Carpark_Vehicles
WHERE VehicleCustNo IN (SELECT CustID FROM [dbo].[Carpark_Customer]
WHERE CarParkID IN (SELECT CarParkID FROM [dbo].[Carpark_CarPark]
WHERE CTypeID IN ('PD', 'SPD')))
--and CTypeID IN (#Carpark_type))
**--Vehicle Details Dataset**
SELECT
Carpark_Vehicles.VehicleID
,Carpark_Vehicles.RegNo
,VehicleCustNo
FROM
Carpark_Vehicles
WHERE CarPark_Vehicles.RegNo = #RegNo
**--Staff details dataset**
SELECT
StaffDetails_StaffandLeaverDetails.StaffNumber
,StaffDetails_StaffandLeaverDetails.LastName
,StaffDetails_StaffandLeaverDetails.Name1
FROM
StaffDetails_StaffandLeaverDetails
**--Student details dataset**
SELECT
Student_CurrentStudentDetails.StudentNumber
,Student_CurrentStudentDetails.StudentID
,Student_CurrentStudentDetails.LastName
,Student_CurrentStudentDetails.FirstName
FROM
Student_CurrentStudentDetails
**--Carpark permit type (Pay & Display) parameter**
SELECT DISTINCT
CTypeID, CASE CTypeID WHEN 'abc' THEN 'Students' WHEN 'abc' THEN 'Staff' END AS Label
FROM
Carpark_Customer
WHERE CTypeID IN ('abc', 'Abc')
For your req I just took some sample data. This entire example will give you Idea how to achieve your final result.
Note: this is just showing you way how to achieve it, you have to modify as per your req.
Student and Staff Info from 2 different Tables
Registration Info from another separate Table
Your Final Result when Student as parameter is selected
Your Final Result when Staff as parameter is selected
Now how did I achieved this?
My Design for Final Result
Row visibilty expression for your final result Table in SSRS
Expressiion as below
=IIF(Parameters!ReportParameter1.Value=1,
IIF(Isnothing(Lookup(Fields!PersonId.Value,Fields!PersonId.Value,Fields!Name.Value, "DSStudentInfo")),true,false),
IIF(Isnothing(Lookup(Fields!PersonId.Value,Fields!PersonId.Value,Fields!Name.Value, "DSStaffInfo")),true,
false)
)
first Name column visibility
second column name visibility
First Name data value expression
=Lookup(Fields!PersonId.Value,Fields!PersonId.Value,Fields!Name.Value, "DSStudentInfo")
second name data value expression
=Lookup(Fields!PersonId.Value,Fields!PersonId.Value,Fields!Name.Value, "DSStaffInfo")
I am using Visual Studio 2017 and I installed the latest crystal reportviewer (22)
What I want is to click a button and create a report from the customer that is selected in the datagridview and the addresses that are shown in the second datagridview.
I managed to do all that but the problem is that a few fields contain numbers which need to be converted to text. An SQL query I would use to do this would be like:
SELECT c.customer_nr, c.status, s.rename FROM CUSTOMERS c INNER JOIN SETUP s on s.id = c.status WHERE s.afk = 'STA'
In my SETUP database I have the columns ID,AFK and RENAME so if the status would be 1 it would convert to text: "ACTIVE", if status = 2 it would convert to "INACTIVE" for example.
I could do something with a formula field like this:
IF ({c.status} = 1) THEN "ACTIVE" ELSE
IF ({c.status}) = 2 THEN "INACTIVE"
but that is not good because i could add another status or change the name in the database etc.
So then I tried with an SQL expression field and I put something like this:
(
SELECT "SETUP"."RENAME" FROM SETUP
WHERE "SETUP"."AFK" = 'STA' AND "SETUP"."ID" = "CUSTOMERS"."STATUS"
)
There must be something wrong because I get the correct conversion but there is only one address in the database but I get 7 pages all with the same address. There should only be one address like I get when I remove the SQL expression field. Where does it go wrong?
* EDIT *
I found the problem. When I create a new database that contains only unique id's then it works. In my original database I have multiple times the id's 1,2,3,4,5 but with different abbreviations in column AFK. Somehow the query looks for the id value and every time it finds this id no matter the AFK value it generates an entry for the address value.
Maybe in the future I will find out how this exactly works for now I have a workaround.
Create a new table for example CrRepSta and add the following entries:
ID,AFK,RENAME
1,STA,Active
2,STA,Inactive
etc
The new query:
(
SELECT "CrRepSta"."RENAME" FROM CrRepSta
WHERE "CrRepSta"."AFK" = 'STA' AND "CrRepSta"."ID" = "CUSTOMERS"."STATUS"
)
And by the way the statement "CrRepSta"."AFK" = 'STA' is not really needed.
I have two select statements that both contain a column of substrings that derived from a database table. They are substrings derived from a varchar that should be an XML, but were saved as varcars because they could be not well-formed and potentially invalid.
I am trying to take the table that results in the 1st query, a list of 50 Varchars, and search the database using the 2nd query. I could get from 0 to n SQLRelatesMessageID sets from each SQLmessageID if I use each row in the first query and append a string to get the node ("z4480" is an example here).
I have tried a cursor implementation but the performance detered me from finishing it. Join doesn't work if you try giving the substring column with an as alias. What steps should I do to get the overall list of SQLRelatesMessageIDs. My goal is to get all MessageLogId (3 in picture) given a NCPDPID.
I am using SQL Server Manager 2012.
--1--Recieves a list based on a given NCPDPID node Value
select substring(m.message, charindex('<MessageID>', m.message)+11, charindex('</MessageID>', m.message)-charindex('<MessageID>', m.message)-11) as
SQLmessageID from messagelog m where message like '%<NCPDPID>'+'1234567'+'</NCPDPID>%'
--2--Selects messageID from top select and searches RelatesToMessageID node
select substring(r.message, charindex('<RelatesToMessageID>', r.message)+20, charindex('</RelatesToMessageID>', r.message)-charindex('<RelatesToMessageID>', r.message)-20) as SQLRelatesMessageID, * from messagelog r
where message like ('%<RelatesToMessageID>'+'z4480'+'</RelatesToMessageID>%')
This works for this answer.
---main
SELECT * FROM
(
select substring(m.message, charindex('<MessageID>', m.message)+11, charindex('</MessageID>', m.message)-charindex('<MessageID>', m.message)-11) as SQLmessageID from messagelog m
where message like '%<NCPDPID>1234567</NCPDPID>%' and dateTime > '3/01/2016'
) a JOIN
(
select
substring(r.message, charindex('<RelatesToMessageID>', r.message)+20, charindex('</RelatesToMessageID>', r.message)-charindex('<RelatesToMessageID>', r.message)-20) as SQLRelatesMessageID,
message,
messagelogid from messagelog r
where
dateTime > '3/01/2016' AND
message LIKE ('%<RelatesToMessageID>%</RelatesToMessageID>%')
) b ON b.SQLRelatesMessageID = a.SQLmessageID