I have a table in SQL that have many columns which the value of each columns in every row is either ' ' or 'X'. I need to count this 'X' for every columns which can be done by following code;
SELECT COUNT(GVI0) AS GVI0, COUNT(GVI1) AS GVI1, COUNT(GVI2) AS GVI2
FROM dbo.HullInspectionProgram
WHERE (StructureEntry='1' AND Year='2016')
The result of the query is;
GVI0 NDT0 GVI1 NDT1 GVI2 NDT2
11 11 2 4 11 11
However, (in my understanding) in order for this count value to be bind into ASP.net Chart Control with multiple series name 'GVI' and 'NDT', I need to make the column into row for the DataTable.
I try to use UNPIVOT in SQL like this;
SELECT GVI0Count
FROM (
SELECT COUNT(GVI0) AS GVI0, COUNT(GVI1) AS GVI1, COUNT(GVI2) AS GVI2
FROM dbo.HullInspectionProgram
WHERE (StructureEntry='1' AND Year='2016')
)
UNPIVOT (GVI0Count FOR ListOfColumns IN (GVI0)) AS unpivott
but it seem that the code is wrong.
How do I do this?
I think the following might work for you. At least, as a start.
SELECT *
FROM (
SELECT COUNT(GVI0) AS GVI0, COUNT(GVI1) AS GVI1, COUNT(GVI2) AS GVI2
FROM dbo.HullInspectionProgram
WHERE (StructureEntry='1' AND Year='2016')
) P
UNPIVOT (GVI0Count FOR ListOfColumns IN (GVI0, GVI1, GVI2)) AS unpivott
Related
This script does give me the list of changed rows but I also need to make sure that the number of changed rows should be equal to the number of rows those were update in the last 1 hour which would give me more comfort level on validation.
Here is the code which gives me the list of changed rows. Except does give me all the rows from the first select statement which are not in the second select if I am not wrong. I am just wondering how to check the number of rows that were updated in the last hr which must match the count of rows when I run this below query.
Select [Accounting_Period_ID]
,[Policy_Number]
,[Risk_ID]
,[Product_ID]
,[Inception_Date_ID]
,[Effective_Date_ID]
,[Expiration_Date_ID]
,[Cancellation_Date_ID]
,[Reinstate_Date_ID]
,[Policy_Source_System_ID]
,[Risk_Geo_ID]
,[Risk_Profile_ID]
,[Policy_Status_ID]
,[Agency_ID]
,[Limit_Selection_ID]
,[Written_Premium_MTD]
,[Written_Premium_ITD]
,[Fees_MTD]
,[Fees_ITD]
,[Commission_MTD]
,[Commission_ITD]
,[Earned_Premium_MTD]
,[Earned_Premium_ITD]
,[In_Force_Count]
,[New_Business_Count]
,[Renewed_Count]
,[Cancelled_Count]
,[Reinstated_Count]
,[Dwelling_Limit]
,[Other_Structures_Base_Limit]
,[Other_Structures_Extended_Limit]
,[Other_Structures_Total_Limit]
,[Contents_Limit]
,[Additional_Living_Expense_Limit]
,[Liability_Limit]
,[Medical_Limit]
,[Total_Insured_Value]
,[Replacement_Value]
,[AOP_Deductible]
,[Days_in_Force]
,[Earned_House_Years]
,[Cancellation_Entry_Date_ID]
,[Reinstate_Entry_Date_ID]
,[Seq]
,[Inserted_Date]
,[Inserted_By]
,[Last_Updated_Date]
,[Last_Updated_By]
,[Insurance_score]
,[Rewrite_Count]
,[Entry_Date_ID] from Datamart.Policy.Fact_Monthly_Policy_Snap_20190403
where Policy_Source_System_ID = 8
EXCEPT
Select [Accounting_Period_ID]
,[Policy_Number]
,[Risk_ID]
,[Product_ID]
,[Inception_Date_ID]
,[Effective_Date_ID]
,[Expiration_Date_ID]
,[Cancellation_Date_ID]
,[Reinstate_Date_ID]
,[Policy_Source_System_ID]
,[Risk_Geo_ID]
,[Risk_Profile_ID]
,[Policy_Status_ID]
,[Agency_ID]
,[Limit_Selection_ID]
,[Written_Premium_MTD]
,[Written_Premium_ITD]
,[Fees_MTD]
,[Fees_ITD]
,[Commission_MTD]
,[Commission_ITD]
,[Earned_Premium_MTD]
,[Earned_Premium_ITD]
,[In_Force_Count]
,[New_Business_Count]
,[Renewed_Count]
,[Cancelled_Count]
,[Reinstated_Count]
,[Dwelling_Limit]
,[Other_Structures_Base_Limit]
,[Other_Structures_Extended_Limit]
,[Other_Structures_Total_Limit]
,[Contents_Limit]
,[Additional_Living_Expense_Limit]
,[Liability_Limit]
,[Medical_Limit]
,[Total_Insured_Value]
,[Replacement_Value]
,[AOP_Deductible]
,[Days_in_Force]
,[Earned_House_Years]
,[Cancellation_Entry_Date_ID]
,[Reinstate_Entry_Date_ID]
,[Seq]
,[Inserted_Date]
,[Inserted_By]
,[Last_Updated_Date]
,[Last_Updated_By]
,[Insurance_score]
,ISNULL([Rewrite_Count],0) Rew
,[Entry_Date_ID] from Datamart.Policy.Fact_Monthly_Policy_Snap
where Policy_Source_System_ID = 8
DATEADD(hh,-1,GETDATE()) gives you the actual Time minus 1 hour .this you can compare with Last_Updated_Date.
Count(*) gives you the number of rows.
I have a SQL command that works great in SQL Server. Here's the query:
SELECT TOP 1000
(
SELECT COUNT(LINENUM)
FROM OEORDD D1
WHERE D1.ORDUNIQ = OEORDD.ORDUNIQ
)
- (SELECT COUNT(LINENUM)
FROM OEORDD D1
WHERE D1.ORDUNIQ = OEORDD.ORDUNIQ
AND D1.LINENUM > OEORDD.LINENUM)
FROM OEORDD
ORDER BY ORDUNIQ, LINENUM
The query looks at the total lines on an order, then looks at the current "LINENUM" field. With the value of the LINENUM field, it looks to see how many lines have a greater LINENUM value on the order and subtracts it from the number of lines on an order to get the correct Line number.
When I try to add it as a SQL expression in version 14.0.2.364 as follows:
(
(
SELECT COUNT("OEORDD"."LINENUM")
FROM "OEORDD" "D1"
WHERE "D1"."ORDUNIQ" = "OEORDD"."ORDUNIQ"
)
- (SELECT COUNT("OEORDD"."LINENUM")
FROM "OEORDD" "D1"
WHERE "D1"."ORDUNIQ" = "OEORDD"."ORDUNIQ"
AND "D1"."LINENUM" > "OEORDD"."LINENUM"
)
)
I get the error "Column 'SAMDB.dbo.OEORDD.ORDUNIQ' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
If I try to add GROUP BY "OEORDD"."ORDUNIQ" at the end, I get "Incorrect syntax near the keyword 'GROUP'. I've tried adding "FROM OEORDD" at the end of query and it errors out on the word "FROM". I have the correct tables linked in the Database Expert.
EDIT --------------
I was able to get the first query working by getting rid of the alias, it's as follows:
(
SELECT COUNT(LINENUM)
FROM OEORDD
WHERE OEORDH.ORDUNIQ=OEORDD.ORDUNIQ)
)
However, I believe I need to use the alias in the second query to compare line numbers. I'm still stuck on that one.
I need data from a variety of tables and below is the only way I know to do it (I just know the basics). The query below works fine but shows duplicates. I need to know how to remove those.
SELECT DISTINCT
a.int_order_id, a.trans_id, a.wtn,a.isp_id,
d.first_name, d.middle_initial, d.last_name,
d.company_name, d.emaiL,
a.ilec_lob, a.node_type_id, a.cddd,
a.isp_ban, a.tos_version, a.isp_ckt_id,
a.isp_circuit_type, a.atm_vpi, a.atm_vci,
a.frs_dlci, b.order_create_date, b.pon,
b.order_status_id, e.trans_status_id,
e.description, c.STREET_NUMBER,
c.STREET_NUMBER_SUFFIX, c.DIRECTIONAL_ID,
c.street_name, c.thoroughfare_id,
c.street_suffix, c.address_line1, c.address_line2,
c.unit_type_id, c.unit_value, c.city, c.state_id, c.zip
FROM
VZEXTRACT1.vvov_os_ord_dsl A
JOIN
VZEXTRACT1.vvov_os_order_details B ON a.int_order_id = b.int_order_id
JOIN
VZEXTRACT1.vvov_os_ord_address C ON b.int_order_id = c.int_order_id
JOIN
vzextract1.vvov_os_ord_contact D ON c.int_order_id = d.int_order_id
JOIN
VZEXTRACT1.vv0v_trans_status E On b.order_status_id = e.trans_status_id
WHERE
a.isp_id NOT IN (657,500)
AND B.ORDER_CREATE_DATE >= to_date('01-may-15')
AND B.ORDER_CREATE_DATE < to_date('30-JUL-15')
When you want to remove duplicate rows you will have to use
Distinct : It will consider distinct of all selected columns so you will have to find out because of which column you are getting duplicate rows even if you have used distinct
Group by : You can use group by clause to get distinct rows. You can use aggregate function for column causing duplicate rows from point 1 and avoid duplicates.
over(partition by) : you can also use this clause for column causing duplicates. Like you concat values of such column ,
wm_concat(my_col)over(partition by id)
Hello from an absolute beginner in SQL!
I have a field I want to populate based on another table. For this I have written this query, which fails with: Msg 512, Level 16, State 1, Line 1
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated.
oK, here goes:
Update kre.CustomerOrderLineCopy
SET DepNo = (SELECT customerordercopy.DepNo
FROM kre.CustomerOrderCopy , kre.CustomerOrderLineCopy
WHERE CustomerOrderLineCopy.OrderCopyNo =kre.CustomerOrderCopy.OrderCopyNo)
WHERE CustomerOrderLineCopy.OrderCopyNo = (SELECT CustomerOrderCopy.OrderCopyNo
FROM kre.CustomerOrderCopy, kre.CustomerOrderLineCopy
WHERE kre.CustomerOrderLineCopy.OrderCopyNo = kre.CustomerOrderCopy.OrderCopyNo)
What I'm trying to do is to change DepNo in CustomerOrderLineCopy, with the value in DepNo in CustomerOrderCopy - based on the same OrderCopyNo in both tables.
I'm open for all suggestion.
Thanks,
ohalvors
If you just join the tables together the update is easier:
UPDATE A SET A.DepNo = B.DepNo
FROM kre.CustomerOrderLineCopy A
INNER JOIN kre.CustomerOrderCopy B ON A.OrderCopyNo = B.OrderCopyNo
The problem is that at least one of your sub queries return more than one value. Think about this:
tablePerson(name, age)
Adam, 11
Eva, 11
Sven 22
update tablePerson
set name = (select name from tablePerson where age = 11)
where name = 'Sven'
Which is equivalent to: set Sven's name to Adam and Eva. Which is not possible.
If you want to use sub queries, either make sure your sub queries can only return one value or force one value by using:
select top 1 xxx from ...
This may be enough to quieten it down:
Update kre.CustomerOrderLineCopy
SET DepNo = (SELECT customerordercopy.DepNo
FROM kre.CustomerOrderCopy --, kre.CustomerOrderLineCopy
WHERE CustomerOrderLineCopy.OrderCopyNo =kre.CustomerOrderCopy.OrderCopyNo)
WHERE CustomerOrderLineCopy.OrderCopyNo = (SELECT CustomerOrderCopy.OrderCopyNo
FROM kre.CustomerOrderCopy --, kre.CustomerOrderLineCopy
WHERE kre.CustomerOrderLineCopy.OrderCopyNo = kre.CustomerOrderCopy.OrderCopyNo)
(Where I've commented out kre.CustomerOrderLineCopy in the subqueries) That is, you were hopefully trying to correlate these subqueries with the outer table - not introduce another instance of kre.CustomerOrderLineCopy.
If you still get an error, then you still have multiple rows in kre.CustomerOrderCopy which have the same OrderCopyNo. If that's so, you need to give us (and SQL Server) the rules that you want to apply for how to select which row you want to use.
The danger of switching to the FROM ... JOIN form shown in #Avitus's answer is that it will no longer report if there are multiple matching rows - it will just silently pick one of them - which one is never made clear.
Now I look at the query again, I'm not sure it even needs a WHERE clause now. I think this is the same:
Update kre.CustomerOrderLineCopy
SET DepNo = (
SELECT customerordercopy.DepNo
FROM kre.CustomerOrderCopy
WHERE CustomerOrderLineCopy.OrderCopyNo = kre.CustomerOrderCopy.OrderCopyNo)
Inside a varchar2 column I have text values like :
aaaaaa. fgdfg.
bbbbbbbbbbbbbb ccccccccc
dddddd ddd dddddddddddd,
asdasdasdll
sssss
if i do select column from table where id=... i get the whole text in a single row, normally.
But i would like to get the result in multiple rows, 5 for the example above.
I have to use just one select statement, and the delimiters will be new line or carriage return (chr(10), chr(13) in oracle)
Thank you!
Like this, maybe (but it all depends on the version of oracle you are using):
WITH yourtable AS (SELECT REPLACE('aaaaaa. fgdfg.' ||chr(10)||
'bbbbbbbbbbbbbb ccccccccc ' ||chr(13)||
'dddddd ddd dddddddddddd,' ||chr(10)||
'asdasdasdll ' ||chr(13)||
'sssss '||chr(10),chr(13),chr(10)) AS astr FROM DUAL)
SELECT REGEXP_SUBSTR ( astr, '[^' ||chr(10)||']+', 1, LEVEL) data FROM yourtable
CONNECT BY LEVEL <= LENGTH(astr) - LENGTH(REPLACE(astr, chr(10))) + 1
see: Comma Separated values in Oracle
The answer by Kevin Burton contains a bug if your data contains empty lines.
The adaptation below, based on the solution invented here, works. Check that post for an explanation on the issue and the solution.
WITH yourtable AS (SELECT REPLACE('aaaaaa. fgdfg.' ||chr(10)||
'bbbbbbbbbbbbbb ccccccccc ' ||chr(13)||
chr(13)||
'dddddd ddd dddddddddddd,' ||chr(10)||
'asdasdasdll ' ||chr(13)||
'sssss '||chr(10),chr(13),chr(10)) AS astr FROM DUAL)
SELECT REGEXP_SUBSTR ( astr, '([^' ||chr(10)||']*)('||chr(10)||'|$)', 1, LEVEL, null, 1) data FROM yourtable
CONNECT BY LEVEL <= LENGTH(astr) - LENGTH(REPLACE(astr, chr(10))) + 1;