我有一个具有这种设计的表数据库:
CREATE TABLE [dbo].[Facturen](
[UUID] [uniqueidentifier] NOT NULL,
[Invoice] [nvarchar](15) NULL,
[Company] [nvarchar](10) NULL,
[MESSAGEID] [nvarchar](max) NOT NULL,
[SENTON] [datetime] NOT NULL,
[SENDER] [nvarchar](320) NOT NULL,
[RECIPIENT] [nvarchar](320) NOT NULL,
[CLIENT] [nvarchar](15) NOT NULL,
[OK] [bit] NULL,
[ERROR] [nvarchar](max) NULL
此表包含以下示例数据:
+--------------------------------------+-----------+---------+---------------------------------------------------------------+-------------------------+-------------------+----------------------+--------------+----+----------------------------------+-----------------------------------------------+------------------------------------------------+------------------------------------------------------------+
| UUID | Invoice | Company | MESSAGEID | SENTON | SENDER | RECIPIENT | CLIENT | OK | ERROR | | | |
+--------------------------------------+-----------+---------+---------------------------------------------------------------+-------------------------+-------------------+----------------------+--------------+----+----------------------------------+-----------------------------------------------+------------------------------------------------+------------------------------------------------------------+
| B7B989C3-AD46-48EC-8399-0000EA265704 | 9940541 | V | <B94EA522-1C20-4F08-A4F1-C6E687717058@relay.pacombigroup.com> | 2019-06-08 01:00:38.317 | sender@domain.com | recipient@domain.com | 172.16.2.78 | 1 | NULL | | | |
| 31D995CB-CC0F-402D-ABAC-0003D5B9FC1B | 9230694 | W01 | <A3DC2BAA-2C8A-40E3-ABF7-354AEA4A8666@relay.pacombigroup.com> | 2019-05-29 22:19:12.603 | sender@domain.com | recipient@domain.com | 172.16.2.78 | 1 | NULL | | | |
| 8F8AB577-BEFE-4A9C-84E0-00116B8F7E35 | 9307584 | V03 | <3608E53B-1152-482E-92C5-E420C2672539@relay.pacombigroup.com> | 2019-05-31 18:37:14.013 | sender@domain.com | recipient@domain.com | 172.16.2.78 | 1 | NULL | | | |
| 6C72A5C6-B39F-47AA-9B28-0012A569AE48 | 9936729 | V | <33BBF9E9-3E04-4ED4-B132-21B0DE181713@relay.pacombigroup.com> | 2019-05-23 18:54:39.600 | sender@domain.com | recipient@domain.com | 172.16.2.78 | 1 | NULL | | | |
| 7627141F-7004-4E26-8061-001B1D21D76F | 1931923 | W03 | <B1F1D18E-CEFD-412F-AF1A-913640EEE88B@relay.pacombigroup.com> | 2019-06-06 16:56:49.643 | sender@domain.com | recipient@domain.com | 172.16.0.93 | 1 | NULL | | | |
| FA86911B-A9C2-4449-9101-00248B46D626 | 9937761 | V | <54396856-70CA-4846-A8E7-8F17C2841F64@relay.pacombigroup.com> | 2019-05-28 20:12:50.250 | sender@domain.com | recipient@domain.com | 172.16.2.78 | 0 | Error Type: SMTP | Remote server (63.34.218.7) issued an error. | hMailServer sent: EHLO relay.pacombigroup.com | Remote server replied: Remote server closed connection. |
| 9BF2E3C9-CC2E-4CC8-BA77-002CC9AE9984 | 636393256 | V | <C9D242F3-5B6B-412E-8603-CA033985D93D@relay.pacombigroup.com> | 2019-05-23 16:51:56.163 | sender@domain.com | recipient@domain.com | 172.16.2.149 | 1 | NULL | | | |
| 537C2C48-2ED5-40B1-8023-002E1E874237 | 9231441 | W01 | <6F029189-6393-456D-8CEB-29D250E49D4A@relay.pacombigroup.com> | 2019-06-04 17:17:17.860 | sender@domain.com | recipient@domain.com | 172.16.2.57 | 1 | NULL | | | |
| E68F3AF0-7307-486F-832E-00353A4F3A62 | 636393206 | V | <8D4CE2E6-70A5-47AF-AEA1-7229C7061889@relay.pacombigroup.com> | 2019-05-22 16:56:49.743 | sender@domain.com | recipient@domain.com | 172.16.2.70 | 0 | Remote server closed connection. | | | |
| 009CE9DA-496A-4761-BE9B-003864D70605 | 9937733 | V | <57B1688C-3EE8-4E56-8092-BAF8270D3767@relay.pacombigroup.com> | 2019-05-28 20:47:44.990 | sender@domain.com | recipient@domain.com | 172.16.2.78 | 1 | NULL | | | |
| 490A0637-388D-4DD3-9372-0050EC04FAE7 | 9938751 | V | <0A226B21-4343-4123-8E5D-C8A2F804B8B2@relay.pacombigroup.com> | 2019-05-31 19:21:29.147 | sender@domain.com | recipient@domain.com | 172.16.2.78 | 1 | NULL | | | |
| BDEEC909-F708-4C79-BAC8-0057EC803252 | 9231144 | W01 | <FEAA2AE8-7808-484E-B1A3-920623DE4DF8@relay.pacombigroup.com> | 2019-06-03 15:15:26.007 | sender@domain.com | recipient@domain.com | 172.16.2.57 | 1 | NULL | | | |
| 7212EDE1-4500-4A03-B27F-005E67EE8528 | 9231612 | W01 | <369F701B-1D62-4409-B3D3-E6367E91EFE6@relay.pacombigroup.com> | 2019-06-04 18:10:13.913 | sender@domain.com | recipient@domain.com | 172.16.2.57 | 1 | NULL | | | |
| FFC0C644-4A43-4502-820E-005F32685CE7 | 311909810 | V | <C6EF87E9-0132-4494-9F6A-985749CD12C8@relay.pacombigroup.com> | 2019-06-06 21:44:16.193 | sender@domain.com | recipient@domain.com | 172.16.2.78 | 1 | NULL | | | |
| C7F22163-F776-484E-8139-00615FFC3F84 | 9940777 | V | <0433BA6C-A8EF-455A-8BA3-8B254CF7CA84@relay.pacombigroup.com> | 2019-06-08 00:08:30.017 | sender@domain.com | recipient@domain.com | 172.16.2.78 | 1 | NULL | | | |
| AD9415D7-126C-45E6-B829-0065124EA70C | 311908978 | V | <7E6F8283-A542-4119-96BE-4B040349CDE4@relay.pacombigroup.com> | 2019-05-23 21:32:49.513 | sender@domain.com | recipient@domain.com | 172.16.2.78 | 1 | NULL | | | |
| 247A6385-671D-4AF7-BEBA-0075BF12320C | 9940891 | V | <866BFCA3-92F1-4E7E-9445-88107F217D92@relay.pacombigroup.com> | 2019-06-11 11:24:14.803 | sender@domain.com | recipient@domain.com | 172.16.2.108 | 1 | NULL | | | |
| A24844C1-153C-4DC7-812E-0079EAD2DDD5 | 9230357 | W01 | <718DED8C-4CA2-43BB-A0F1-82FE7E32E460@relay.pacombigroup.com> | 2019-05-28 18:17:11.330 | sender@domain.com | recipient@domain.com | 172.16.2.57 | 1 | NULL | | | |
| B41D3BC1-56E4-49F3-A838-007C45565A87 | 311909841 | V | <760A11E9-5829-4276-A8CA-73885B209F74@relay.pacombigroup.com> | 2019-06-06 21:37:16.117 | sender@domain.com | recipient@domain.com | 172.16.2.78 | 1 | NULL | | | |
| 1E8926C6-F4E0-4DDE-985A-0089063BDBA4 | 9937563 | V | <ADA74A5C-74B9-4322-8051-59540554F6C3@relay.pacombigroup.com> | 2019-05-27 19:24:38.643 | sender@domain.com | recipient@domain.com | 172.16.2.78 | 1 | NULL | | | |
| 47A8CC55-7E25-4C81-AAC2-008C1E62E6F6 | 311909072 | V | <203992D3-9892-41BB-9031-9977A3EDC978@relay.pacombigroup.com> | 2019-05-24 23:26:32.157 | sender@domain.com | recipient@domain.com | 172.16.2.78 | 1 | NULL | | | |
| FDDE4FAB-6B84-4D29-AA62-008E0F77B66F | 9937220 | V | <176D67E8-52AB-42C6-958E-2C4D1C913819@relay.pacombigroup.com> | 2019-05-24 19:22:13.560 | sender@domain.com | recipient@domain.com | 172.16.2.78 | 1 | NULL | | | |
| E720070F-B3F5-42D6-A899-0099CD379C09 | 9229900 | W01 | <3D45A8CB-87AF-4A73-9B08-80F4098AA07E@relay.pacombigroup.com> | 2019-05-27 15:08:03.220 | sender@domain.com | recipient@domain.com | 172.16.2.57 | 1 | NULL | | | |
| D04BD8C1-6BBF-4DB1-A6C4-009E8CECCC63 | 9505925 | V05 | <0D2AE477-65CC-4DA0-BEDF-3861F3FB64FF@relay.pacombigroup.com> | 2019-05-24 21:56:22.550 | sender@domain.com | recipient@domain.com | 172.16.2.78 | 1 | NULL | | | |
| 37E35F68-BB22-4C06-BA57-009ECC4FF47A | 9407267 | V04 | <92DAC1C2-5735-44F0-B90B-B0B59AC5F03D@relay.pacombigroup.com> | 2019-05-24 21:09:08.763 | sender@domain.com | recipient@domain.com | 172.16.2.78 | 1 | NULL | | | |
| 207D930A-1225-4768-8ABF-00A114BC85FA | 636393442 | V | <2994754D-2BD4-4C33-AC01-2C09D0F9EE11@relay.pacombigroup.com> | 2019-06-04 10:51:37.830 | sender@domain.com | recipient@domain.com | 172.16.2.76 | 1 | NULL | | | |
| C67CA5CF-4AF9-4EF7-B40E-00A69BBCC351 | 9939771 | V | <90277372-15C5-4A5C-831C-4632F47B027B@relay.pacombigroup.com> | 2019-06-05 20:16:55.193 | sender@domain.com | recipient@domain.com | 172.16.2.78 | 1 | NULL | | | |
| CDC62243-8B9C-496C-86BB-00A85BABBC0A | 9940006 | V | <949EC972-241A-4EFF-8241-90FC5E60B7E0@relay.pacombigroup.com> | 2019-06-05 18:52:13.953 | sender@domain.com | recipient@domain.com | 172.16.2.78 | 1 | NULL | | | |
| 93036181-5268-4548-9A8F-00B09CCDD316 | 9939875 | V | <78C9D63A-567C-4E01-B1BF-BD5D70DAA98E@relay.pacombigroup.com> | 2019-06-05 19:06:25.580 | sender@domain.com | recipient@domain.com | 172.16.2.78 | 1 | NULL | | | |
| 6A0A7119-8CF4-451D-A12C-00B15E876CF0 | 9937709 | V | <E75B7085-D831-488D-B78E-8800AE99CE8B@relay.pacombigroup.com> | 2019-05-27 19:46:08.007 | sender@domain.com | recipient@domain.com | 172.16.2.78 | 1 | NULL | | | |
| 41CF9249-0101-4AE2-ACEE-00B3A045BEEF | 9938746 | V | <0339624B-9708-43C1-A7B4-366F06DDBE6D@relay.pacombigroup.com> | 2019-05-31 20:04:14.687 | sender@domain.com | recipient@domain.com | 172.16.2.78 | 1 | NULL | | | |
| B9C1649A-4CAF-4A0C-AE78-00B616030A57 | 9232022 | W01 | <4A8155EC-B3D7-414F-8095-4FA69D8B4B91@relay.pacombigroup.com> | 2019-06-06 17:21:31.590 | sender@domain.com | recipient@domain.com | 172.16.2.57 | 1 | NULL | | | |
| 9070853E-1659-4208-B200-00C24BA5FDE1 | 9229059 | W01 | <AFE5C70C-7678-4849-8DD3-BD5800BF81BE@relay.pacombigroup.com> | 2019-05-22 17:08:26.467 | sender@domain.com | recipient@domain.com | 172.16.2.57 | 1 | NULL | | | |
| B93C4E32-6EF0-44B7-B4EA-00C8CE642FCF | 9938830 | V | <9FA501AE-B38A-4F33-9B72-EC8CD594BDC4@relay.pacombigroup.com> | 2019-06-01 12:00:33.080 | sender@domain.com | recipient@domain.com | 172.16.0.83 | 1 | NULL | | | |
| 8600D2E7-A602-4032-811D-00CA17A2C559 | 9940767 | V | <BE4550E8-1B0E-4B25-9103-B227AF292A96@relay.pacombigroup.com> | 2019-06-08 00:25:59.760 | sender@domain.com | recipient@domain.com | 172.16.2.78 | 1 | NULL | | | |
| A40A621E-0E97-4FD7-B892-00D7CF9ABE05 | 9231322 | W01 | <99B65A5A-94C1-4613-A9C7-D432DC91F5B7@relay.pacombigroup.com> | 2019-06-03 17:21:37.307 | sender@domain.com | recipient@domain.com | 172.16.2.57 | 1 | NULL | | | |
| 51F069BC-977D-4C72-82F4-00DBFB3F498B | 9936699 | V | <D9FE8ADB-405C-412C-8A5E-D5240D7FE538@relay.pacombigroup.com> | 2019-05-23 19:16:51.480 | sender@domain.com | recipient@domain.com | 172.16.2.78 | 1 | NULL | | | |
| EF290E19-1446-419F-8492-00DF704A4D8B | 9232337 | W01 | <7F61D771-9A4B-4DC9-B05E-78F3A857A8DD@relay.pacombigroup.com> | 2019-06-07 17:28:13.777 | sender@domain.com | recipient@domain.com | 172.16.2.57 | 1 | NULL | | | |
| C98B9027-DA45-4E24-B22C-00EC6FD16C21 | 9007771 | V00 | <E2C0B457-9121-48B7-BB59-6239D5154DEC@relay.pacombigroup.com> | 2019-05-24 18:17:53.600 | sender@domain.com | recipient@domain.com | 172.16.2.78 | 1 | NULL | | | |
| 95B8C712-1827-4E3D-90A7-00F499086956 | 9008157 | V00 | <5C8952FC-849C-4C38-B540-F79369449A2E@relay.pacombigroup.com> | 2019-05-31 18:13:06.860 | sender@domain.com | recipient@domain.com | 172.16.2.78 | 1 | NULL | | | |
+--------------------------------------+-----------+---------+---------------------------------------------------------------+-------------------------+-------------------+----------------------+--------------+----+----------------------------------+-----------------------------------------------+------------------------------------------------+------------------------------------------------------------+
我想创建一个查询,该查询向我显示OK列中具有“ true”的记录数和OK列中具有“ false”的记录数。所有这些都应基于过去7天的工作日进行汇总,包括零。
例如:
+---------+-----+--------+
| WEEKDAY | OK | NOT OK |
+---------+-----+--------+
| Sunday | 0 | 0 |
| Monday | 100 | 7 |
| Tuesday | 95 | 18 |
+---------+-----+--------+
请帮我提供这个查询代码?
答案 0 :(得分:1)
检查以下脚本。这应该起作用。
SELECT FORMAT(SENTON, 'dddd') WEEKDAY,
SUM(CASE WHEN OK = 'true' THEN 1 ELSE 0 END) 'OK',
SUM(CASE WHEN OK = 'false' THEN 1 ELSE 0 END) 'NOT OK'
FROM Facturen
WHERE SENTON BETWEEN <start date> AND <end date>
GROUP BY FORMAT(SENTON, 'dddd')
答案 1 :(得分:0)
您可以尝试一下。
SET DATEFIRST 1
SELECT DATENAME(WEEKDAY, W.ID-1)
, COUNT(CASE WHEN [OK] = 1 THEN 1 End) [OK]
, COUNT(CASE WHEN [OK] = 0 THEN 1 End) [NOT OK]
FROM
(VALUES (1),(2),(3),(4),(5),(6),(7)) AS W(ID)
LEFT JOIN [Facturen] O ON W.ID = DATEPART(WEEKDAY, [SENTON])
AND [SENTON] > DATEADD(DAY, -7, GETDATE())
GROUP BY W.ID