Importing a temp table with declare into another DB - sql-insert
I have a very complicated form which we cannot amend the tables from, so I have made a scoring system based on some help received on a previous question a while back.
I did want a View so I could import it via SQL Agent to another 'Data-cube' DB with the results which could then be easily reported on but because I have a "DECLARE" function this cannot be made a view.
How would I get the results into another DB in its current state, or do I need to change the way it works?
Here is the code so far (Some need case statements as they can be skipped on the submitted form if not required for that job so they come back as NULL):
DECLARE #Translate AS TABLE
(
string varchar(17),
number int
)
INSERT INTO #Translate VALUES
('Checked Ok', 0),
('Rectified On-Site', 5),
('Failed', 5),
('N/A', 0)
SELECT [form].[Name],
[form].[aField875] AS [WorkOrder],
[form].[aField874] AS [Address],
[form].[TimeTag1] AS [Started],
[form].[TimeTag2] AS [Submitted],
DATEDIFF(MINUTE,[form].[TimeTag1],[form].[TimeTag2]) AS [TimeDifference],
(100 - [Form].[Field46] - [Form].[Field2] - [Form].[Field4] - [Form].[Field5]
- [Form].[Field7] - [Form].[Field6] - [Form].[Field12] - [Form].[Field11] - [Form].[Field10] --Risk Assessments & Method Statements
- [Form].[Field17] - [Form].[Field16] - [Form].[Field15] - [Form].[Field14] -- Training/Certs
- [Form].[Field23] - [Form].[Field22] - [Form].[Field21] - [Form].[Field20] -- P.P.E
- [Form].[Field28] - [Form].[Field27] - [Form].[Field26] - [Form].[Field25] - [Form].[Field717] -- Emergency Preparedness
- [Form].[Field34] - [Form].[Field33] - [Form].[Field32] - [Form].[Field31] - [Form].[Field38] - [Form].[Field30] - [Form].[Field37] -- Plant and Vehicles
- [Form].[Field36] - [Form].[Field35] - [Form].[Field42] - [Form].[Field41] - [Form].[Field40] - [Form].[Field39] - [Form].[Field43] -- Plant and Vehicles
- [Form].[Field45] - [Form].[Field50] - [Form].[Field49] - [Form].[Field48] - [Form].[Field47] - [Form].[Field53] - [Form].[Field52] - [Form].[Field56] - [Form].[Field57] - [Form].[Field58] -- Tools and Equipment
- [Form].[Field54] - [Form].[Field62] - [Form].[Field61] - [Form].[Field60] - [Form].[Field59] -- COSHH
- [Form].[Field67] - [Form].[Field66] - [Form].[Field65] - [Form].[Field64] - [Form].[Field68] -- Customer
- [Form].[Field266] - [Form].[Field265] - [Form].[Field271] - [Form].[Field264] - [Form].[Field263] - [Form].[Field262] - [Form].[Field261] -- Excavations, Safe digging and Overhead lines
- [Form].[Field260] - [Form].[Field269] - [Form].[Field268] - [Form].[Field267] - [Form].[Field274] - [Form].[Field273] - [Form].[Field272] - [Form].[Field270] -- Excavations, Safe digging and Overhead lines
- [Form].[Field290] - [Form].[Field289] - [Form].[Field288] - [Form].[Field287] - [Form].[Field286] -- Environment
- [Form].[Field285] - [Form].[Field284] - [Form].[Field283] - [Form].[Field344] - [Form].[Field343] -- Environment
- [Form].[Field70] - [Form].[Field72] - [Form].[Field71] - [Form].[Field74] - [Form].[Field73] -- Activities (aField351)
- [Form].[Field78] - [Form].[Field77] - [Form].[Field76] - [Form].[Field75] - [Form].[Field79] -- Activities (aField351)
- [Form].[Field485] - [Form].[Field489] - [Form].[Field488] - [Form].[Field487] - [Form].[Field486] - [Form].[Field491] - [Form].[Field490] -- CDM - General
) AS [Total] INTO [dbo].[vSiteInspectionWater]
FROM
(SELECT
[af].[Name]
,[af].[aField875]
,[af].[aField874]
,[af].[TimeTag1]
,[pd].[TimeTag2]
,[f46].[number] AS [Field46] -- GARD Handheld Device Available and Working?
,[f2].[number] AS [Field2] -- Safe Systems of Work Folder
,[f4].[number] AS [Field4] -- Emergency Phone List Available On-Site
,[f5].[number] AS [Field5] -- Toilets or Toilet List Available On-Site
--------------------------------------------------------------------------
--Risk Assessments & Method Statements
--------------------------------------------------------------------------
,[f7].[number] AS [Field7] -- RAMS in Place Relevant to Work and Being followed
,[f6].[number] AS [Field6] -- Do the RAMS Cover all of the Significant Hazards
,[f12].[number] AS [Field12] -- Have the RAMS Been Briefed out to the Relevant Personell
,[f11].[number] AS [Field11] -- Are All Required Permits/Authorisations Obtained
,[f10].[number] AS [Field10] -- Are site walkways / access routes free from defects
--------------------------------------------------------------------------
-- Training/Certs
--------------------------------------------------------------------------
,[f17].[number] AS [Field17] -- All Operatives Have Required ID Cards
,[f16].[number] AS [Field16] -- Streetworks (One Person Minimum per Site)
,[f15].[number] AS [Field15] -- Operatives Face-Fit Tested Where Required?
,[f14].[number] AS [Field14] -- Operatives Hold the CSCS, SHEA Water..
--------------------------------------------------------------------------
-- P.P.E
--------------------------------------------------------------------------
,[f23].[number] AS [Field23] -- Minimum PPE Being Worn, is in Good Condition and in Date
,[f22].[number] AS [Field22] -- Task Specific PPE Being Worn, is in Good Condition and in Date
,[f21].[number] AS [Field21] -- PPE Being Stored Appropriately When Not in Use
,[f20].[number] AS [Field20] -- Wet Weather PPE Available
--------------------------------------------------------------------------
-- Emergency Preparedness
--------------------------------------------------------------------------
,CASE WHEN [f28].[number] IS NULL THEN '0' ELSE [f28].[number] END AS [Field28] -- First Aid Kit Available, in Date, Good Condition and Accessible?
,CASE WHEN [f27].[number] IS NULL THEN '0' ELSE [f27].[number] END AS [Field27] -- Eye Wash Available, in Date, Good Condition and Accessible?
,CASE WHEN [f26].[number] IS NULL THEN '0' ELSE [f26].[number] END AS [Field26] -- Fire Extinguisher Charged, in Date, Good Condition and Accessible?
,CASE WHEN [f25].[number] IS NULL THEN '0' ELSE [f25].[number] END AS [Field25] -- Gas Monitor Calibrated, in Date (where required)
,CASE WHEN [f717].[number] IS NULL THEN '0' ELSE [f717].[number] END AS [Field717] -- Outcome of the Emergency Preparedness Test
--------------------------------------------------------------------------
-- Plant and Vehicles
--------------------------------------------------------------------------
,[f34].[number] AS [Field34] -- All Vehicles Parked in a Safe Considerable Manner?
,[f33].[number] AS [Field33] -- Drivers Hold the Correct License for their Category of Vehicle and Trailer (where Towing)?
,[f32].[number] AS [Field32] -- Operators Hold an Operater License For the Plant Being Used?
,[f31].[number] AS [Field31] -- Daily Vehicle and Plant Inspections Being Completed?
,[f38].[number] AS [Field38] -- All Vehicles, Plant and Trailers Free From Defects?
,[f30].[number] AS [Field30] -- Vehicles Have Required Flashing Beacons
,[f37].[number] AS [Field37] -- All Vehicles Plant and Trailers Clean and Tidy?
,[f36].[number] AS [Field36] -- Trailer Break Away Cable in Place and Free From Defects?
,[f35].[number] AS [Field35] -- Trailer Board, Lights and Number Plate in Place, Clean and Secure?
,[f42].[number] AS [Field42] -- Are all Keys Removed From Vehicles/Plant and Equipment?
,[f41].[number] AS [Field41] -- Correct Vehicle Livery in Place for the Contract?
,[f40].[number] AS [Field40] -- Warning Stickers on Rear of Vehicle as Required (Flamable Liquid, gas or Compressed Air)
,[f39].[number] AS [Field39] -- Restraint System/Seat Belts Fitted, Used and in Good Condition?
,[f43].[number] AS [Field43] -- Vehicle Reversing Alarm Working Where Installed?
-------------------------------------------------------------------------
-- Tools and Equipment
-------------------------------------------------------------------------
,[f45].[number] AS [Field45] -- All Plant and Equipment Stored Safely Inside Barriers?
,[f50].[number] AS [Field50] -- Electrical Tools in Good Condition and Within PAT Test?
,[f49].[number] AS [Field49] -- Ladders Free From Defects and Within Inspection Date?
,[f48].[number] AS [Field48] -- Hand Tools in Good Condition and Fit for use?
,[f47].[number] AS [Field47] -- Vibrating Tools in Good Condition and Within Service Dates?
,[f53].[number] AS [Field53] -- Sthil Saw Including Blade in Good Condition with Working Dust Suppression?
,[f52].[number] AS [Field52] -- Portable Generator is in a Serviceable Condition and Being
,[f56].[number] AS [Field56] -- Stop Tap Box Cleaner Available Where Required?
,[f57].[number] AS [Field57] -- Full Set of Valve Keys Available Where Required?
,[f58].[number] AS [Field58] -- Full Set of Lifting Keys Available Where Required?
-------------------------------------------------------------------------
-- COSHH
-------------------------------------------------------------------------
,[f54].[number] AS [Field54] -- COSHH Materials Identified, Labelled Correctly
,[f62].[number] AS [Field62] -- Relevant COSHH Documents Available On-Site?
,[f61].[number] AS [Field61] -- Spill Kit Available and Personnel Familiar with its use?
,[f60].[number] AS [Field60] -- Where Atmospheric Monitoring has Been Identified
,[f59].[number] AS [Field59] -- Where Health Surveillance Has Been Identified as a Control Measure
------------------------------------------------------------------------
-- Customer
------------------------------------------------------------------------
,[f67].[number] AS [Field67] -- Customer Notification Completed Appropiately
,[f66].[number] AS [Field66] -- All Steps Being Taken to Minimise Impact on Customers
,[f65].[number] AS [Field65] -- Site Clean and Tidy
,[f64].[number] AS [Field64] -- Plastic Sheeting/Other used to Protect Custoemr Property from Spoil/Dirt
,[f68].[number] AS [Field68] -- No Spoil, Tools, Equipment Leaning up Against Customer Property
-----------------------------------------------------------------------
-- Excavations, Safe digging and Overhead lines
-----------------------------------------------------------------------
,[f266].[number] AS [Field266] -- Operatives have completed safe digging course?
,[f265].[number] AS [Field265] -- Correct & Relevant Plans
,[f271].[number] AS [Field271] -- If there is a MAHP within the work area, are the workforce aware of it?
,[f264].[number] AS [Field264] -- CAT & Genny present and in calibration?
,[f263].[number] AS [Field263] -- Spare batteries available for the CAT and Genny (minimum 6)
,[f262].[number] AS [Field262] -- Volt stick available (where required)?
,[f261].[number] AS [Field261] -- Utilities marked up with TWEG plate, entended 500m outside excavation area?
,[f260].[number] AS [Field260] -- Waterproof spray/chalk markers used?
,[f269].[number] AS [Field269] -- All digging being completed safely in line with HSG47?
,[f268].[number] AS [Field268] -- No digging within 1m of tree trunk and no roots cut over 25mm diameter?
,[f267].[number] AS [Field267] -- Spoil being stored a safe distance from excavations,
,[f274].[number] AS [Field274] -- Are there adequate measures in place to prevent people,
,[f273].[number] AS [Field273] -- Is the stability of any adjacent structures being put at risk?
,[f272].[number] AS [Field272] -- Are exposed services undamaged and adequately protected/supported?
,[f270].[number] AS [Field270] -- Have all overhead lines been identified and appropiate controls implemented?
-----------------------------------------------------------------------
-- Environment
-----------------------------------------------------------------------
,[f290].[number] AS [Field290] -- Dewatering/Pumping out away from drains,
,[f289].[number] AS [Field289] -- Dechlorination being completed where required?
,[f288].[number] AS [Field288] -- Where there are areas to be protected, are they adequately protected?
,[f287].[number] AS [Field287] -- Where required, are there adequate biosecurity arrangements in place for invasive or non-native species?
,[f286].[number] AS [Field286] -- Are adequate measures taken to prevent dust, mud, air, noise and vibration issues?
,[f285].[number] AS [Field285] -- re all waste transfer notes or equivalent being completed correctly for all waste streams?
,[f284].[number] AS [Field284] -- Has hazardous waste and non hazardous waste
,[f283].[number] AS [Field283] -- Are drip trays or plant nappies being used and maintained on site where required?
,[f344].[number] AS [Field344] -- Where necessary is a concrete washout facility provided and is it correctly used and maintained?
,[f343].[number] AS [Field343] -- Are stockpiles of excavatd material controlled to prevent silty run-off into environmentally sensitive receptors?
----------------------------------------------------------------------
-- Activities (aField351)
-- Working in the Highway
----------------------------------------------------------------------
,CASE WHEN [f70].[number] IS NULL THEN '0' ELSE [f70].[number] END AS [Field70] -- Advance warning signs in place and in good condition?
,CASE WHEN [f72].[number] IS NULL THEN '0' ELSE [f72].[number] END AS [Field72] -- Traffic lights setup correctly and working?
,CASE WHEN [f71].[number] IS NULL THEN '0' ELSE [f71].[number] END AS [Field71] -- Have appropiate lead-in and exit tapers been established as required?
,CASE WHEN [f74].[number] IS NULL THEN '0' ELSE [f74].[number] END AS [Field74] -- Barriers in place and in good condition?
,CASE WHEN [f73].[number] IS NULL THEN '0' ELSE [f73].[number] END AS [Field73] -- Cones, sufficient and in good condition?
,CASE WHEN [f78].[number] IS NULL THEN '0' ELSE [f78].[number] END AS [Field78] -- Appropiate provision for pedestian access including 1KM walkways and safety zones?
,CASE WHEN [f77].[number] IS NULL THEN '0' ELSE [f77].[number] END AS [Field77] -- Kerb ramps installed where required and in good condition?
,CASE WHEN [f76].[number] IS NULL THEN '0' ELSE [f76].[number] END AS [Field76] -- Sand bags used on all signs and every second barrier?
,CASE WHEN [f75].[number] IS NULL THEN '0' ELSE [f75].[number] END AS [Field75] -- Courtesy board in place with the correct permit number displayed?
,CASE WHEN [f79].[number] IS NULL THEN '0' ELSE [f79].[number] END AS [Field79] -- Site compliant with the requirements of the specification?
----------------------------------------------------------------------
-- CDM - General
----------------------------------------------------------------------
,CASE WHEN [f485].[number] IS NULL THEN '0' ELSE [f485].[number] END AS [Field485] -- Is there a current F10 in place and is there a copy on site?
,CASE WHEN [f489].[number] IS NULL THEN '0' ELSE [f489].[number] END AS [Field489] -- Are valid insurance certificates available?
,CASE WHEN [f488].[number] IS NULL THEN '0' ELSE [f488].[number] END AS [Field488] -- Are current Health, Safety and Environmental policies
,CASE WHEN [f487].[number] IS NULL THEN '0' ELSE [f487].[number] END AS [Field487] -- Are emergency arrangements in place and clearly displayed where applicable?
,CASE WHEN [f486].[number] IS NULL THEN '0' ELSE [f486].[number] END AS [Field486] -- Are the Site Rules briefed in the project induction and clearly visible?
,CASE WHEN [f491].[number] IS NULL THEN '0' ELSE [f491].[number] END AS [Field491] -- Have on-site staff received a project specific induction and is there evidence
,CASE WHEN [f490].[number] IS NULL THEN '0' ELSE [f490].[number] END AS [Field490] -- Are all visitors receiving a visitors induction and is there evidence on-site?
FROM [vAdvF_218] [af]
LEFT JOIN #Translate f46 ON [af].[aField46] = [f46].[string]
LEFT JOIN #Translate f2 ON [af].[aField2] = [f2].[string]
LEFT JOIN #Translate f4 ON [af].[aField4] = [f4].[string]
LEFT JOIN #Translate f5 ON [af].[aField5] = [f5].[string]
-- Risk Assessments & Method Statements
LEFT JOIN #Translate f7 ON [af].[aField7] = [f7].[string]
LEFT JOIN #Translate f6 ON [af].[aField6] = [f6].[string]
LEFT JOIN #Translate f12 ON [af].[aField12] = [f12].[string]
LEFT JOIN #Translate f11 ON [af].[aField11] = [f11].[string]
LEFT JOIN #Translate f10 ON [af].[aField10] = [f10].[string]
-- Training/Certs
LEFT JOIN #Translate f17 ON [af].[aField17] = [f17].[string]
LEFT JOIN #Translate f16 ON [af].[aField16] = [f16].[string]
LEFT JOIN #Translate f15 ON [af].[aField15] = [f15].[string]
LEFT JOIN #Translate f14 ON [af].[aField14] = [f14].[string]
-- P.P.E
LEFT JOIN #Translate f23 ON [af].[aField23] = [f23].[string]
LEFT JOIN #Translate f22 ON [af].[aField22] = [f22].[string]
LEFT JOIN #Translate f21 ON [af].[aField21] = [f21].[string]
LEFT JOIN #Translate f20 ON [af].[aField20] = [f20].[string]
-- Emergency Preparedness
LEFT JOIN #Translate f28 ON [af].[aField28] = [f28].[string]
LEFT JOIN #Translate f27 ON [af].[aField27] = [f27].[string]
LEFT JOIN #Translate f26 ON [af].[aField26] = [f26].[string]
LEFT JOIN #Translate f25 ON [af].[aField25] = [f25].[string]
LEFT JOIN #Translate f717 ON [af].[aField717] = [f717].[string]
-- Plant and Vehicles
LEFT JOIN #Translate f34 ON [af].[aField34] = [f34].[string]
LEFT JOIN #Translate f33 ON [af].[aField33] = [f33].[string]
LEFT JOIN #Translate f32 ON [af].[aField32] = [f32].[string]
LEFT JOIN #Translate f31 ON [af].[aField31] = [f31].[string]
LEFT JOIN #Translate f38 ON [af].[aField38] = [f38].[string]
LEFT JOIN #Translate f30 ON [af].[aField30] = [f30].[string]
LEFT JOIN #Translate f37 ON [af].[aField37] = [f37].[string]
LEFT JOIN #Translate f36 ON [af].[aField36] = [f36].[string]
LEFT JOIN #Translate f35 ON [af].[aField35] = [f35].[string]
LEFT JOIN #Translate f42 ON [af].[aField42] = [f42].[string]
LEFT JOIN #Translate f41 ON [af].[aField41] = [f41].[string]
LEFT JOIN #Translate f40 ON [af].[aField40] = [f40].[string]
LEFT JOIN #Translate f39 ON [af].[aField39] = [f39].[string]
LEFT JOIN #Translate f43 ON [af].[aField43] = [f43].[string]
-- Tools and Equipment
LEFT JOIN #Translate f45 ON [af].[aField45] = [f45].[string]
LEFT JOIN #Translate f50 ON [af].[aField50] = [f50].[string]
LEFT JOIN #Translate f49 ON [af].[aField49] = [f49].[string]
LEFT JOIN #Translate f48 ON [af].[aField48] = [f48].[string]
LEFT JOIN #Translate f47 ON [af].[aField47] = [f47].[string]
LEFT JOIN #Translate f53 ON [af].[aField53] = [f53].[string]
LEFT JOIN #Translate f52 ON [af].[aField52] = [f52].[string]
LEFT JOIN #Translate f56 ON [af].[aField56] = [f56].[string]
LEFT JOIN #Translate f57 ON [af].[aField57] = [f57].[string]
LEFT JOIN #Translate f58 ON [af].[aField58] = [f58].[string]
-- COSHH
LEFT JOIN #Translate f54 ON [af].[aField54] = [f54].[string]
LEFT JOIN #Translate f62 ON [af].[aField62] = [f62].[string]
LEFT JOIN #Translate f61 ON [af].[aField61] = [f61].[string]
LEFT JOIN #Translate f60 ON [af].[aField60] = [f60].[string]
LEFT JOIN #Translate f59 ON [af].[aField59] = [f59].[string]
LEFT JOIN #Translate f67 ON [af].[aField67] = [f67].[string]
LEFT JOIN #Translate f66 ON [af].[aField66] = [f66].[string]
LEFT JOIN #Translate f65 ON [af].[aField65] = [f65].[string]
LEFT JOIN #Translate f64 ON [af].[aField64] = [f64].[string]
LEFT JOIN #Translate f68 ON [af].[aField68] = [f68].[string]
LEFT JOIN #Translate f266 ON [af].[aField266] = [f266].[string]
LEFT JOIN #Translate f265 ON [af].[aField265] = [f265].[string]
LEFT JOIN #Translate f271 ON [af].[aField271] = [f271].[string]
LEFT JOIN #Translate f264 ON [af].[aField264] = [f264].[string]
LEFT JOIN #Translate f263 ON [af].[aField263] = [f263].[string]
LEFT JOIN #Translate f262 ON [af].[aField262] = [f262].[string]
LEFT JOIN #Translate f261 ON [af].[aField261] = [f261].[string]
LEFT JOIN #Translate f260 ON [af].[aField260] = [f260].[string]
LEFT JOIN #Translate f269 ON [af].[aField269] = [f269].[string]
LEFT JOIN #Translate f268 ON [af].[aField268] = [f268].[string]
LEFT JOIN #Translate f267 ON [af].[aField267] = [f267].[string]
LEFT JOIN #Translate f274 ON [af].[aField274] = [f274].[string]
LEFT JOIN #Translate f273 ON [af].[aField273] = [f273].[string]
LEFT JOIN #Translate f272 ON [af].[aField272] = [f272].[string]
LEFT JOIN #Translate f270 ON [af].[aField270] = [f270].[string]
-- Environment
LEFT JOIN #Translate f290 ON [af].[aField290] = [f290].[string]
LEFT JOIN #Translate f289 ON [af].[aField289] = [f289].[string]
LEFT JOIN #Translate f288 ON [af].[aField288] = [f288].[string]
LEFT JOIN #Translate f287 ON [af].[aField287] = [f287].[string]
LEFT JOIN #Translate f286 ON [af].[aField286] = [f286].[string]
LEFT JOIN #Translate f285 ON [af].[aField285] = [f285].[string]
LEFT JOIN #Translate f284 ON [af].[aField284] = [f284].[string]
LEFT JOIN #Translate f283 ON [af].[aField283] = [f283].[string]
LEFT JOIN #Translate f344 ON [af].[aField344] = [f344].[string]
LEFT JOIN #Translate f343 ON [af].[aField343] = [f343].[string]
-- Activities
LEFT JOIN #Translate f70 ON [af].[aField70] = [f70].[string]
LEFT JOIN #Translate f72 ON [af].[aField72] = [f72].[string]
LEFT JOIN #Translate f71 ON [af].[aField71] = [f71].[string]
LEFT JOIN #Translate f74 ON [af].[aField74] = [f74].[string]
LEFT JOIN #Translate f73 ON [af].[aField73] = [f73].[string]
LEFT JOIN #Translate f78 ON [af].[aField78] = [f78].[string]
LEFT JOIN #Translate f77 ON [af].[aField77] = [f77].[string]
LEFT JOIN #Translate f76 ON [af].[aField76] = [f76].[string]
LEFT JOIN #Translate f75 ON [af].[aField75] = [f75].[string]
LEFT JOIN #Translate f79 ON [af].[aField79] = [f79].[string]
-- CDM - General
LEFT JOIN #Translate f485 ON [af].[aField485] = [f485].[string]
LEFT JOIN #Translate f489 ON [af].[aField489] = [f489].[string]
LEFT JOIN #Translate f488 ON [af].[aField488] = [f488].[string]
LEFT JOIN #Translate f487 ON [af].[aField487] = [f487].[string]
LEFT JOIN #Translate f486 ON [af].[aField486] = [f486].[string]
LEFT JOIN #Translate f491 ON [af].[aField491] = [f491].[string]
LEFT JOIN #Translate f490 ON [af].[aField490] = [f490].[string]
INNER JOIN [PhoneData] [pd] ON [af].[RecId] = [pd].[recid]) AS [Form]
Resolved by re-designing the script and creating a CURSOR, which is run via SP and pulls the data into a data cube.
Related
Nopcommerce export user roles
I'm trying to get a list of ALL user roles for each user through the nopcommerce database. i just need the Customer.Id, Customer.Username, Customer.Email and Customer.Role (some have multiple roles) This is all jacked up. SELECT Customer.Id, Customer.Username, Customer.Email, CustomerRole.Name FROM ((Customer INNER JOIN CustomerRole ON CustomerRole.Id = Customer.Id) INNER JOIN Customer_CustomerRole_Mapping ON Customer_CustomerRole_Mapping.CustomerRole_Id = CustomerRole.Id) Any help is greatly appreciated!
I think you need a LEFT OTUER JOIN because in your DB can be user without role; in this case you can use this: SELECT DISTINCT CUST.Id, CUST.Username, CUST.Email, CR.Id, CR.Name, CCRM.CustomerRole_Id , FROM CUSTOMER CUST LEFT OUTER JOIN Customer_CustomerRole_Mapping CCRM ON CUST.ID = CCRM.Customer_Id LEFT OUTER JOIN CustomerRole CR ON CCRM.CustomerRole_Id = CR.Id Also, you can have only one row per user with a column with his roles, like this: SELECT DISTINCT CUST.Id, CUST.Username, CUST.Email, Roles = STUFF(( SELECT ',' + CAST(CR1.Name as varchar(4000)) FROM CustomerRole CR1 LEFT OUTER JOIN Customer_CustomerRole_Mapping CCRM1 ON CCRM1.CustomerRole_Id = CR1.Id LEFT OUTER JOIN CUSTOMER CUST1 ON CUST1.ID = CCRM1.Customer_Id WHERE CUST1.Id = CUST.Id FOR XML PATH('') ), 1, 1, '') FROM CUSTOMER CUST LEFT OUTER JOIN Customer_CustomerRole_Mapping CCRM ON CUST.ID = CCRM.Customer_Id LEFT OUTER JOIN CustomerRole CR ON CCRM.CustomerRole_Id = CR.Id Depending of your using, last query can be a little bit slow if you have a lot of customers.
Why would oracle subquery with AND & OR return returning wrogn results set
I have two subqueries. as shown below. the first query works fine but the second query which is basically the first query that I modified to use AND & OR, doesn't work in the sense that it doesn't return ID as expected. any suggestions on what is happening here? 1. (SELECT * FROM (SELECT EMPID FROM EVENT_F INNER JOIN WCINFORMATION_D ON EVENT_F.JOB_INFO_ROW_WID= WCINFORMATION_D.ROW_WID INNER JOIN WCANDIDATE_D ON WCCANDIDATE_D.ROW_WID = VENT_F.CANDIDATE_ROW_WID WHERE STEP_NAME = 'Offer' AND WCINFORMATION_D.JOB_FAMILY_NAME IN ('MDP','ELP','Emerging Leader Program','Other') AND TITLE NOT IN ('Student Ambassador Program for Eligible Summer Interns','Student Ambassador') AND PI_CANDIDATE_NUM = OUTERAPP.PI_CANDIDATE_NUM --limit 1 ORDER BY CREATION_DT ASC ) T1 WHERE ROWNUM=1) AS A_ID, 2.(SELECT * FROM (SELECT EMPID FROM EVENT_F INNER JOIN WCINFORMATION_D ON EVENT_F.JOB_INFO_ROW_WID= WCINFORMATION_D.ROW_WID INNER JOIN WCANDIDATE_D ON WCCANDIDATE_D.ROW_WID = VENT_F.CANDIDATE_ROW_WID WHERE STEP_NAME = 'Offer' AND WCINFORMATION_D.JOB_FAMILY_NAME IN ('MDP','ELP','Emerging Leader Program','Other') or WCINFORMATION_D.JOB_FAMILY_NAME NOT IN ('MDP','ELP','Emerging Leader Program','Other') AND TITLE NOT IN ('Student Ambassador Program for Eligible Summer Interns','Student Ambassador') AND PI_CANDIDATE_NUM = OUTERAPP.PI_CANDIDATE_NUM --limit 1 ORDER BY CREATION_DT ASC ) T1 WHERE ROWNUM=1) AS A_ID,
If you're wanting to get the count of people in one set of job families, plus a count of people in another set, you need to use a conditional count, e.g. something along the lines of: SELECT COUNT(CASE WHEN wid.job_family_name IN ('MDP', 'ELP', 'Emerging Leader Program', 'Other') THEN 1 END) job_family_grp1, COUNT(CASE WHEN wid.job_family_name IS NULL OR wid.job_family_name NOT IN ('MDP', 'ELP', 'Emerging Leader Program', 'Other') THEN 1 END) job_family_grp2 FROM event_f ef INNER JOIN wcinformation_d wid ON ef.job_info_row_wid = wid.row_wid INNER JOIN wcandidate_d wcd ON wcd.row_wid = ef.candidate_row_wid WHERE step_name = 'Offer' -- alias this column name AND title NOT IN ('Student Ambassador Program for Eligible Summer Interns', 'Student Ambassador') -- alias this column name; You will most likely need to amend this to work for your particular case (it'll have to go as a join into your main query, given there are two columns being selected) since you didn't provide enough information in your question to give us the wider context.
Use check box selected elements to filter report
I have an issue with making a correct report for my application. The report is supposed to return free rooms for a set of parameters (including a list of equipements). However, I cannot include the equipements search in my interractive report. Here is the SQL request I've got: SELECT s.code || ' ' || s.nom as Salle, s.capacite, LISTAGG(eq.libelle, ', ') WITHIN GROUP (ORDER BY eq.code) as Equipements, b.libelle as Batiment, s.numero FROM SALLES s INNER JOIN batiments b ON b.numero = s.bat_numero INNER JOIN sal_gpe sg ON s.numero = sg.sal_numero INNER JOIN groupespersonnes gp ON gp.numero = sg.gpe_numero LEFT JOIN equ_sal e ON e.sal_numero = s.numero LEFT JOIN equipements eq ON eq.numero = e.equ_numero -- On vérifie les salles visibles pour le groupe de personne affecté à l'utilisateur connecté. WHERE gp.code = (SELECT code FROM groupespersonnes INNER JOIN personnes p ON p.gpe_numero = groupespersonnes.numero WHERE UPPER(:APP_USER) = UPPER(p.username)) AND :P30_PERIODE_DEBUT NOT IN (SELECT per_numero FROM enregistrements WHERE sal_numero = s.numero AND TO_CHAR(jour, 'DD.MM.RR') = :P30_DATE) AND :P30_PERIODE_FIN NOT IN (SELECT per_numero FROM enregistrements WHERE sal_numero = s.numero AND TO_CHAR(jour, 'DD.MM.RR') = :P30_DATE) AND e.equ_numero = regexp_substr(:P30_EQUIPEMENTS,'[^:]+') GROUP BY s.capacite, s.nom, b.libelle, s.numero, s.code I'm doing a LISTAGG on the equipements, so that I can show all the equipements for one room fit in one cell. Now :P30_EQUIPEMENT is actually a STRING. It takes the number of the equipements selected from the checkbox list (Dynamic LOV), and separates them with a ":". I've been trying many solutions but nothing worked. I'm now diving in the PL/SQL option. If you have a idea of an easier way, I'd gladly take it.
If this is what :P30_EQUIPMENTS contains, then you should: SQL> with test (p30_equipments) as 2 (select 'tv:balcony:shower' from dual) 3 select regexp_substr(p30_equipments, '[^:]+', 1, level) equipment 4 from test 5 connect by level <= regexp_count(p30_equipments, ':') + 1; EQUIPMENT ----------------- tv balcony shower SQL> i.e. applied to your code: and e.equ_numero in (select regexp_substr(:p30_equipments, '[^:]+', 1, level) from dual connect by level <= regexp_count(:p30_equipments, ':') + 1 )
Selecting incorrect records
In following query, what is happening is that, the 3rd join is not being done. we are getting pharmacy match and then the display is showing patients in other facilities who share the same pharmacy, can you see why this would be happening? Insert Into #tblNDC SELECT ROW_NUMBER() OVER(ORDER BY ID_KEY DESC) AS RN,* From ( Select distinct A.PHARMACYNPI, f.FACILITY_NAME, ID_KEY, [BATCH] AS column1, [IMPORTDATE], [DATEBILLED], [RX], [DATEDISPENSED], [DAYSUPPLY], [PAYTYPE], A.[NPI], [PHYSICIAN], [COST], [QUANTITY], [MEDICATION], A.[NDC], f.FACILITY_ID FROM [PBM].[T_CHARGES] A LEFT OUTER JOIN [OGEN].[NDC_M_FORMULARY] B ON A.[NDC] = B.[NDC] --Left Outer Join PBM.FACILITY f on A.FACILITYNPI = f.FACILITY_NPI Left Outer Join PBM.PHARMACY_NPI pn on A.PHARMACYNPI = pn.NPI Inner join PBM.PHARMACY_FACILITY pp on pn.PHARMACY_ID = pp.PHARMACY_ID Inner Join PBM.FACILITY f on pp.FACILITY_ID = f.FACILITY_ID Where [STAT] not in (3, 4, 5) AND [TIER] <> 'T1' AND f.FACILITY_ID IN ( select FacilityID from #tblFacility ) AND f.FACILITY_ID IN ( SELECT * FROM [PBM].[Split1] (#selectedFacility) ) --- it seems 3rd condition not being done ----------------------------------
Its hard to see for sure without knowing the data, but my first thoughts are that the Left Outer joins will be giving you joinage you might not want. Go through each join and remove it until you start to get dodgy records back, if it is the 3rd one when it suddenly goes strange, then i suspect you have multiple IDs for a facility.
Full Outer Join in sqlite on 4 tables
I need to join 4 tables based on a common primary key. If sqlite implemented full outer joins it might look something like this (with optimization not taken into account). SELECT S.pair, C.ball, P.bluejeans, B.checkered FROM Socks S FULL OUTER JOIN Caps C FULL OUTER JOIN Pants P FULL OUTER JOIN Boxers B WHERE S.color = C.color AND S.color = P.color AND S.color = B.color; I've looked long and hard and the best I found was this 2 table sqlite full join implemented with left joins and union alls: SELECT employee.*, department.* FROM employee LEFT JOIN department ON employee.DepartmentID = department.DepartmentID UNION ALL SELECT employee.*, department.* FROM department LEFT JOIN employee ON employee.DepartmentID = department.DepartmentID WHERE employee.DepartmentID IS NULL; I'm trying to modify this to work for more than 2 tables but I'm new to SQL and I'm not getting too far. Is it possible to get this result in a reasonable amount of time? I think I have a correct implementation for 3 tables (it might not be correct) but I still can't seem to get it for 4. Here's what I have for 3: SELECT S.pair, C.ball, P.bluejeans FROM Socks S LEFT JOIN Caps C LEFT JOIN Pants P ON C.color = S.color AND P.color = S.color UNION ALL SELECT S.pair, C.ball, P.bluejeans FROM Socks S LEFT JOIN Caps C LEFT JOIN Pants P ON S.color = C.color AND S.color = P.color WHERE S.color IS NULL; Any help is much appreciated
The general construction for a full outer join between two tables A and B in SQLite indeed is: SELECT ... FROM A LEFT JOIN B ON ... UNION ALL SELECT ... FROM B LEFT JOIN A ON ... WHERE A.key IS NULL Now create a view SocksCaps for the full outer join between Socks and Caps: CREATE VIEW SocksCaps AS SELECT ... FROM Socks LEFT JOIN Caps ON ... UNION ALL SELECT ... FROM Caps LEFT JOIN Socks ON ... WHERE Socks.color IS NULL Do the same for Pants and Boxers. Then treat these views just like tables and do a full outer join with the same construction: SELECT ... FROM SocksCaps LEFT JOIN PantsBoxers ON ... UNION ALL SELECT ... FROM PantsBoxers LEFT JOIN SocksCaps ON ... WHERE SocksCaps.color IS NULL